
Modifying the Generated Save Code to Perform the Hierarchical Update
Save changes from the related data tables in the dataset to the database by calling the TableAdapterManager.UpdateAll method and passing in the name of the dataset that contains the related tables. For example, run the TableAdapterManager.UpdateAll(NorthwindDataset) method to send updates from all the tables in NorthwindDataset to the back-end database.
After you drop the items from the Data Sources window, code is automatically added to the Form_Load event to populate each table (the TableAdapter.Fill methods). Code is also added to the Save button click event of the BindingNavigator to save data from the dataset back to the database (the TableAdapterManager.UpdateAll method).
The generated save code also contains a line of code that calls the CustomersBindingSource.EndEdit method. More specifically, it calls the EndEdit method of the first BindingSource added to the form. In other words, this code is only generated for the first table dragged from the Data Sources window onto the form. The EndEdit call commits any changes that are in process in any data-bound controls that are currently being edited. Therefore, if a data-bound control still has focus and you click the Save button, all pending edits in that control are committed before the actual save (the TableAdapterManager.UpdateAll method).
Note: |
|---|
The designer only adds the
BindingSource.EndEdit code for the first table dropped onto the form. Therefore, you have to add a line of code to call the BindingSource.EndEdit method for each related table on the form. For this walkthrough, this means you have to add a call to the OrdersBindingSource.EndEdit method.
|
To update the code to commit changes to the related tables before saving
Double-click the Save button on the BindingNavigator to open Form1 in the Code Editor.
Add a line of code to call the OrdersBindingSource.EndEdit method after the line that calls the CustomersBindingSource.EndEdit method. The code in the Save button click event should resemble the following:
Me.Validate()
Me.CustomersBindingSource.EndEdit()
Me.OrdersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.NorthwindDataSet)
this.Validate();
this.customersBindingSource.EndEdit();
this.ordersBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.northwindDataSet);
In addition to committing changes on a related child table before saving data to a database, you might also have to commit newly created parent records before adding new child records to a dataset. In other words, you might have to add the new parent record (Customer) to the dataset before foreign key constraints enable new child records (Orders) to be added to the dataset. To accomplish this, you can use the child BindingSource.AddingNew event.
Note: |
|---|
You may or may not have to commit new parent records; it depends on the type of control that is used to bind to your data source. In this walkthrough, you use individual controls to bind to the parent table; this requires the additional code to commit the new parent record. If the parent records were displayed in a complex binding control like the
DataGridView, this additional EndEdit call for the parent record would not be necessary. This is because the underlying data-binding functionality of the control handles the committing of the new records.
|
To add code to commit parent records in the dataset before adding new child records
Create an event handler for the OrdersBindingSource.AddingNew event.
Open Form1 in design view, click OrdersBindingSource in the component tray, select Events in the Properties window, and then double-click the AddingNew event.
Add to the event handler a line of code that calls the CustomersBindingSource.EndEdit method. The code in the OrdersBindingSource_AddingNew event handler should resemble the following:
Me.CustomersBindingSource.EndEdit()
this.customersBindingSource.EndEdit();