retrieve a column value from a row that might be DBNull

M

marty.overdear

I have a dataset that I want to update. I pull the row out that I want
to update. There is a value in one of the columns that must be
analized to determine if other values need to change. This works great
as long as there is something in that column. If that DB column is
empty, I get this error...

"System.Data.StrongTypingException: Cannot get value because it is
DBNull. ---> System.InvalidCastException: Cast from type 'DBNull' to
type 'Decimal' is not valid.

I have tried all sorts of things.
I am wanting to get like, row.CCNum
THis is great unless CCNumis empty. I've tried the method
isDBNull(row.CCNum), but I still get the same error. How do I return a
value from a DB and not error out if its empty? I have multiple
columns to update, so I don't want to use an execute reader. I would
like to use the 'row' type methods to get this done.
 
R

rew190

Is your dataset typed? (I'm assuming so because of the reference to
row.CCNum).

If so, you should have an auto-generated method "isCCNumNull()" method.
Use that instead.
 
W

W.G. Ryan - MVP

Marty, you can just check it with
(IsDbNull(tablename.Rows[index][ColumnIndexorName])) ? WhateverDefaultvalue
: tablename.rows[index][ColumnIndexorName]; but if I understand you
correctly, this is what you're doing. I don't know if there's a cast or
something else in the expression, but here's an article that walks you
through it in depth.

http://www.knowdotnet.com/articles/handlingnullvalues.html

HTH,

Bill
 
M

marty.overdear

The auto-generated method "isCCNumNull" is there and worked great. Thx
for the info, had no clue it existed. I was trying the isdbnull stuff,
but couldn't get it to work out for me
 
R

rew190

Cool, glad that did it for you.

As an aside, there is also a SetCCNumNull() method that's
auto-generated that will set your field to DBNull.
 

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