DataSet ERROR Either InvalidConstraintException or Cannot have a relationship between tables in different DataSets

StackOverflow https://stackoverflow.com/questions/19192466

Question

This problem will take a while to explain in detail so please bear with me. First the code:

    public DataModule(Main main)
    {
        frmMain = main;

        InitializeComponent();

        getWarehouseData();

        createSortTables();
    }


    /// <summary>method : getWarehouseData
    /// Loads data from database to local objects
    /// </summary>
    private void getWarehouseData()
    {
        WarehouseDataSet dsWarehouse = new WarehouseDataSet();

        WarehouseDataSetTableAdapters.LocationTableAdapter taLocation =
            new WarehouseDataSetTableAdapters.LocationTableAdapter();

        WarehouseDataSetTableAdapters.PickOrderTableAdapter taPickOrder =
            new WarehouseDataSetTableAdapters.PickOrderTableAdapter();

        WarehouseDataSetTableAdapters.ProductTableAdapter taProduct =
            new WarehouseDataSetTableAdapters.ProductTableAdapter();

        WarehouseDataSetTableAdapters.Prod_LocTableAdapter taProd_Loc =
        new WarehouseDataSetTableAdapters.Prod_LocTableAdapter();

        taLocation.Fill(dsWarehouse.LOCATION);
        taPickOrder.Fill(dsWarehouse.PICK_ORDER);
        taProduct.Fill(dsWarehouse.PRODUCT);
        taProd_Loc.Fill(dsWarehouse.PROD_LOC);

        dtLocation = dsWarehouse.Tables["LOCATION"];
        dtPickOrder = dsWarehouse.Tables["PICK_ORDER"];
        dtProduct = dsWarehouse.Tables["PRODUCT"];
        dtProd_Loc = dsWarehouse.Tables["PROD_LOC"];
    }

    private void createSortTables()
    {
        // Modified Location Table from View
        dvSortedLocations = new DataView(dtLocation);
        dvSortedLocations.Sort = "PickTime ASC";


        // Modified Product Table as view
        DataView dvSortedProducts = new DataView(dtProduct);
        dvSortedProducts.Sort = "NumOfPicks DESC";
        dtSortedProduct = dvSortedProducts.ToTable();


        // New table for optimised Product Locations
        dtOptProd_Loc = new DataTable("OPTPROD_LOC");
        dtOptProd_Loc.Columns.Add("ProductCode", typeof(string));
        dtOptProd_Loc.Columns.Add("Location", typeof(string));
        dtOptProd_Loc.Columns.Add("StockQtty", typeof(double));

        dsWarehouse.Tables.Add(dtOptProd_Loc);  
        DataColumn parentColumn = dsWarehouse.Tables["PRODUCT"].Columns["ProductCode"];
        DataColumn childColumn = dsWarehouse.Tables["OPTPROD_LOC"].Columns["ProductCode"]; 
        DataRelation REL_Prod_OptProdLoc = new DataRelation("PRODUCTOPTPROD_LOC", parentColumn, childColumn); 
        dsWarehouse.Relations.Add(REL_Prod_OptProdLoc); 

        // New table for Picking from optimised Locations
        dsWarehouse.Tables.Add(new DataTable("OPTPICK"));
        dtOptPick = dsWarehouse.Tables["OPTPICK"];

        dtOptPick.Columns.Add("PickID", typeof(int));
        dtOptPick.Columns.Add("ProductCode", typeof(string));
        dtOptPick.Columns.Add("Location", typeof(string));
        dtOptPick.Columns.Add("PickQtty", typeof(double));
        dtOptPick.Columns.Add("SaleNumber", typeof(double));

        DataColumn parentColumn2 = dsWarehouse.Tables["LOCATION"].Columns["LocationCode"];
        DataColumn childColumn2 = dsWarehouse.Tables["OPTPICK"].Columns["Location"];
        DataRelation REL_Loc_OptPick = new DataRelation("REL_Loc_OptPick", parentColumn2, childColumn2);
        dsWarehouse.Relations.Add(REL_Loc_OptPick);


        parentColumn = dsWarehouse.Tables["PRODUCT"].Columns["ProductCode"];
        childColumn = dtOptPick.Columns["ProductCode"];
        DataRelation REL_Prod_OptPick = new DataRelation("REL_Prod_OptPick", parentColumn, childColumn);
        dsWarehouse.Relations.Add(REL_Prod_OptPick);

        foreach (DataRow dr in dtPickOrder.Rows)
        {
            dtOptPick.Rows.Add(dr.ItemArray);
        }

Writing a WindowsForms GUI for an Access Database application. It takes the data, reorganises it into some new tables and analyses the result. Initial data loaded as dsWarehouse dataset as shown in getWarehouseData(), has 4 tables.

In createSortTables() 2 new tables are added to dsWarehouse and relationships to exisiting tables added. However with code as shown when the foreach loop at the bottom is reached to fill the new dtOptPick table the InvalidConstraintException is thrown saying that first "Location" reached is not present in the Parent Table (dtLocation). Inspection of dsWarehouse.Tables["LOCATION"] in the debugger shows 6 items in the list of non-public members as expected but looking in datatable visualisation none contains any data. However inspecting dtLocation shows 1371 datarows.

So I tried substituting dsWarehouse.Tables["LOCATION"] with dtLocation, but this throws the InvalidConstraintException at the new DataRelation line, saying "Cannot have a relationship between tables in different DataSets". So despite using dsWarehouse reference in all cases there seems to be 2 different actual datasets, the original which does hold all the data, and a new one when I try to add tables with no data.

I have tried to apply acceptChanges and Merge but not worked at least the way I applied them.

Anyone know why there seems to be 2 datasets with the same name? Or any solution at all would be brilliant!!

Was it helpful?

Solution

I will answer this myself, for the benefit of anyone else who gets this problem.

What had happened is that I had tried to go through the MSDN walkthrough for setting the database connections and table adapters (TA's) up. However I found these instructions to be more like finding your way through a complicated maze. So as it turns out I had both dragged a dataset in from the Toolbox and called it dsWarehouse and later declared the same programmatically as above, along with all the TA's.

I would have expected to get an error at that point about ambiguity of a variable name. But instead referencing dsWarehouse later gave the mentioned error message about "relationships between tables in different datasets".

Part of the walkthrough instructions state that all these declarations are necessary. If you don't drag from the toolbox that is true. I had, so deleting all these declarations started to fix this up. Most of the code of the for modifying/adding tables was also simplified since the variable names now did refer to the same objects as intended.

One other related tip, make sure you have a primary key set in any tables that you are going to write data to. I had neglected to do this on my new tables and kept getting the error that data could not be added because it did not comply with the foreign key constraints. I spent along time confirming that in fact there was no constraint problem in the data, but that lack of a primary key was causing the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top