none
Adding data to related datatables RRS feed

  • Question

  • I am having problem in creating  2 datatables  with parent-child relationship. They are tSections and tStatements.

    They relationship has been defined in the designer, with foreign key constraint on the sectionID columns in both tables. I need to fill these tables with data from 2 similar tables from a web service.

     

    The tSections table is generated as expected. But there's a problem in the child table tStatement. I think the problem has to do with the  sectionID column in tStatement.

     

    Below are to procedures I used to create the datables. I call the AddSections sub, which in turn will call the AddStatement sub.

     

    Any help is much appreciated.

    Code Snippet

     

    Private wRow As ServiceReference1.tiaraDS.SectionsRow

    Private wTable As ServiceReference1.tiaraDS.SectionsDataTable

     

    Private Sub AddSections()

     

    Dim sectRow As auditDS.tSectionsRow

    Dim originalSectionID As Integer

    wTable = wssDS.Sections

     

    For Each wRow In wTable        '

     

    sectRow = sectionTbl.NewtSectionsRow

    sectRow.auditID = auditID

    sectRow.secTionText = wRow.sectionText

    originalSectionID = wRow.sectionID

    sectionTbl.AddtSectionsRow(sectRow)

    AddStatements(sectRow, originalSectionID)

    Next

     

     End Sub

     

     

    Private Sub AddStatements(ByRef sectRow As auditDS.tSectionsRow, ByVal originalSectionID As Integer)

     

    Dim statRow As auditDS.tStatementsRow

     

    wTable = wssDS.statements

     

     

    For Each wRow In wTable

    If CheckRequiredStatements(wRow) Then

    If wRow.sectionID = originalSectionID Then

    statRow = stateTbl.NewtStatementsRow

    statRow.SetParentRow(sectRow) '  note - use this statement to specify parent row

     

    statRow.auditID = auditID

    statRow.StatementText = wRow.statementText

     

     stateTbl.Rows.Add(statRow)

     

    End If

    End If

     

    Next

    End sub

     

     

     

    Thursday, October 23, 2008 9:25 PM

Answers

  • Relationships between DataTable are based on value matching.

     

    The DataRow.SetParentRow(DataRow) method will set the values in the child row for every DataColumn from every childRow.Table.ParentRelations where the parentRow table is the parent table of the relationship.

     

    If you have a specific relationship you want to use, use the DataRow.SetParent(DataRow, DataRelationship) overload.

     

    From your code example, if sectRow.auditID was the parent value of the relationship and statRow.auditID was the child value of the relationship then calling statRow.SetParentRow(sectRow) may actually be unnecessary.

     

     

     

    Friday, October 24, 2008 4:56 PM
    Moderator