Groups | Blog | Home
all groups > dotnet ado.net > july 2007 >

dotnet ado.net : Null value Check in DataTable?


Gregg Walker
7/25/2007 12:11:01 PM
[quoted text, click to view]

Nulls in DataTable are stored as DBNull.Value (not *null* as you were
checking for).

So try this...

if ( this.Rows[0]["Col_A"] is DBNull)
{
throw new Exception ( "no" );
}

lucius
7/25/2007 3:00:00 PM

I have a DataRow in a DataTable. I want to know if there is a value. I
do this

if ( this.Rows[0]["Col_A"] == null )
{
throw new Exception ( "no" );
}
However this condition is never hit. Why?

v-wywang NO[at]SPAM online.microsoft.com
7/26/2007 12:00:00 AM
Hello Lucius,

The DBNull class represents a nonexistent value in ADO.net.

I agree with Gregg.
this.Rows[0]["Col_A"] is DBNull
should works fine in your case.

Have a great day,
Best regards,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Larry Smith
7/26/2007 3:07:36 PM
[quoted text, click to view]

See "DataRow.IsNull()". Note that you should rely on strongly-typed DataSets
instead however. It's much easier and cleaner than the route you're now
taking. Each strongly-typed "DataRow" derivative also has an "IsNull()"
member for each (nullable) column of your tables. Using your example above
you would then just call "YourRow.IsCol_ANull()".

v-wywang NO[at]SPAM online.microsoft.com
7/30/2007 11:17:23 AM
Hello Lucius,

Have you resolved the issue so far?
I just want to check if there is anything we can help.
Please feel free to update here, we are glad to assist you.

Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button