8.4 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); 


ADO. NET in a Nutshell
ADO.NET in a Nutshell
ISBN: 0596003617
EAN: 2147483647
Year: 2005
Pages: 415

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net