June 21, 2018
Hot Topics:

Handling Data Conflicts in the Microsoft Sync Framework

  • April 23, 2009
  • By Matt Goebel, Rachel Baker
  • Send Email »
  • More Articles »

Resolving Conflicts with the ApplyChangeFailed Event

Knowing the conflict type is important, but you also need to know which rows are in conflict. MSF exposes the ApplyChangeFailed event on both the DbServerSyncProvider and the SqlCeClientSyncProvider that allows you to review the conflict information and decide how to handle it. Each event will be raised on either the server or the client SyncProvider depending on the phase of synchronization involved. The ApplyChangeFailedEventArgs object exposes the Action and Conflict properties. The Action property can be used to resolve the conflict by setting it to one of the ApplyAction types described in the previous section. The Conflict property describes in more detail the conflict, like its type and the conflicting rows. ApplyChangeFailedEventArgs also exposes the Context property, which allows you to programmatically change the data being synchronized, allowing you to create virtually any custom conflict resolution scheme. This gives you the most flexibility in applying custom application business logic to complex data conflicts.

Let's take a look at some examples. We created a demonstration project that shows a simple table that tracks customers' favorite numbers. The top half of the form shows the server copy of the data. The bottom half of the form shows the client cache's copy of the data. We can change the data in either of the tables, save the data to the server or the client, and then attempt to synchronize. If a data conflict is encountered, a custom form is displayed that allows us to choose one of the ApplyAction types to resolve the conflict.

Click here for larger image

Listing 1.2 Conflict resolution demonstration form

We hook up to the ApplyChangeFailed event by modifying some code in the data cache (.sync) file's code-behind, using partial classes. In our example, the code is as follows:

      public partial class DataConflictsDataCacheServerSyncProvider
          partial void OnInitialized()
              this.ApplyChangeFailed += new
          void DataConflictsDataCacheServerSyncProvider_ApplyChangeFailed(object sender, 
              Microsoft.Synchronization.Data.ApplyChangeFailedEventArgs e)
              ConflictResolverForm cr = new ConflictResolverForm();
              cr.Text = "Server Data Conflict Detected";
              cr.ApplyChangeEventArgs = e;
      public partial class DataConflictsDataCacheClientSyncProvider
          void DataConflictsDataCacheClientSyncProvider_ApplyChangeFailed(
              object sender, Microsoft.Synchronization.Data.ApplyChangeFailedEventArgs e)
              ConflictResolverForm cr = new ConflictResolverForm();
              cr.Text = "Client Data Conflict Detected";
              cr.ApplyChangeEventArgs = e;

We also must add the following line of code in the client SyncProvider's constructor (you can find it in the code of the .designer.cs code file):

              this.ApplyChangeFailed +=new 

In the ConflictResolverForm, we tell the Sync framework how to solve the conflict by setting the appropriate ApplyAction as follows:

applyChangeEventArgs.Action = Microsoft.Synchronization.Data.ApplyAction.Continue;

applyChangeEventArgs.Action = Microsoft.Synchronization.Data.ApplyAction.RetryApplyingRow;

applyChangeEventArgs.Action = Microsoft.Synchronization.Data.ApplyAction.RetryWithForceWrite;

Each of these actions is described in the examples on the following page.

Page 2 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date