Search code examples
c#.netstrongly-typed-dataset

Validate DBNULL throwing "operator cannot be applied to operands of type int and system dbnull"


I thought it should be simple to validate but i failed to figure it by myself.

I am trying validate if the value have DBNULL or not

using

If((objAssessCircleRows[0].ClassValue) != System.DBNull.Value)
{
//do something

}

but it complains "operator cannot be applied to operands of type int and system dbnull".please some one guide me the right approach to validate DBNULL value

objAssessCircleRows[0] is a DataRow.


Solution

  • If ClassValue is an Int32, it will never be equal to DBNull.Value as there's no conversion between those two types. I'm guessing you're getting an exception when you try to call the getter for .ClassValue though.

    You'll want to use the IsNull(string) method to check first. Something like:

    if(objAssessCircleRows[0].IsNull("ClassValue")) // Column name from DataSet
    {
       //do something
    }
    

    Also, if this was generated using typed data sets, there should also be generated matching IsNull methods to check for a Null value:

    if(objAssessCircleRows[0].IsClassValueNull())
    {
       //do something
    }