How to get float value with SqlDataReader?
<p>In my database, I have NextStatDistanceTime value as a float. When "<code>float time = reader.GetFloat(0);</code>" line excecuted, it gives an error of</p>

<blockquote>
  <p>system invalid cast exception</p>
</blockquote>

<p>How can I get float value from sql command in this code?</p>

<p>Here is my code:</p>

<pre><code>using (SqlConnection conn = new SqlConnection(@"&lt;myconnectionstring&gt;"))
{
    float totaltime = 0;
    for (int i = startStationIndex; i &lt; endStationIndex; i++)
    {
        SqlCommand command = new SqlCommand("SELECT NextStatDistanceTime FROM [MetroDatabase].[dbo].[MetroStation] WHERE StationIndex = " + i + "", conn);
        try
        {
            conn.Open();
            command.ExecuteNonQuery();
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    float time = reader.GetFloat(0);
                    totaltime = totaltime + time;
                    conn.Close();
                }
            }                        
        }
        catch (Exception ex)
        {
            result = ex.Message;
            Console.WriteLine(ex.Message);
        }
    }

}
</code></pre>