DekGenius.com
[ Team LiB ] Previous Section Next Section

13.5 Null Data

The strongly typed DataSet adds two methods for each column, IsColumnNameNull( ) and SetColumnNameNull( ), to make it easier and more intuitive to work with null values in DataRow columns. IsColumnNameNull( ) returns a Boolean value indicating whether the underlying field value is null, while SetColumnNameNull( ) 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;
    [ Team LiB ] Previous Section Next Section