Updating data

Now you will add another button to the form that updates any changes the user makes in the detail DataWindow to the database. You use the UpdateData method to update data in the database.

NoteUse UpdateData, not Update The DataWindowControl has both Update and UpdateData methods. Update is inherited from System.Windows.Forms.Control and causes the control to redraw invalidated regions in its client area. Use UpdateData to update data to the database.

UpdateData can be called with two boolean arguments. If the first argument is true, the data that the user last entered is automatically validated and stored in the control’s buffer. If the second argument is true, retained information about what data has been modified is automatically reset. In this case, you call UpdateData with the second argument set to false, and then use ResetUpdateStatus to clear the status flags manually.

The manual approach is preferred if you expect there might be data validation errors triggered at the DBMS level. If the update fails in that case, you might want to retain the status of the DataWindow before the update to allow the user to amend the data and retry.

If UpdateData throws an exception, the code that commits the transaction and clears the delete buffer is never reached.

StepsTo update data in the database:

  1. Add a button to the form as you did in “Retrieving data into the master DataWindow”.

  2. In the button’s Properties window, change the Name property of the button to btnUpdate and the Text property to Update.

  3. Double-click the button to add an event handler for its Click event.

  4. In the code editor, complete the code for the event handler as follows:

  5. [Visual Basic]
    Private Sub btnUpdate_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) _
    Handles btnUpdate.Click
    
       Try
         dwCustomer.UpdateData(True, False)
         myTrans.Commit()
         dwCustomer.ResetUpdateStatus()
       Catch ex As Sybase.DataWindow.DbErrorException
         MessageBox.Show(ex.SqlErrorText + vbcrlf _
         + "No changes made to the database.", _
         "Database Error", MessageBoxButtons.OK, _
         MessageBoxIcon.Warning)
    
       Catch ex As Exception
         MessageBox.Show(ex.ToString() + vbcrlf + _
         "No changes made to the database.", _
         "Unexpected Exception", _
         MessageBoxButtons.OK, MessageBoxIcon.Warning)
    
       End Try
    End Sub
    
    [C#]
    private void btnUpdate_Click(object sender,
       System.EventArgs e)
      {
       try 
       {
        dwCustomer.UpdateData(true, false);
        myTrans.Commit();
        dwCustomer.ResetUpdateStatus();
       }
       catch (Sybase.DataWindow.DbErrorException ex)
       {
        MessageBox.Show(ex.SqlErrorText + 
        "\n\nNo changes made to the database.",
         "Database Error", MessageBoxButtons.OK, 
         MessageBoxIcon.Warning);
       }
       catch (Exception ex)
       {
        MessageBox.Show(ex.ToString() + 
        "\n\nNo changes made to the database.", 
        "Unexpected Exception", MessageBoxButtons.OK, 
         MessageBoxIcon.Warning);
       }
      }
    
  6. Select Debug>Start Debugging to run the program, and click the Retrieve button.

  7. Change the first name of the customer displayed in the detail DataWindow, and click the Update button.

    The name does not change in the master DataWindow.

  8. Now click the Retrieve button again.

    The data in the master DataWindow is refreshed, and your name change displays.

  9. Close the application.