ASP.Net check value with DBNULL

c11ada picture c11ada · Mar 23, 2010 · Viewed 21.3k times · Source

I have the following code

foreach (DataRowView dr in Data)
        {
            if (dr == System.DBNull.Value)
            {
                nedID = 1;
            }
        }

but i get the following error Operator == cannot be applied to operands of type System.Data.DataRowView and System.DBNull

please can some one advice me on how i can check if the value is null or DBNULL

Answer

Michael Dean picture Michael Dean · Mar 23, 2010

You need to specify the field name or index.

foreach (DataRowView dr in Data)
{
    if (dr["nameOfField"] == System.DBNull.Value)
    {
        nedID = 1;
    }
}