Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

InvalidCastException for Float datatype

Status
Not open for further replies.

larrydavid

Programmer
Jul 22, 2010
174
US
Hello,

I have this code...

Code:
if (dr["Test_Ratio"] != null && (float)dr["Test_Ratio"] > 0.005)

...which is pulling from this output in a SQL Server 2000 table:

Code:
[Test_Ratio]
0.049322094006
0.016395231470
0.008675014321
0.008047576179
0.007420138036
0.006629020378
0.005919742477
0.005892462558
0.005701503123
0.005210464577
0.004883105546
0.004855825626
0.004719426030
0.003955588291

The Data Type is: numeric(24, 12)

The error is: "When casting from a number, the value must be a number less than infinity."

I've tried several approaches including rounding, casting to int, float and double datatypes. I'm stumped so any help is greatly appreciated.

Thanks,
Larry
 
If you're pulling data from SQL Server then != null will never work, you have to do a comparison with DBNull.Value. ;

Code:
DBNull.Value.Equals(dr["Test_Ratio"])

Hope this helps...

Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"
Terry Pratchett
 
actually, I found the solution:

if (dr["Denial Ratio"] != null && (decimal)dr["Denial Ratio"] > 0.005m)

If you have to use double, use double.TryParse(dr["Denial Ratio"].ToString(),out var) and then compare the var to 0.005d.

Larry


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top