Sync Services Part 2
In the second post of this series we will make it so changes we make to the local data will be sent back to the server. Lets start by opening the project we created in the previous post.
So lets extend the SyncAgent Partial classes to make the sync 2 way. The class contains a partial method OnInitialized which you can add code to. In this method we will make the Products table Sync Direction Bidirectional.
Partial Public Class NorthwindSyncAgent
Private Sub OnInitialized()
Me.Products.SyncDirection = SyncDirection.Bidirectional
End Sub
End Class
Now what if there is a conflict? Let create a form to give the user the option of keeping the changes he/she made or to accept the changes on the server. Add a form named frmConflict to the project. On the form add 2 buttons (btnClient, and btnServer) and 2 datagridviews (dgvClient, and dgvServer). Here is what my form looks like
Add the following code to the buttons
Private Sub btnClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClient.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Private Sub btnServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnServer.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
Now we need to extend the ServerSyncProvider to raise an event when there is a conflict. If the user presses the Keep my changes button we force the changes to be written to the database otherwise we allow the change to be made
Partial Public Class NorthwindServerSyncProvider
Private Sub OnInitialized()
AddHandler ApplyChangeFailed, AddressOf ApplyChangesFailed
End Sub
Public Sub ApplyChangesFailed(ByVal sender As Object, ByVal e As ApplyChangeFailedEventArgs)
Dim frm As New frmConflict
frm.dgvClient.DataSource = e.Conflict.ClientChange
frm.dgvServer.DataSource = e.Conflict.ServerChange
frm.ShowDialog()
If frm.DialogResult = DialogResult.OK Then
e.Action = ApplyAction.RetryWithForceWrite
Else
e.Action = ApplyAction.Continue
End If
End Sub
End Class