RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Build Occasionally Connected Systems Using Sync Services for ADO.NET v1.0 : Page 3

When workers aren't always connected, Occasionally Connected Systems (OCS) can help. Find out how Visual Studio 8's Sync Services for ADO.NET enables OCS, synchronizing databases between different devices and computers.

Syncing the Database
Select Data—>Show Data Sources and drag-and-drop the authors table onto Form1 (see Figure 7). Drag-and-drop a Button control onto the form, as well.

Figure 7. Form1: The results of the synchronization process.
Figure 8. The Synchronize() Method: The image shows the results of the synchronization process.

Double-click on the Synchronize button and code it as follows:

        private void btnSynchronize_Click(object sender, EventArgs e)
            //---sync with the database---
            LocalAuthorsSyncAgent syncAgent = new LocalAuthorsSyncAgent();
            Microsoft.Synchronization.Data.SyncStatistics syncStats = 

            //---reload the form to refresh the data---
            this.Form1_Load(null, null);

What you are doing here is creating an instance of the LocalAuthorsSyncAgent class (defined in the LocalAuthors.Designer.cs file) and then calling its Synchronize() method to perform the synchronization. The Synchronize() method returns a result of type Microsoft.Synchronization.Data.SyncStatistics. You can set a breakpoint here to examine the results returned by the Synchronize() method (see Figure 8).

Right-click on the LocalAuthors.sync file and select View Code. Add the code shown below in bold type:

namespace SyncServices {  
    public partial class LocalAuthorsSyncAgent {
        partial void OnInitialized(){
            this.authors.SyncDirection = 
Here, you are setting the synchronization direction to Bidirectional. Possible values are: Bidrectional, DownloadOnly (default), Snapshot, and UploadOnly. By setting the synchronization direction to Bidirectional, all modifications to the databases (server and client) will be synchronized.

That's it! Press F5 to test the application. You can make changes to the records in the DataGridView control and then save the changes. The changes are then saved to the local pubs.sdf database. To synchronize the changes with the server, click the Synchronize button (see Figure 9).

Figure 9. Testing the Application: To synchronize the changes with the server, click the Synchronize button.
Figure 10. The Updated Record: Right-click on the table name in Server Explorer and select Show Table Data.

Stop the application and you can now examine the server database. Right-click on the table name (authors) in Server Explorer and then select Show Table Data (see Figure 10). You will now be able to see the updated record.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date