8

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

David Basarab
  • 72,212
  • 42
  • 129
  • 156
c11ada
  • 4,302
  • 15
  • 48
  • 62

2 Answers2

13

You need to specify the field name or index.

foreach (DataRowView dr in Data)
{
    if (dr["nameOfField"] == System.DBNull.Value)
    {
        nedID = 1;
    }
}
Michael Dean
  • 1,506
  • 9
  • 11
5

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

Convert.IsDBNull(dr["somefield"])

which returns true if it's DBNnull

Dead account
  • 19,587
  • 13
  • 52
  • 82