2

using C#, .net framework 4.5, VS 2012

Try to create simple relation between tables in data set, but got System.NullReferenceException, as I can see on MSDN, it's mean occurs when you try to reference an object in your code that does not exist. But, think i create all required objects.

My code below:

    //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"];
    }

and here I got exception

    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);
    }

Why i got this Null reference Exception? What i miss?

EDIT

enter image description here

hbk
  • 9,872
  • 9
  • 82
  • 110
  • 3
    Why don't you set a breakpoint in `BuildTableRelationShip` and step through the code until you hit the null reference exception? You can mouseover variables to see their values and/or store "watch variables" in a `Watch` window – rliu Dec 01 '13 at 10:18
  • Do you have a connection string called `AutoLotSqlProvider` ? `ConfigurationManager.ConnectionStrings["AutoLotSqlProvider"]` – ta.speot.is Dec 01 '13 at 10:19
  • yes, i have cnStr - it's in config file – hbk Dec 01 '13 at 10:31
  • @roliu , i do, but i got whole sentence `DataRelation dr = new DataRelation("CustomersOrders", //name of relation myDS.Tables["Customers"].Columns["CustID"], //main columns myDS.Tables["Orders"].Columns["OrderID"]);` like an exception – hbk Dec 01 '13 at 10:32
  • and all of this not null... – hbk Dec 01 '13 at 10:33
  • @Kirill You might want to add the exact error message to the post (meaning copy and paste it). If `myDS.Tables["Customers"].Columns` and `myDS.Tables["Orders"].Columns` are both non-null when you set a breakpoint at that line then perhaps the `DataRelation` constructor is throwing the exception. – rliu Dec 01 '13 at 10:36
  • @roliu please see my edit, here http://stackoverflow.com/q/20274016/2012219 you can see structure of my database – hbk Dec 01 '13 at 10:39
  • 2
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 01 '13 at 10:42
  • @Kirill You cut off the stack trace which is what we're missing :) – rliu Dec 01 '13 at 10:43
  • 1
    @Kirill Sigh I forgot what the VS exception window looks like. I'm afraid to ask if `View Details` contains the stack exception because it'll probably be hidden elsewhere and just be another back-and-forth. I think the solution JohnSaunders presented is the most beneficial to you. If you understand what a `NullReferenceException` is then you should be on your way to solving this yourself. It might take some time, but it's worth knowing. – rliu Dec 01 '13 at 10:55
  • 1
    Your `fill` statements are on the entire `DataSet`. Try adding your three `DataTable`s to your `DataSet` and then fill those. – Leon Newswanger Dec 01 '13 at 11:02
  • like `sqlAInventory.Fill(myDS, "Customers");` ?- try -now this error dissapear - looks like data set was empty because tables not added to data set – hbk Dec 01 '13 at 11:14
  • Excellent. I'll post the solution as an answer. – Leon Newswanger Dec 01 '13 at 11:22

1 Answers1

2

You should call fill on individual DataTables rather than the whole DataSet.

sqlAInventory.Fill(myDS);
sqlAOrders.Fill(myDS);
sqlACustomers.Fill(myDS);

would become

sqlAInventory.Fill(myDS, "Inventory");
sqlAOrders.Fill(myDS, "Orders");
sqlACustomers.Fill(myDS, "Customers");

This method will automatically add a table to your DataSet if it doesn't exist, and populates it with data if it does. MSDN has more information on this method of the fill.

Leon Newswanger
  • 607
  • 1
  • 5
  • 17