Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

relationship between dataset tables

Status
Not open for further replies.

ludmann

Programmer
Apr 14, 2004
49
GB
I am trying to create a parent/child relationship within two related tables customer and cust_addresses. The tables are linked via customer.o_id = cust_addresses.instance_id

Here is the code to create the relationship between the tables within the dataset

//Fill the dataset's first table with the Customer table
...
string sqlQueryString = "SELECT * FROM ss_customer_t";
SqlConnection myConn = new SqlConnection(connString);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(sqlQueryString, myConn);
// Build the DataSet
ds = new DataSet();
adapter.Fill(ds, "Customer");


//Fill the dataset's second table with the Addresses table
...
string sqlQueryString2 = "SELECT * FROM ss_cust_address_t";
SqlConnection myConn2 = new SqlConnection(connString2);
SqlDataAdapter adapter2 = new SqlDataAdapter();
adapter2.SelectCommand = new SqlCommand(sqlQueryString2, myConn2);
adapter2.Fill(ds, "Addresses");

//Create the relationship between Customer and Addresses
drel = new DataRelation("CustomerAddresses", ds.Tables["Customer"].Columns["o_id"], ds.Tables["Addresses"].Columns["instance_id"]);

ds.Relations.Add(drel);


AND THIS IS HOW I AM TRYING TO ACCESS THE RELATIONSHIP, TO GET THE CHILD ROWS
DataRow[] ChildRows = ds.Tables["Customer"].Rows[rowCtr].GetChildRows("CustomerAddresses");

Unfortunately I keep getting the error message:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Even though there is addresses for each customer

Can anyone help? What am I doing wrong?

Marika
 
Marika,
Give this a try
Code:
DataRelation drel = new DataRelation ("CustomerAddresses",
ds.Tables["Customer"].Columns["o_id"], 
ds.Tables["Addresses"].Columns["instance_id"]);

ds.Relations.Add(drel)

good luck


The solution is simple, the problem is complex.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top