retrieve a column value from a row that might be DBNull

  • Thread starter Thread starter marty.overdear
  • Start date Start date
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.
 
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.
 
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
 
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
 
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.
 
Back
Top