Null value Check in DataTable?

  • Thread starter Thread starter lucius
  • Start date Start date
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.
 
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" );
}
 
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.
 
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()".
 
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.
 
Back
Top