Home All Groups Group Topic Archive Search About

Master-Detail Relationship

Author
23 Feb 2006 12:57 PM
Good
Hi all

I have created a master-detail relationship dataset under VS2005. However,
when I update the tables, it seems the foreign key information couldn't be
passed from the master table, therefore, the update is failed. Do you know
how to solve this problem?

Cheers
Good

Author
1 Apr 2006 8:03 AM
mikeinthemud
While updating related tables in a dataset, it is important to update in the proper sequence to reduce the chance of violating the relation between tables. To prevent data integrity errors update the data source in the following sequence:

Child table: delete records.
Parent table: insert, update, and delete records.
Child table: insert and update records.

Private Sub UpdateDB()
    Dim DeletedChildRecords As DataTable = _
        DS.Orders.GetChanges(DataRowState.Deleted)
    Dim NewChildRecords As DataTable = _
        DS.Orders.GetChanges(DataRowState.Added)
    Dim ModifiedChildRecords As DataTable = _
        DS.Orders.GetChanges(DataRowState.Modified)
    Try
        If Not DeletedChildRecords Is Nothing Then
            DAOrders.Update(DeletedChildRecords)
            DeletedChildRecords.Dispose()
        End If
        DACustomer.Update(DS, "Customer")
        If Not NewChildRecords Is Nothing Then
            DAOrders.Update(NewChildRecords)
            NewChildRecords.Dispose()
        End If
        If Not ModifiedChildRecords Is Nothing Then
            DAOrders.Update(ModifiedChildRecords)
            ModifiedChildRecords.Dispose()
        End If
        DS.AcceptChanges()
    Catch ex As Exception
        ' Update error, resolve and try again
    End Try
End Sub

As an alternative download IdeaBlade’s DevForce Express and use that for ORM (I don’t work for them – but use it extensively)