473,395 Members | 2,467 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,395 software developers and data experts.

Convert.ToDecimal(dr[2]).ToString("#,##0.00") and NULL

Bob

Hello folks.

I use this formating but if the value in dr[0] is a null it generates
an error

"Object cannot be cast from DBNull to other types."

Anybody know how I can deal with nulls?

Thanks,

Bob

Aug 1 '07 #1
6 21770
Bob wrote:
Hello folks.

I use this formating but if the value in dr[0] is a null it generates
an error

"Object cannot be cast from DBNull to other types."

Anybody know how I can deal with nulls?

Thanks,

Bob
Use dr.IsDBNull(0) to determine if the value is null before you try to
use the value.

--
Göran Andersson
_____
http://www.guffa.com
Aug 1 '07 #2
Bob <Go****@Yahoo.Comwrote on 8/1/2007 3:05:59 PM
>Hello folks.
I use this formating but if the value in dr[0] is a null it generates
an error
"Object cannot be cast from DBNull to other types."
Anybody know how I can deal with nulls?
Thanks,
Bob
<hr>
Bob,

create your own conversion for the decimal conversion. Convert.ToDecimal throws exceptions for DBNull, null, not-a-number, etc, etc.
It's better to catch those before-hand and in case of failure return 0 (most likely anyway).
You can use regular expressions for checking if it is really a decimal.

Besides that, one other point (assuming dr[2] = DataRow[third column]) you really should consider using data-column names and not indexes.

Leon.
Aug 1 '07 #3

Check for null first..

dr.IsDBNull(2) ? "" : Convert.ToDecimal(dr[2]).ToString("#,##0.00")

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Wed, 01 Aug 2007 06:03:05 -0700, Bob <Go****@Yahoo.Comwrote:
>
Hello folks.

I use this formating but if the value in dr[0] is a null it generates
an error

"Object cannot be cast from DBNull to other types."

Anybody know how I can deal with nulls?

Thanks,

Bob
Aug 1 '07 #4
On 01 Aug 2007 13:18:15 GMT, le***********@frost-nospam-bits.nl wrote:
>Besides that, one other point (assuming dr[2] = DataRow[third column]) you really should consider using data-column names and not indexes.

Leon.
There's a performance difference between using indexes and column
names. It's best not to use the column name based retrieval methods
on a datarow whenever you're retreiving many records.

You can use GetOrdinal(name) to get the index from a name, store that
in a local variable (outside the read loop) and then use that (inside
the read loop) to retrieve actual data from the reader.

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

Aug 1 '07 #5
On top of all the other suggestions here, I have to question you
converting to a decimal, and then back to a string. The only way I can see
this being of use is if the value in dr[2] is a number in string format,
which you want to express in a different format.

Otherwise, the value in dr[2] is a numeric type, and you should be able
to call ToString directly on it, you just have to perform a cast on the
value returned so that you can make the appropriate call:

((decimal) dr[2]).ToString("#,##0.00");

If the type of dr[2] is something other than decimal, I recommend
casting to that type, since you aren't going to gain anything by casting any
numeric type to decimal, and then formatting the string. If you had to do
further work with it, then I would understand the cast to decimal (for the
need for precision), but that isn't the case here.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bob" <Go****@Yahoo.Comwrote in message
news:11**********************@l70g2000hse.googlegr oups.com...
>
Hello folks.

I use this formating but if the value in dr[0] is a null it generates
an error

"Object cannot be cast from DBNull to other types."

Anybody know how I can deal with nulls?

Thanks,

Bob

Aug 1 '07 #6

Casting data from a data reader can be a problem with numbers
depending on the sql provider. Some providers will return different
data types depending on the particular data being returned (not always
even depending on the declared field type).

It's safer to always use Convert than cast. In case where you're
converting to the class it already is, the performance impact is
negligible (a call to the appropriate interface method which in the
end just does "return this").

Also, Convert.ToDecimal(dr[2]) can be give different results from
dr.GetDecimal(2) in some providers.

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Wed, 1 Aug 2007 11:40:32 -0400, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.comwrote:
On top of all the other suggestions here, I have to question you
converting to a decimal, and then back to a string. The only way I can see
this being of use is if the value in dr[2] is a number in string format,
which you want to express in a different format.

Otherwise, the value in dr[2] is a numeric type, and you should be able
to call ToString directly on it, you just have to perform a cast on the
value returned so that you can make the appropriate call:

((decimal) dr[2]).ToString("#,##0.00");

If the type of dr[2] is something other than decimal, I recommend
casting to that type, since you aren't going to gain anything by casting any
numeric type to decimal, and then formatting the string. If you had to do
further work with it, then I would understand the cast to decimal (for the
need for precision), but that isn't the case here.
Aug 1 '07 #7

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

Similar topics

2
by: Jianwei Sun | last post by:
Hi, guys, If I simply do a double d = 2.0 ,and then s=d.ToString() , it will trim the .0 off and return me 2, is there a good way to keep .0 and return me 2.0. Thanks,
1
by: Steven Spits | last post by:
Hi, Suppose decimal Price contains 12,3. Price.ToString("C2") gives 12,30 ? (yes, I'm located in Europe). What would be the best way to convert this string to a decimal again? Steven - -...
4
by: Larry Smith | last post by:
Once you call "ToString()" for an object, how can you then convert the string back to the original object (assuming you know its type of course)? Also, why does "ToString()" produce one string and...
1
by: Marc Gravell | last post by:
Just been debugging a daylight savings glitch... something that interested me: (in a BST locale at 5:14 local time, 4:14 GMT) DateTime.Now.ToString("R") >"Mon, 26 Mar 2007 05:14:34 GMT" ...
5
by: Bob | last post by:
Hello Folks, I am bring backa data reader dr from the database/ So I have a number of fields such as dr To put this in a table I use "<td>" + dr.ToString() + "</td>" But I want to...
5
by: piyumi80 | last post by:
hi, i write the following code to get a specific data row from the data set.but it generates the "Object reference not set to an instance of an object.".....error private void...
3
by: Ronald S. Cook | last post by:
Stupid q for the day, but if the user enters 2 in a textbox, I want to display as 2.00. If they enter 2.129 I want it as 2.13. I'm guessing will code in the validated method of the textbox,...
3
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi, I use Session to save and pass values between pages, (please see the following). It works fine on my computer, but on the server, I launch the main page, wait for 20 minutes, and click a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.