ASP.Net Validation Value with DBNULL

I have the following code

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

but I get the following error The operator == cannot be applied to operands of type System.Data.DataRowView and System.DBNull

please can someone advise me how can I check if the value is null or DBNULL

+6
dbnull
source share
2 answers

You need to specify the name or index of the field.

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

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

 Convert.IsDBNull(dr["somefield"]) 

which returns true if DBNnull

+4
source share

All Articles