ComponentOne Scheduler for WPF and Silverlight
Scheduler Components and Controls / Data-centric Architecture with Silverlight / The Sample Application / Implement the Client Side / Commit Changes to the Server
In This Topic
    Commit Changes to the Server
    In This Topic

    We are almost done now. The only piece still missing is the code that submits the changes made by the user back to the server, so they can be applied to the database.

    The first step is to add a button for commiting changes to server. The event handler for this button will save data back to server:

    C#
    Copy Code
    // Commit changes to server
    private void _btnCommit_Click(object sender, RoutedEventArgs e)
    {
      SaveData();
    }
    

    And here is the implementation for the SaveData method, which does the real work:

    C#
    Copy Code
    // Save data back into the database
    void SaveData()
    {
      if (_ds != null)
      {
        // get changes of each type
        byte[] dtAdded = GetChanges(DataRowState.Added);
        byte[] dtModified = GetChanges(DataRowState.Modified);
        byte[] dtDeleted = GetChanges(DataRowState.Deleted);
    
        // Invoke service
        var svc = new GetDataService();
        svc.UpdateDataCompleted += svc_UpdateDataCompleted;
        svc.UpdateDataAsync(dtAdded, dtModified, dtDeleted);
      }
    }
    void svc_UpdateDataCompleted(object sender, UpdateDataCompletedEventArgs e)
    {
      if (!string.IsNullOrEmpty(e.Result))
      {
        throw new Exception("Error updating data on the server: " + e.Result);
      }
      _tbStatus.Text = "Changes accepted by server.";
      _ds.AcceptChanges();
    }
    

    The method starts by calling the GetChanges method to build three byte arrays. Each one represents a DataSet with the rows that have been added, modified, or deleted since the data was downloaded from the server. Then the method invokes the Web service we implemented earlier and listens for the result. If any errors were detected while updating the server, we throw an exception (real applications would deal with the error in a more elegant way).

    The only piece still missing is the GetChanges method. Here it is:

    C#
    Copy Code
    byte[] GetChanges(DataRowState state)
    {
        DataSet ds = _ds.GetChanges(state);
        if (ds != null)
        {
            MemoryStream ms = new MemoryStream();
            ds.WriteXml(ms);
            return ms.ToArray();
        }
        return null;
    }
    

    The method uses the DataSet.GetChanges method to obtain a new DataSet object containing only the rows that have the DataRowState specified by the caller. This is the same method available on the ADO.NET DataSet class.

    The method then serializes the DataSet containing the changes into a MemoryStream, and returns the stream contents as a byte array.

    Try running the application now. Make some changes, then click the "Commit Changes" button to send the changes to the server. If you stop the application and start it again, you should see that the changes were indeed persisted to the database.