DataRelation question

OL922

Member
Joined
Dec 3, 2002
Messages
5
Location
Montreal
Im using two dataadapters to bring in 2 tables into a dataset and then creating a one to many relationship but I keep on getting this error

"Object Reference not set to an instance of the object"

Heres the code

can someone help
Code:
 Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\My Documents\Access Database\SimpleDataAccessExample.mdb;" 
            Connection object
            Dim mdbConn As OleDbConnection = New OleDbConnection(strConn)

            DataAdapter for the customer table
            Dim daCustomer As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Customer", mdbConn) where City=" & txtSearch.Text, mdbConn)

             DataAdapter for the orders table
            Dim daOrders As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Orders", mdbConn)

            ----------------------------------------------------------
            opening and closing
            mdbConn.Open()

            Create DataSet
            Dim dsCustomerOrder As New DataSet()

            Fill DataSet
            daCustomer.Fill(dsCustomerOrder)
            daOrders.Fill(dsCustomerOrder)

            Close the connection
            mdbConn.Close()
            ----------------------------------------------------------

            The tables are now loaded into the dataset and the 
            relationship between the Customer and Orders table can be added
            Dim CustOrderRel As DataRelation = dsCustomerOrder.Relations.Add("CustOrder", _
            dsCustomerOrder.Tables("Customer").Columns("CustomerID"), _
            dsCustomerOrder.Tables("Orders").Columns("CustomerID"))

            Dim pRow, cRow As DataRow

            For Each pRow In dsCustomerOrder.Tables("Customer").Rows
                Console.WriteLine(pRow("CustomerID").ToString())
            Next

            For Each cRow In pRow.GetChildRows(CustOrderRel)
                Console.WriteLine(vbTab & cRow("OrderId").ToString())
            Next

        Catch ex As Exception
            MsgBox(ex.Message & " ErrorSource: " & ex.Source & "  Stacktrace: " & ex.StackTrace)
        End Try
 
Are you getting the error on the line that sets up CustOrderRel, your DataRelation? You seem to be doing it correctly so maybe the table and/or column name is wrong? It shouldnt be case sensitive, but maybe the spelling is off...

-nerseus
 
My mistake had to do with the fill method. If I dont use the overloaded version that takes in the name of the table that I specify as the second argument, then a table with the name "Table" is created when I use the fill method with just the dataset as the parameter. So i was referring to the wrong tables names when I was trying to instantiate the datarelation object.

So this works fine
Code:
 daCustomer.Fill(dsCustomerOrder, "Customer")
 daOrders.Fill(dsCustomerOrder, "Order")
 
Back
Top