After you modify and validate the data in the dataset, you may need to send the updated data back to the database. To send the modified data to the database, you need to call the Update method. The update method of this adapter updates a single data table and executes the correct life (INSERT, Update, or DELETE) based on the RowState of each data row in the table.
Call the adapter's Update method in the try/catch block.
If an exception is caught, the data row that raised the error is found.
Reconcile the problems in the data rows (either programmatically, if possible, or display the invalid rows to the user for modification), and then try the update again.
Try me.validate () Me.CustomersBindingSource.EndEdit () Me.CustomersTableAdapter.Update ( Me.NorthwindDataSet.Customers) MsgBox ("Update successful") Catch ex as Exception MsgBox ("Update failed") End Try
Update two related tables in a dataset using TableAdapter
When updating related tables in a dataset, it is important to update them in the correct order to reduce the likelihood of violating referential integrity constraints. The order in which the commands are executed will also follow the order of the indexes of the DataRowCollection in the dataset. To prevent data integrity errors from being raised, it is a best practice to update the database in the following order:
Child table: Delete records.
Parent table: Inserting, updating, and deleting records.
Child tables: inserting and updating records.
- Update two related tables using TableAdapter
-
Create three temporary data tables to hold different records.
Call the Update method from the try/catch block for each child rowset. If an update error occurs, you should split the expense and resolve the errors.
Commits the changes to the database.
Dispose of temporary data tables to release resources.
The following example shows how to update a data source with a dataset that contains related tables.
Private Sub updatedb () Dim deletedchildrecords as Northwinddataset.ordersdatatable = _ CType (northwinddataset.or DERs. GetChanges (Data.DataRowState.Deleted), northwinddataset.ordersdatatable) Dim Newchildrecords as Northwinddataset.ordersdatatable = _ CType (NorthwindDataSet.Orders.GetChanges (Data.DataRowState.Added), northwindd ataset.ordersdatatable) Dim Modifiedchildrecords as Northwinddataset.ordersdatatable = _ CType (NorthwindDataSet. Orders.getchanges (Data.DataRowState.Modified), northwinddataset.ordersdatatable) Try If deletedchildrecords IsNo T Nothing Then orderstableadapter.update (deletedchildrecords) End If customerstableadapter.update (northwinddataset.customers) If newchildrecords IsNot Nothing Then Orderstableadapter.update (Newchildrec Ords) End If if modifiedchildrecords IsNot Nothing Then Orderstableadapter.update (modifiedchildre Cords) End If northwinddataseT.acceptchanges () Catch ex as Exception MessageBox.Show ("An error occurred during the update process") ' A DD code to handle error here. Finally if deletedchildrecords IsNot Nothing Then Deletedchildrecords.dispose () End if If Newchildrecords IsNot Nothing Then Newchildrecords.dispose () End if Modifiedchildrecords IsNot Nothing Then Modifiedchildrecords.dispose () End If End Tryend Sub
Update two related tables in a dataset using TableAdapter