hjdoran 0 Newbie Poster

Hi everyone,

I've got an interesting problem that has me stumped and I just cannot seem to find the right answer in the help files.

Background: My project requires that I establish more than a few relationships amongst datatables in a dataset so I decided to create a generic routine that would create the table relationships that I could call on as needed. All the relationship require multiple datacolumns so I pass arrays of datacolumns for the parent and child tables.

Problem: My problem stems from trying to populate an array of datacolumns. I pass in a list of fields to be used in the datatable relation, parse out each field name into an array, create a datacolumn object, iterate through the list of fields and try to copy the datacolumn from the parent/child table to the appropriate datacolumn array. First time through the loop it works fine, but on the second trip through the loop it is unable to find the datacolumn in the parent/child table and it assigns nothing to the array element.

I've stared at this code for too long and I know I'm missing something, but I cannot seem to find the answer so any help would be greatly appreciated.

cheers

Private Sub CreateRelation(ByRef dsDataset As DataSet, ByVal strParentTable As String, ByVal strChildTable As String, _
                               ByVal strParentFields As String, ByVal strChildFields As String, ByVal strRelationName As String)
        'Purpose is to create a generic sub routine that will join tables and return a relation or add the relation to 
        'the referenced dataset. 
        'Pre: tables must exist in the referenced dataset and must contain the listed field to establish the relation on
        'Post: adds relation to referenced dataset

        'parse out field lists
        Dim strParentFieldsArray As String() = strParentFields.Split(",")
        Dim strChildFieldsArray As String() = strChildFields.Split(",")
        Dim intI As Integer     'loop counter
        Dim intSize As Integer = strParentFieldsArray.Length - 1
        Dim dcParentColumnsArray As DataColumn()
        Dim dcChildColumnsArray As DataColumn()
        ReDim dcParentColumnsArray(intSize)
        ReDim dcChildColumnsArray(intSize)
        Dim dtParent As DataTable = dsDataset.Tables(strParentTable)
        Dim dtChild As DataTable = dsDataset.Tables(strChildTable)

        For intI = 0 To intSize
            'get the datcolumns from the data tables
            Dim dcParent As DataColumn = dtParent.Columns(strParentFieldsArray(intI).ToString)
            dcParentColumnsArray(intI) = dcParent
            Dim dcChild As DataColumn = dtChild.Columns(strChildFieldsArray(intI).ToString)
            dcChildColumnsArray(intI) = dcChild
        Next

        'establish the relationship based on the columns collections
        Dim relation As DataRelation = New DataRelation(strRelationName, dcParentColumnsArray, dcChildColumnsArray)
        dsDataset.Relations.Add(relation)

    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.