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.

Re: Null value Check in DataTable? by Gregg

Gregg
Wed Jul 25 14:11:01 CDT 2007

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



RE: Null value Check in DataTable? by v-wywang

v-wywang
Thu Jul 26 03:48:24 CDT 2007

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.


Re: Null value Check in DataTable? by Larry

Larry
Thu Jul 26 14:07:36 CDT 2007

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



RE: Null value Check in DataTable? by v-wywang

v-wywang
Mon Jul 30 06:17:23 CDT 2007

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.