Master Detail

D

DC

The help article "Walkthrough: Saving Data to a Database (Multiple
Tables)"
in VB 2005 Express does not seem to work. If I add a new Master record
and a
new Detail record at the same time, the Detail record does not contain
the
new Master record id value. I have went through this article a couple
of
times but I can't get it to work. Anyone know how to do this?
 
C

Cor Ligthert [MVP]

DC,

If we know how we have to do something, we are not reading again the
walkthrough, you have the change to get an answer from another newbie (which
can be very correct).

Otherwise just show the code from the updating part that you have made.

Cor
 
D

DC

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top