Conversion of type object

S

Steve Wasser

I'm pulling SQL data into a dataset to be used to perform some math against.
I asked this question earlier, but the answer someone gave me left me with
further questions. The SQL data is stored as decimal, and the local
variables I create to store them in is float. Fine. When I iterate through a
datarow, I am assigning each cell to a variable. At least, I want to. I
always get the message cannot implicitly convert type 'object' to type
'decimal' (or 'float'). Does that mean when I call from SQL it doesn't
retain the same variable type? I've been getting around the problem by
boxing everything ToString then parsing, but jesus H, that's adding a
truckload of extra code. Is there a simpler way to do it my limited brain is
failing to comprehend? The other guy suggested if SQL stores it as type
decimal to assign the datarow cells to a decimal variable type....same
implicit error. Am I misunderstanding how C# pulls SQL data?
 
G

Guest

What is exact error message you are getting, this will help in solving the problem


----- Steve Wasser wrote: ----

I'm pulling SQL data into a dataset to be used to perform some math against
I asked this question earlier, but the answer someone gave me left me wit
further questions. The SQL data is stored as decimal, and the loca
variables I create to store them in is float. Fine. When I iterate through
datarow, I am assigning each cell to a variable. At least, I want to.
always get the message cannot implicitly convert type 'object' to typ
'decimal' (or 'float'). Does that mean when I call from SQL it doesn'
retain the same variable type? I've been getting around the problem b
boxing everything ToString then parsing, but jesus H, that's adding
truckload of extra code. Is there a simpler way to do it my limited brain i
failing to comprehend? The other guy suggested if SQL stores it as typ
decimal to assign the datarow cells to a decimal variable type....sam
implicit error. Am I misunderstanding how C# pulls SQL data
 
J

Jon Skeet [C# MVP]

Steve Wasser said:
I'm pulling SQL data into a dataset to be used to perform some math against.
I asked this question earlier, but the answer someone gave me left me with
further questions. The SQL data is stored as decimal, and the local
variables I create to store them in is float. Fine. When I iterate through a
datarow, I am assigning each cell to a variable. At least, I want to. I
always get the message cannot implicitly convert type 'object' to type
'decimal' (or 'float'). Does that mean when I call from SQL it doesn't
retain the same variable type?

No, it doesn't. It means that the signature for each cell in the row is
object, whatever has been stored. To get the decimal or double value,
you just have to cast:

double foo = (double) row["fieldname"];
 
S

Steve Wasser

Ahh, see, that's the thing. What you say makes sense, but I originally tried
to do that with the result "Specified cast is not valid".
For example:
g220 =
(decimal)dr["LiftingUnl"]+(decimal)dr["DeliveriesUnl"]+(decimal)dr["Adjustme
ntUnl"];

g220 is declared earlier as decimal (formerly float)...it was at that moment
I started boxing, but that adds like 700 lines of code and seems
superfluous. Maybe it's because I'm doing the math on the fly?
--
Steve Wasser
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
Jon Skeet said:
Steve Wasser said:
I'm pulling SQL data into a dataset to be used to perform some math against.
I asked this question earlier, but the answer someone gave me left me with
further questions. The SQL data is stored as decimal, and the local
variables I create to store them in is float. Fine. When I iterate through a
datarow, I am assigning each cell to a variable. At least, I want to. I
always get the message cannot implicitly convert type 'object' to type
'decimal' (or 'float'). Does that mean when I call from SQL it doesn't
retain the same variable type?

No, it doesn't. It means that the signature for each cell in the row is
object, whatever has been stored. To get the decimal or double value,
you just have to cast:

double foo = (double) row["fieldname"];
 
J

Jon Skeet [C# MVP]

Steve Wasser said:
Ahh, see, that's the thing. What you say makes sense, but I originally tried
to do that with the result "Specified cast is not valid".
For example:
g220 =
(decimal)dr["LiftingUnl"]+(decimal)dr["DeliveriesUnl"]+(decimal)dr["Adjustme
ntUnl"];

g220 is declared earlier as decimal (formerly float)...it was at that moment
I started boxing, but that adds like 700 lines of code and seems
superfluous. Maybe it's because I'm doing the math on the fly?

I suspect one of those columns isn't a decimal. I suggest you check
what the type of each of those columns is.
 
S

Steve Wasser

Would this happen if a column in the SQL database was empty?

--
Steve Wasser
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
Jon Skeet said:
Steve Wasser said:
Ahh, see, that's the thing. What you say makes sense, but I originally tried
to do that with the result "Specified cast is not valid".
For example:
g220 =
(decimal)dr["LiftingUnl"]+(decimal)dr["DeliveriesUnl"]+(decimal)dr["Adjustme
ntUnl"];

g220 is declared earlier as decimal (formerly float)...it was at that moment
I started boxing, but that adds like 700 lines of code and seems
superfluous. Maybe it's because I'm doing the math on the fly?

I suspect one of those columns isn't a decimal. I suggest you check
what the type of each of those columns is.
 
B

Brad Williams

Yup, NULL's will do that to you. Casting to this might work for you:
System.Data.SqlTypes.SqlDecimal
 
S

Steve Wasser

Thanks, but that also got the same error. How about this, is there a way to
do a foreach datarow and change any IsNull to a 0 and feed that back into
the DataTable?
 
S

Steve Wasser

Thank You! I was able to do a ForEach and replace the null with a zero.
Thanks to everyone!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top