Casting Obj to Float

  • Thread starter Thread starter Steve Wasser
  • Start date Start date
S

Steve Wasser

I posted this in the wrong forum, story of my life.

foreach (DataRow dr1 in MyTable1.Rows)
{

reg[count] = (float)dr1["REG"];
prem[count] = (float)dr1["PREM"];
die[count] = (float)dr1["DIESEL"];
eth[count] = (float)dr1["ETH"];
term[count] = reg[count]+prem[count]+die[count]+eth[count];
count++;
}

Results in specified cast is invalid. Do I have to box? I know I'm trying to
cram a ref into a val, but that's how SQL objects get retrieved.

Thanks,
 
If the type being returned by SQL is a truly a SQL float, then you need to
cast as *double*. It's just one of those idiosyncricies that a SQL Server
float == .NET System.Double and SQL Server *real* == .NET System.Single
(float in C#).

So if my assumption is true and you are using a SQL Server float type then
this narrowing cast should work:

reg[count] = (float)(double)dr1["REG"];

Richard
 
Back
Top