How to Track Changes to an ADO.NET* DataSet in a Mobilized Application

Submit New Article

December 9, 2008 11:00 PM PST



Challenge

Create a DataSet containing only changed records for returning to the central server. This is a common requirement in a mobilized application.

The insert, update, and delete changes that are covered in the separate item, How to Make Local Changes to an ADO.NET DataSet in a Mobilized Application are made locally in the DataSet. Even if the data was originally downloaded from an external data source, no changes are sent back to the server, and no connection to the server is required.


Solution

Use the DataSet that has a GetChanges method that can return a DataSet with the same structure as the original, but containing only the rows that are in a particular DataRowState. To track the changes, each DataRow maintains up to four versions of its data: Current, Default, Original, and Proposed. To represent these in code, use the DataRowVersion enumeration. In addition, the DataRow object has a RowState property, which can be represented by one of the DataRowState enumeration values.

Thus, your application can display records filtered by their row state in two ways:

  • Create an array of DataRows by using the DataViewRowState argument in the Select method of the DataTable.
  • Create a filtered DataView by using its RowStateFilter property.

 

In either case, you will need one of the values of a third enumeration: DataViewRowState. Each value in DataViewRowState specifies a separate combination of DataRowState and DataRowVersion to control what rows are included.

dsChanges = dsTitles.GetChanges(DataRowState.Modified);

//clone the structure from the original
dsChanges = dsTitles.Clone();
DataRow[] drChanges; //array of selected data rows
DataRow drNew;
int iRow;
int iCol;
//make the selection
drChanges = dsTitles.Tables[0].Select("", "", DataViewRowState.ModifiedCurrent);
//copy array into dsChanges
for (iRow=0; iRow <= drChanges.GetUpperBound(0); iRow++)
{
drNew = dsChanges.Tables[0].NewRow();
//copy all cols in row
for(iCol=0;iCol<=drNew.ItemArray.GetUpperBound(0);iCol++)
{
drNew[iCol] = drChanges[iRow].ItemArray[iCol];
}
dsChanges.Tables[0].Rows.Add(drNew);
}

 

This item may be most useful when used in conjunction with the following, separate items:

 


Sources

Data Synchronization with the ADO.NET* DataSet

Identifying and Extracting Data Changes in the ADO.NET* Dataset