Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Testing for DBNull

I'm working with data from a table that may have a column with a null value. It seems to me the following line should test for that condition:

If IsDBNull(Books.Title) Then ...

But when the program encounters that line for a record with a null, it throws an exception: "The value for column 'Title' in table 'Books' is DBNull."

If I wrap the line with a Try/Catch, the exception is reported just before the line gets executed.

I've tried other ways of testing, like this:

If Books.Title.IsNullOrEmpty ...

But that doesn't seem to work for a strongly-typed variable.

Does anyone have suggestions?

Thanks,

Benn Gibson
[735 byte] By [bgibson] at [2007-11-11 10:19:03]
# 1 Re: Testing for DBNull
Not sure if this will help,

I've used If IsDBNull(field) along with IsNothing(field) or if not IsDBNull(field). These seem to do the trick for me.

If IsDBNull(field) or IsNothing(field) Then...
or
If Not IsDBNull(field)...

Hope this helps... :( Sorry if it doesn't.

jb
jcb1269 at 2007-11-11 20:48:15 >
# 2 Re: Testing for DBNull
Thanks for the suggestion. Mysteriously, though, the problem went away before I had a chance to try your idea.

I'm bewildered but not complaining!

Benn
bgibson at 2007-11-11 20:49:21 >