Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net check value with DBNULL

Tags:

asp.net

dbnull

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

like image 988
c11ada Avatar asked Mar 23 '10 16:03

c11ada


2 Answers

You need to specify the field name or index.

foreach (DataRowView dr in Data)
{
    if (dr["nameOfField"] == System.DBNull.Value)
    {
        nedID = 1;
    }
}
like image 194
Michael Dean Avatar answered Oct 12 '22 09:10

Michael Dean


You need to replace dr == System.DBNull.Value with...

Convert.IsDBNull(dr["somefield"])

which returns true if it's DBNnull

like image 45
Dead account Avatar answered Oct 12 '22 10:10

Dead account