Null value Check in DataTable?

L

lucius

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?

Thanks.
 
G

Gregg Walker

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?

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" );
}
 
W

WenYuan Wang [MSFT]

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

Larry Smith

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?

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()".
 
W

WenYuan Wang [MSFT]

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.
 

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