gemini6609
gemini6609

Reputation: 332

Error: Datatable named "Items" already belongs to this dataset

Why am I receiving the error Datatable named "Items" already belongs to this dataset in the code below? The error is only occurring when I am attempting to add values where if ((x > 1) || (x == 1)). I can successfully create multiple new "Item" datatables and then add them to the ds dataset in the while loop, however trying to more than one "AItem" datatable to AllItems dataset causes the error. The only difference between these datasets is that the ds dataset has many other datatables added to it which have data adapters filling them from a SQL db.

DataSet AllItems = new DataSet("Items");
DataSet ds = new DataSet("Header");

foreach (DataRow fieldRow in myDataset.Tables["tempTable"].AsEnumerable())
{
    while (x < 1)
    {
        x++;
        DataTable Item = new DataTable("Item");

        Item.Columns.Add("ID");
        Item.Columns.Add("LineNumber");
        Item.Columns.Add("ItemID");
        Item.Columns.Add("UnitPrice");
        Item.Columns.Add("Description");
        Item.Columns.Add("OrderUOM");
        Item.Columns.Add("OrderQty");

        DataRow aItem = Item.NewRow();
        Item["ID"] = 1.ToString();

        Item["LineNumber"] = x;
        Item["ItemID"] = fieldRow[0].ToString();
        Item["UnitPrice"] = fieldRow[1].ToString();
        Item["Description"] = fieldRow[2].ToString();
        Item["OrderUOM"] = fieldRow[3].ToString();
        Item["OrderQty"] = fieldRow[4].ToString();
        Item.Rows.Add(aItem);

        ds.Tables.Add(Item);
    }



    if ((x > 1) || (x == 1))
    {
        DataTable AItem = new DataTable("AItem");
        Item.Columns.Add("ID");
        Item.Columns.Add("LineNumber");
        Item.Columns.Add("ItemID");
        Item.Columns.Add("UnitPrice");
        Item.Columns.Add("Description");
        Item.Columns.Add("OrderUOM");
        Item.Columns.Add("OrderQty");

        DataRow aItem = Item.NewRow();
        Item["ID"] = 1.ToString();

        Item["LineNumber"] = x;
        Item["ItemID"] = fieldRow[0].ToString();
        Item["UnitPrice"] = fieldRow[1].ToString();
        Item["Description"] = fieldRow[2].ToString();
        Item["OrderUOM"] = fieldRow[3].ToString();
        Item["OrderQty"] = fieldRow[4].ToString();
        Item.Rows.Add(aItem);

        AllItems.Tables.Add(AItem);
    }
}

Upvotes: 0

Views: 1818

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460108

I'm not sure if this is your only problem but you are using the DataTable as if it was a DataRow. Item is the table, it has no string-indexer, so replace

DataRow aItem = Item.NewRow();
Item["ID"] = 1.ToString();    
Item["LineNumber"] = x;
Item["ItemID"] = fieldRow[0].ToString();
Item["UnitPrice"] = fieldRow[1].ToString();
Item["Description"] = fieldRow[2].ToString();
Item["OrderUOM"] = fieldRow[3].ToString();
Item["OrderQty"] = fieldRow[4].ToString();
Item.Rows.Add(aItem);

with

DataRow aItem = Item.NewRow();
aItem["ID"] = 1.ToString();    
aItem["LineNumber"] = x;
aItem["ItemID"] = fieldRow[0].ToString();
aItem["UnitPrice"] = fieldRow[1].ToString();
aItem["Description"] = fieldRow[2].ToString();
aItem["OrderUOM"] = fieldRow[3].ToString();
aItem["OrderQty"] = fieldRow[4].ToString();
Item.Rows.Add(aItem);

But i assume that this is just the next error, "Datatable named “Items” already belongs to this dataset" is thrown because you are adding a table to a DataSet with the same name of another table. You are adding tables with the static name "AItem" in the loop here:

foreach (DataRow fieldRow in myDataset.Tables["tempTable"].AsEnumerable())
{
    // ...
    DataTable AItem = new DataTable("AItem");
    // ...
    AllItems.Tables.Add(AItem);
    // ...
}

I don't understand the logic, maybe it's sufficient to use the default constructor without a name.

Upvotes: 2

Related Questions