Retrieving data

You need to retrieve data into both DataWindow objects. You will add a button to the form to populate the master DataWindow, then code its RowFocusChanged event handler to populate the detail DataWindow.

Retrieving data into the master DataWindow

First add a button to the form that will retrieve data into the master DataWindow.

StepsTo retrieve data into the master DataWindow:

  1. Make sure there is enough room at the bottom of the form to add buttons, then expand the All Windows Forms tab in the Visual Studio .NET Toolbox and drag the Button item to the form.

  2. Right-click the button and select Properties, change the Name property to btnRetrieve and the Text property to Retrieve, and close the Properties window.

  3. Double-click the button.

    This creates an event handler for the button’s clicked event and opens the code editor.

  4. Type the following in the btnRetrieve_Click event handler (add a semicolon to the end of the line for C#):

    dwCustList.Retrieve()
    

    This line retrieves data into the master DataWindow, dwCustList. Select Debug>Start Debugging to run the program. When you click the Retrieve button, the first DataWindow displays a list of customers.

  5. Close the application.

Retrieving data into the detail DataWindow

Since these DataWindow objects have a master-detail relationship, you want the details for the row that has focus in dwCustList to display in dwCustomer. To do that, you need to write code in the RowFocusChanged event handler for dwCustList.

The second DataWindow object, d_customer, has a WHERE clause that you set up in the SQL painter (see “Specify a WHERE clause”):

WHERE "customer"."id" = :cust_id

The Retrieve method can take a list of retrieval arguments as a parameter. In this case, you use the GetItemDouble method to return the value of the customer id column in dwCustList that has focus. That value is then passed to the Retrieve method for dwCustomer to be used within the WHERE clause.

In Visual Studio .NET, the techniques for adding an event handler are different in Visual Basic and C#.

StepsTo retrieve data into the detail DataWindow in Visual Basic:

  1. Right-click on dwCustList and select View Code.

  2. From the drop-down list on the right in the Code Editor, select RowFocusChanged.

    This generates the event handler method and places the edit cursor in the body of the method.

  3. Now code the event handler. The following code is wrapped in a try-catch statement that catches database errors and other exceptions:

    Private Sub dwCustList_RowFocusChanged(ByVal  _
    sender As System.Object, ByVal e As  _
    Sybase.DataWindow.RowFocusChangedEventArgs)  _
    Handles dwCustList.RowFocusChanged
       Try
         ' deselect all rows, then select current row
         dwCustList.SelectRow(0, False)
         dwCustList.SelectRow(e.RowNumber, True)
    
         dwCustomer.Retrieve(dwCustList.GetItemDouble _
         (e.RowNumber, "id"))
       Catch ex As Sybase.DataWindow.DbErrorException
          dwCustomer.Reset()
          MessageBox.Show(ex.SqlErrorText, _
          "DataWindow Operation Failed", _
          MessageBoxButtons.OK, MessageBoxIcon.Stop)
       Catch ex As Exception
          dwCustomer.Reset()
          MessageBox.Show(ex.ToString(), _
          "Unexpected Exception", _
          MessageBoxButtons.OK, MessageBoxIcon.Stop)
       End Try
    End Sub
    

StepsTo retrieve data into the detail DataWindow in C#:

  1. Right-click on dwCustList and select Properties.

  2. In the Properties window, click the Events button and double-click RowFocusChanged in the list of events.

    This adds a declaration of the event handler for dwCustList to the setup code in the InitializeComponent method:

    this.dwCustList.RowFocusChanged += new Sybase.DataWindow.RowFocusChangedEventHandler(this.dwCustList_RowFocusChanged);
    

    It also generates the event handler method and places the edit cursor in the body of the method.

  3. Now code the event handler. The following code is wrapped in a try-catch statement that catches database errors and other exceptions:

    private void dwCustList_RowFocusChanged
       (object sender,
       Sybase.DataWindow.RowFocusChangedEventArgs e)
      {
       try 
       {
         // deselect all rows, then select current row
         dwCustList.SelectRow(0, false);
         dwCustList.SelectRow(e.RowNumber,
          true);
    
         dwCustomer.Retrieve(dwCustList.GetItemDouble
          ((int) e.RowNumber, "id"));
       } 
    
         // catch database error and reset dwCustList
       catch (Sybase.DataWindow.DbErrorException ex) 
       {
         dwCustomer.Reset();
         MessageBox.Show(ex.SqlErrorText, 
          "DataWindow Operation Failed",
          MessageBoxButtons.OK, MessageBoxIcon.Stop);
       }
       catch (Exception ex)
       { 
         dwCustomer.Reset();
         MessageBox.Show(ex.ToString(), "Unexpected
          Exception", MessageBoxButtons.OK,
          MessageBoxIcon.Stop);
       }
      }
    

Testing retrieve

Now you can test whether selecting a different row in the master DataWindow changes the detail DataWindow.

StepsTo test the interaction between the master and detail DataWindows:

  1. Select Debug>Start Debugging to run the program, and click the Retrieve button.

    The first DataWindow displays a list of customers and the second displays the data for the first customer in the list (because the first row has focus).

  2. Select a different row in the master DataWindow.

    The detail DataWindow displays the data for the new row.

  3. Close the application.