The DBNull represents an uninitialized variant or nonexistent database column. It is not the number zero or it is not an empty string value. DBNull is a singleton class, which means only this instance of this class can exist. The DBNull.Value member represents the sole DBNull object.
In many situations while reading data from DataSource, we have seen the error message like the following :
Conversion from type 'DBNull' to type '' is not valid
This message is getting because the ASP.NET program unable to handle DBNull value. In these cases you can determine whether a value retrieved from a database field is a DBNull value by passing the value of that field to the DBNull.Value.Equals method.
vb.netIf IsDBNull(ds.Tables(0).Rows(i).Item(0)) Then Label1.Text = "DBNULL exist in the field "End If
if (ds.Tables[0].Rows[0].ItemArray[0] == System.DBNull.Value)
{
Label1.Text = "DBNULL exist in the field ";
}
The following ASP.NET program is checking wether the retrieved values is DBNull.
Default.aspx
Untitled Page
Click the following links to see full source code
Advertisement