By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,587 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,587 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.