473,416 Members | 1,983 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,416 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 5853
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.