Note that there are some explanatory texts on larger screens.

plurals
  1. POExceptions: System.NullReferenceException while trying to create relation between tables in dataSet
    primarykey
    data
    text
    <p>using C#, .net framework 4.5, VS 2012</p> <p>Try to create simple relation between tables in data set, but got System.NullReferenceException, as I can see on MSDN, it's mean <code>occurs when you try to reference an object in your code that does not exist</code>. But, think i create all required objects.</p> <p>My code below:</p> <pre><code> //create place for storing all tables from data base private DataSet myDS = new DataSet("AutoLot"); //command builders for easy way access to tables private SqlCommandBuilder sqlCInventory; private SqlCommandBuilder sqlCOrders; private SqlCommandBuilder sqlCCustomers; //adapters for each table private SqlDataAdapter sqlAInventory; private SqlDataAdapter sqlAOrders; private SqlDataAdapter sqlACustomers; //connection string private string cnStr = string.Empty; public MainForm() { InitializeComponent(); //get connection string from .config file cnStr = ConfigurationManager.ConnectionStrings["AutoLotSqlProvider"].ConnectionString; //create adapters sqlACustomers = new SqlDataAdapter("Select * From Customers", cnStr); sqlAInventory = new SqlDataAdapter("Select * From Inventory", cnStr); sqlAOrders = new SqlDataAdapter("Select * From Orders", cnStr); //automatic generate commands sqlCCustomers = new SqlCommandBuilder(sqlACustomers); sqlCInventory = new SqlCommandBuilder(sqlAInventory); sqlCOrders = new SqlCommandBuilder(sqlAOrders); //add table to data Set sqlAInventory.Fill(myDS); sqlAOrders.Fill(myDS); sqlACustomers.Fill(myDS); //create relationship between tables BuildTableRelationShip(); //create DataSourse for datGrids on UI dataGridViewCustomer.DataSource = myDS.Tables["Inventory"]; dataGridViewOrders.DataSource = myDS.Tables["Orders"]; dataGridViewCustomer.DataSource = myDS.Tables["Customers"]; } </code></pre> <p>and here I got exception</p> <pre><code> private void BuildTableRelationShip() { //create object of relationShips DataRelation dr = new DataRelation("CustomersOrders", //name of relation myDS.Tables["Customers"].Columns["CustID"], //main columns myDS.Tables["Orders"].Columns["OrderID"]); //related columns myDS.Relations.Add(dr); //second relation dr = new DataRelation("InventoryOrder", myDS.Tables["Inventory"].Columns["CarID"], myDS.Tables["Orders"].Columns["OrderID"]); //add relations to dataset myDS.Relations.Add(dr); } </code></pre> <p>Why i got this Null reference Exception? What i miss?</p> <p><strong>EDIT</strong></p> <p><img src="https://i.stack.imgur.com/YKRs1.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload