13.5 Null Data

The strongly typed DataSet adds two methods for each column, Is ColumnName Null( ) and Set ColumnName Null( ) , to make it easier and more intuitive to work with null values in DataRow columns . Is ColumnName Null( ) returns a Boolean value indicating whether the underlying field value is null , while Set ColumnName Null( ) sets the value of the underlying field to DBNull . The following sample demonstrates using the strongly typed DataSet null handling methods:

 // strongly typed DataSet called Northwind containing the Orders table Northwind.OrdersDataTable ordersTable = new Northwind.OrdersDataTable(); // ... code to add new rows to, or Fill the Orders table // check whether the CustomerID of the first row is null Northwind.OrdersRow ordersRow = ordersTable[0]; if(ordersRow.IsCustomerIDNull()) {     // ... code to handle the null CustomerID condition } // set the EmployeeID to null ordersRow.SetEmployeeIDNull(); 

This example shows comparable code using an untyped DataSet :

 // untyped DataSet containing the Orders table DataSet ds = new DataSet("Northwind"); DataTable ordersTable = ds.Tables.Add("Orders"); // ... code to define or retrieve the schema for the DataTable // ... code to add new rows to, or Fill the Orders table // check whether the CustomerID of the first row is null DataRow ordersRow = ordersTable.Rows[0]; if(ordersRow.IsNull("CustomerID")) {     // ... code to handle the null CustomerID condition } // set the EmployeeID to null ordersRow["EmployeeID"] = Convert.DBNull; 


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