As we mentioned earlier, data-bound controls store the values passed to the data source in separate keys, values (new values), and oldvalues dictionaries. By default, SqlDataSource and ObjectDataSource ignore the OldValues field and use only keys and values. This behavior is detected by the ConflictDetection property of the data source, and the value of this property is set to OverwriteChanges by default. OverwriteChanges mode means "to update or delete records, just match primary key values". This operation means that the record is updated or deleted regardless of whether the underlying value of the record has changed. In general, the ideal state is to have the update or delete operation succeed only if the value of the data row matches exactly the value originally selected. In this ideal scenario, your update will fail if another user updates the line between the time you select a row and update the row. The data source supports this operation by setting the ConflictDetection property to CompareAllValues. In this mode, the data source applies the oldvalues to the command or method, which uses these values to ensure that the update or delete operation must match all the records ' values before updating or deleting the record. You must also set the OldValuesParameterFormatString property to a valid. NET Framework component format string (for example, "original_{0}"). To indicate how the parameters in the OldValues and keys dictionaries are renamed to distinguish them from the newvalues parameters.
The following code example shows a typical SQL command used by the SqlDataSource Control in OverwriteChanges and CompareAllValues mode. The ID field is assumed to be the primary key field. Note that the next command compares all the original values of the data row in the WHERE clause, instead of just comparing the primary key. In this case, the oldvaluesparameterformatstring of the data source needs to be set to "original_{0}".
SELECT [ID], [Name], [Address] from [Contacts]
-- OverwriteChanges
UPDATE [Contacts] SET [Name] = @Name, [Address] = @Address WHERE [ID] = @ID
DELETE FROM [Contacts] WHERE [ID] = @ID
-- CompareAllValues
UPDATE [Contacts] SET [Name] = @Name, [Address] = @Address WHERE [ID] = @original_ID
AND [Name] = @original_Name AND [Address] = @original_Address
DELETE FROM [Contacts] WHERE [ID] = @original_ID AND [Name] = @original_Name
AND [Address] = @original_Address
Note that the insert operation does not require oldvalues,conflictdetection to be meaningful only for the update and delete operations.
The following example shows the behavior when a conflict occurs. To run this example, you have to open two instances of the example in two separate browser windows (two clicks "Run Sample"). Then click the "Edit" button on the same line on the two form to get the row into edit mode. Change a value in the first window and click "Update", please note that this update is successful. In the second window, enter a new value in the row and click "Update", which is unsuccessful because the value of the underlying data row has been changed by the first update operation. This example detects the AffectedRows property of the updated or deleted event argument, which confirms that the conflict occurred for 0.
<script runat="server">
Protected Sub SqlDataSource1_Updated(sender As Object, e As SqlDataSourceStatusEventArgs)
If e.AffectedRows = 0 Then
Response.Write("Row changed, update aborted<br />")
End If
End Sub
Protected Sub SqlDataSource1_Deleted(sender As Object, e As SqlDataSourceStatusEventArgs)
If e.AffectedRows = 0 Then
Response.Write("Row changed, delete aborted<br />")
End If
End Sub
</script>