On the internet I found couple of articles about Sync Services and this part is really good described, but when I wont to implement conflicts in C# project I have some problems, because the examples I found were in VB.NET but I`m not a VB geek.
So I decided to write this post to explain how to handle conflicts in C# when we synchronize our local database cache with SQL Server 2008.
After we make new local database cache and we set bidirectional sync we need to handle conflicts.
An example of situation, when we have a conflict:


Befor changes


After changes
As we can see in example, we made some changes and now we wont to sync our local cache with server, but if we make this without any change in sync code the column on server (in our example “Daniel on server”) will downloaded to the client app because this is the default setting, but what if we wont to upload data even there is a conflict?
I show how to solve this issue:
public partial class SyncTestSyncAgent {
partial void OnInitialized(){
this.Customers.SyncDirection = SyncDirection.Bidirectional;
}
}
This is the code, than we need to add to sync bidirectional, right behaind we need to add new partial class (SyncTest is name of my SyncTest.sync file, you need to change it on your project to name that you choosed):
public partial class SyncTestServerSyncProvider
{
partial void OnInitialized()
{
// here we gone add code to handle conflicts
}
}
Add this code right behind:
public partial class SyncTestServerSyncProvider
{
partial void OnInitialized()
{
this.ApplyChangeFailed += new System.EventHandler
<ApplyChangeFailedEventArgs>
(SyncTestServerSyncProvider_ApplyChangeFailed);
}
private void SyncTestServerSyncProvider_ApplyChangeFailed
(object sender, ApplyChangeFailedEventArgs e)
{
// here we gone add code to check witch type of conflict we //wont to handle and how our app will make the changes on local //catch and server
}
}
At the end we can add an example code with show how to set client app to upload data even conflict type ClientUpdateServerUpdate (we have here couple of options: ClientDeleteServerUpdate, ClientInsertServerInsert, ClientUpdateServerDelete, ClientUpdateServerUpdate, ErrorsOccurred, Unknown) and as ApplyAction we can use Continue, RetryApplyingRow, RetryWithForceWrite:
if (e.Conflict.ConflictType == ConflictType.ClientUpdateServerUpdate)
{
MessageBox.Show("ClientUpdateServerUpdate conflict", "Conflict", MessageBoxButtons.OK, MessageBoxIcon.Information );
e.Action = ApplyAction.RetryWithForceWrite;
}
Our results before sync:

And after sync:

That’s all you have to do to handle conflicts on your OCA app.
If you have any questions please write to me: dan.dudek[at]hotmail[dot]com