Question

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

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top