Chris1973

Hi

My code

DataSet ds = new DataSet();

//Get the customers data

SqlCommand cmdCustomers = conn.CreateCommand();

cmdCustomers.CommandType = CommandType.Text;

cmdCustomers.CommandText = "Select * FROM Customers";

SqlDataAdapter daCustomers = new SqlDataAdapter();

daCustomers.SelectCommand = cmdCustomers;

daCustomers.Fill(ds, "Customers");

//Get the orders data

SqlCommand cmdOrders = conn.CreateCommand();

cmdOrders.CommandType = CommandType.Text;

cmdOrders.CommandText = "Select * FROM Orders";

SqlDataAdapter daOrders = new SqlDataAdapter();

daOrders.SelectCommand = cmdOrders;

daOrders.Fill(ds, "Orders");

//Get the order details data

SqlCommand cmdOrderDetails = conn.CreateCommand();

cmdOrderDetails.CommandType = CommandType.Text;

cmdOrderDetails.CommandText = "Select * FROM [Order Details]";

SqlDataAdapter daOrderDetails = new SqlDataAdapter();

daOrderDetails.SelectCommand = cmdCustomers;

daOrderDetails.Fill(ds, "[Order Details]");

//Add the relations

DataRelation drCustOrder = ds.Relations.Add("CustOrders",

ds.Tables["Customers"].Columns["CustomerID"],

ds.Tables["Orders"].Columns["CustomerID"]);

DataRelation drOrderDetails = ds.Relations.Add("OrderDetails",

ds.Tables["Orders"].Columns["OrderID"],

ds.Tables["Order Details"].Columns["OrderID"]);

//Show the data

masterDatagridView.DataSource = ds;

masterDatagridView.DataMember = "Customers";

geneates the following error for the second data relation, OrderDetails:

"Object reference not set to an instance of an object"

I would like to be able to show Customers, Orders, and Order details on a datagridview, allowing the user to drill down to the lowest level, by expanding the nodes on the grid

The code works fine if the second data relation is removed

Thanks

Chris1973



Re: Windows Forms Data Controls and Databinding Multiple tables in data relations

Zhi-Xin Ye - MSFT

Chris1973 wrote:

SqlDataAdapter daOrderDetails = new SqlDataAdapter();

daOrderDetails.SelectCommand = cmdCustomers;

daOrderDetails.Fill(ds, "[Order Details]");

DataRelation drOrderDetails = ds.Relations.Add("OrderDetails",

ds.Tables["Orders"].Columns["OrderID"],

ds.Tables["Order Details"].Columns["OrderID"]);



The table name you used are not the same, the preceding one has brackets, the other one not. That's the problem.