Sorry about that Cor, here is the code straight from the help article.
Me.Validate()
Me.OrdersBindingSource.EndEdit()
Me.CustomersBindingSource.EndEdit()
Dim deletedOrders As NorthwindDataSet.OrdersDataTable = CType( _
NorthwindDataSet.Orders.GetChanges(Data.DataRowState.Deleted),
NorthwindDataSet.OrdersDataTable)
Dim newOrders As NorthwindDataSet.OrdersDataTable = CType( _
NorthwindDataSet.Orders.GetChanges(Data.DataRowState.Added),
NorthwindDataSet.OrdersDataTable)
Dim modifiedOrders As NorthwindDataSet.OrdersDataTable = CType( _
NorthwindDataSet.Orders.GetChanges(Data.DataRowState.Modified),
NorthwindDataSet.OrdersDataTable)
Try
' Remove all deleted orders from the Orders table.
If Not deletedOrders Is Nothing Then
OrdersTableAdapter.Update(deletedOrders)
End If
' Update the Customers table.
CustomersTableAdapter.Update(NorthwindDataSet.Customers)
' Add new orders to the Orders table.
If Not newOrders Is Nothing Then
OrdersTableAdapter.Update(newOrders)
End If
' Update all modified Orders.
If Not modifiedOrders Is Nothing Then
OrdersTableAdapter.Update(modifiedOrders)
End If
NorthwindDataSet.AcceptChanges()
Catch ex As Exception
MsgBox("Update failed")
Finally
If Not deletedOrders Is Nothing Then
deletedOrders.Dispose()
End If
If Not newOrders Is Nothing Then
newOrders.Dispose()
End If
If Not modifiedOrders Is Nothing Then
modifiedOrders.Dispose()
End If
End Try
The new Orders are added to the database, but not with the new Customer
record. They are added with a blank Customer record Id. If I add new
Orders to an existing Customer then the new Orders contain the correct
Customer Id.
I tried to fix it by adding two sections of code. I entered the
following code before the Try statement.
CurCus = NORTHWNDDataSet.Customers(CustomersBindingSource.Position) _
("CustomerId", DataRowVersion.Current).ToString
And then the following code after the CustomersTableAdapter.Update
statement.
'update new orders with the new customerid
For x = 0 To newOrders.Count - 1
newOrders.Rows(x)("customerid") = CurCus
Next
But the new Orders were still not saved correctly.