Handling Null Values
The AllowDBNull
property determines whether
null
values
can be stored in the column in rows. This value is
true
by default.
The System.DBNull
class must be used to set a column value
to null
and can test whether a column contains a
null
value. Using the null
keyword results in a runtime error. The following code demonstrates
this concept:
DataRow row; // ... retrieve the DataRow // set the value of the first column in the row to null row[0] = DBNull.Value; // test the first column to determine if it contains a null value Boolean isNull = (row[0] == DBNull.Value);
The IsNull( )
method also allows the columns in the
DataRow
to be tested for null
values using a more convenient syntax. The method returns a Boolean
value indicating whether the value for the specified column in the
row is null
.
DataRow row; // ... retrieve the DataRow // test the first column to determine if it contains a null value Boolean isNull = row.IsNull(0);
Get ADO.NET in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.