[ Team LiB ] Previous Section Next Section

13.4 Finding a Row

The strongly typed DataSet has a FindBy( ) method to locate a row in a DataTable based on the primary key. The method accepts one argument for each of the columns that make up the primary key. The method has named arguments, making it easier to work with than similar untyped DataSet code. Additionally, the arguments are typed, allowing mismatch errors to be caught at compilation time rather than at runtime. The following example demonstrates using the strongly typed DataSet FindBy( ) method and also shows comparable code using an untyped DataSet.

// strongly typed DataSet called Northwind containing the Orders table
Northwind.Order_DetailsDataTable table =
  new Northwind.Order_DetailsDataTable();

// ... code to add new rows to, or Fill the Orders table

// locate the row based on its primary key value
Northwind.Order_DetailsRow orderDetailsRow
    = table.FindByOrderIDProductID(10248, 11);
if(orderDetailsRow != null)
{
    // ... code to process the row
}

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

// locate the row based on its primary key value
DataRow orderDetailsRow  =  ordersTable.Find(new Object[] {10248, 11});
if(orderDetailsRow != null)
{
    // ... code to process the row
}
    [ Team LiB ] Previous Section Next Section