DataSets: Ensuring you deliver 'unchanged' data to the Client

From OpenPetra Wiki
Jump to navigation Jump to search

Problem

  • A screen holds modified DataRows although no data has been modified since that data got loaded from the server.
  • A screen holds more modified DataRows than the user has modified since that data got loaded from the server.

Explanation

When data is loaded from the database into a DataSet on the server-side that data is held in (at least one) DataTable(s) in that DataSet, and inside the DataTable(s) the data is held in DataRow(s). When this DataSet is returned to the Client, not just all the data, but also the 'state' of the data, is returned. (The same is true when data gets loaded from the database into a DataTable that isn't part of a DataSet.)

As the data gets loaded from the DB into the DataTable(s) each DataRow that holds loaded data gets DataRowState.Modified. If the DataSet (or DataTable) is returned like this to the Client, the Client will get DataRows with DataRowState.Modified, too! That is not what we want in data edit screen (normally), as this causes all DataRows that were originally loaded from the Server to get saved back to the Server (unnecessarily!) when the user presses 'Save'. The reason for this is that the client-side saving logic detects those originally loaded DataRows as being 'changed' (since it can't possibly distinguish between user-changed DataRows and DataRows that were originally loaded, but had DataRowState.Modified)!

Solution

The simple solution for getting 'unchanged' data to the Client is to call .AcceptChanges() on the DataSet just before it is returned to the Client. This ensures that all loaded DataRows have DataRowVersion.Unchanged!