OL922 Posted January 16, 2003 Posted January 16, 2003 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 Quote
OL922 Posted January 16, 2003 Author Posted January 16, 2003 The problem seems to be that the instance of the DataRelation object is not getting created.......? Quote
*Experts* Nerseus Posted January 16, 2003 *Experts* Posted January 16, 2003 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 Quote "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
OL922 Posted January 16, 2003 Author Posted January 16, 2003 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") Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.