0

I'm retrieving some data with a SqlDataReader and trying to convert one of these objects to a float (the same as its data type in the database).

What I have:

u.unitid = (float)Convert.ToSingle((reader["UNITID"]));

This returns a "0" for every unitid.

10
  • have you tried converting it directly to float instead of first converting it tosingle then casting it to float? Commented Apr 18, 2018 at 14:57
  • 1
    It makes no sense to case the result of ToSingle to float since it is already a float. Have you tried the reader.GetFloat() method? What is the column type of UNITED in the database? Commented Apr 18, 2018 at 14:59
  • 2
    ToSingle already gives a float, why second cast? Also, what are the values of unitid? Commented Apr 18, 2018 at 14:59
  • Did you know DataReaders have a number of typed Getxxxx methods such as GetSingle()? If it always returns 0 for any row consider that the query may be wrong etc Commented Apr 18, 2018 at 15:05
  • Single is the CLR name that C# aliases as float. They are the same, the cast is not necessary. Commented Apr 18, 2018 at 15:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.