473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Conversion of type object

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?

--
Steve Wasser
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
Nov 15 '05 #1
8 5851
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

--
Steve Wasse
http://xdissent.co
the journal of contrarian social discourse and neurotic opinio

Nov 15 '05 #2
Steve Wasser <se******@hotmail.com> 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 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"];

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Steve Wasser <se******@hotmail.com> 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 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"];

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
Steve Wasser <se******@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Steve Wasser <se******@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6
Yup, NULL's will do that to you. Casting to this might work for you:
System.Data.SqlTypes.SqlDecimal
Nov 15 '05 #7
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?

--
Steve Wasser
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
"Brad Williams" <br**************@intel.com> wrote in message
news:c2**********@news01.intel.com...
Yup, NULL's will do that to you. Casting to this might work for you:
System.Data.SqlTypes.SqlDecimal

Nov 15 '05 #8
Thank You! I was able to do a ForEach and replace the null with a zero.
Thanks to everyone!

--
Steve Wasser
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
"Brad Williams" <br**************@intel.com> wrote in message
news:c2**********@news01.intel.com...
Yup, NULL's will do that to you. Casting to this might work for you:
System.Data.SqlTypes.SqlDecimal

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
7
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But...
10
by: junky_fellow | last post by:
K&R say that, It is guaranteed that 1) a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and 2) back again without...
2
by: Sankar Nemani | last post by:
Why does the following code throw an exception? object o = new object{"sankar", "sankar2"}; string s; s =(string)o; The C# language spec says something different on MSDN: For any two...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
10
by: =?Utf-8?B?RWxlbmE=?= | last post by:
I am surprised to discover that c# automatically converts an integer to a string when concatenating with the "+" operator. I thought c# was supposed to be very strict about types. Doesn't it seem...
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
6
by: Peter Lee | last post by:
what's the correct behaver about the following code ? ( C++ standard ) I got a very strange result.... class MyClass { public: MyClass(const char* p) { printf("ctor p=%s\n", p);
2
by: =?Utf-8?B?U2FtZWVrc2hh?= | last post by:
Suppose there are 2 classes A and B with a int parameter called 'val' in each. Both of them provide a public constructor with int type parameter. Suppose class A provides a explicit conversion...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.