My C# ExecuteScalar command was throwing an invalid cast from my call when assigning to a float even though the return value from the SQL was a float. So I did a simple test as follows:
sqlCU = "select cast(1 as float)";
using (SqlCommand command = new SqlCommand(sqlCU, con))
{
object cuP5300x = command.ExecuteScalar();
}
What comes back is always a double.
So to fix it I have to do something what I think is rather silly:
float cuP5300x = (float)((double)command.ExecuteScalar())
because as mentioned
float cuP5300x = (float)command.ExecuteScalar();
Causes an invalid cast exception.
Can anyone explain why, I cannot find anything in the MS Documentation to say that floats are doubles, and is there a more sensible way...?
thanks.