Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm 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"

 

Here's the code

 

can someone help

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

  • *Experts*
Posted

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 shouldn't be case sensitive, but maybe the spelling is off...

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

My mistake had to do with the fill method. If I don't 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

daCustomer.Fill(dsCustomerOrder, "Customer")
daOrders.Fill(dsCustomerOrder, "Order")

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...