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

B

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Bob said:
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.
 
L

leon.friesema

Bob said:
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.
 
S

Samuel R. Neff

Check for null first..

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

HTH,

Sam
 
S

Samuel R. Neff

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
S

Samuel R. Neff

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
 

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