NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Accepting or Rejecting changes to DataTable rows

Once you sure of the changes you've made, you'll want to commit the changes and move the current row values to be the original values. At this point we need to understand that everything we've talked about is in reference to the data held in-memory in the DataSet. We'll discuss how accepting or rejecting changes in the dataset effect updating data in the data store in the section on managed providers.

The code below shows two ways of committing (accepting) the row changes.

Example 1:

[VB]
workTable.AcceptChanges()

[C#]
workTable.AcceptChanges();

Example 2:

[VB]
Public Function AcceptChanges(ByVal workTable As DataTable) As Integer
   Dim acceptedRows As Integer = 0
   Dim i As Integer
   For i = 0 To workTable.Rows.Count – 1
      If Not workTable.Rows(i).HasErrors Then
         acceptedRows += 1
         workTable.Rows(i).AcceptChanges()
      Else
         workTable.Rows(i).RejectChanges()
      End If
   End If
   AcceptChanges = acceptedRows
End Function

public int AcceptChanges(DataTable workTable)
{
 int acceptedRows = 0;

 for (int i = 0; i <= workTable.Rows.Count -1 ; i++)
 {
 if (!workTable.Rows[i].HasErrors )
 {
       acceptedRows++;
       workTable.Rows[i].AcceptChanges();
 }
 else
  {
    workTable.Rows[i].RejectChanges();
  }
 }
 return acceptedRows;
}

The first example accepts all changes that have been made to the table. The second example loops through the changes and accepts each on a row-by-row basis. To determine if AcceptChanges should be called, the code examines the HasErrors property.

[VB]
If Not workTable.Rows(i).HasErrors) Then

[C#]
if (!workTable.Rows[i].HasErrors )

The DataRow allows you to enter an error string and associate it with a row within the table. If any row in the table has an error string associated with it, the HasErrors property will return true. In the second example, we iterate through the rows and accept changes for each that doesn't have any errors.

Once the changes on a row are accepted, all of the original values for the row are replaced with the current values. The row is no longer consisted to have been modified.