dmr
dmr

Reputation: 22333

"DataTable already belongs to another DataSet"

I can't figure out why I am getting a "DataTable already belongs to another DataSet" exception.

Here is my code:

DataSet AlertSet = new DataSet();

DataTable generalAlertData = new DataTable("GeneralAlertData");
generalAlertData = //function that returns datatable

//throws exception
AlertSet.Tables.Add(generalAlertData)

When is another dataset being created and when is the generalAlertData datatable being added to it?

I tried using a slightly different syntax to create and add the table and got the same error:

DataSet AlertSet = new DataSet();

DataTable generalAlertData = //function that returns datatable

//throws exception
AlertSet .Tables.Add(generalAlertData);

Upvotes: 5

Views: 25994

Answers (4)

GMarco1989
GMarco1989

Reputation: 3

I had the same problem and I solved it using Remove. In my opinion, your code could be this:

DataSet AlertSet = new DataSet();

DataTable generalAlertData = new DataTable("GeneralAlertData");

AlertSet.Tables.Add(generalAlertData);
AlertSet.Tables.Remove(generalAlertData);

Please, let me know if this code works.

My working code was like this:

DataTable myTable = new DataTable();

private void Save()
{
    DataSet myDataSet = new DataSet();
    myDataSet.Tables.Add(myTable);
    myDataSet.Tables.Remove(myTable);//This works
    myDataSet.WriteXml("myTable.xml");
}

private void buttonSave_Click(object sender, EventArgs e)
        {
          Save();
        }

Every time I clicked the button buttonSave, the message “DataTable already belongs to another DataSet" appeared. After writing the line code myDataSet.Tables.Remove(myTable);//This works the application started running without problems and now I can click the button more times, without losing the value of myTable and without the error message.

I hope this can help.

Upvotes: 0

TWood
TWood

Reputation: 2583

This is an old question now but maybe this answer will help others.

I too received this error. I looked through my source code, found literally no other datasets and was puzzled as to why this was happening.

The solution to my issue was in my data access library. It didn't have a "getDataTable" routine but I did have a getDataset routine. When I needed just a datatable I called getDataset and took the tables(0) datatable instance.

The end result of this is that I got back my datatable like I wanted, but the datatable was already part of the dataset returned from my data access call. Thus, the error.

I had to add a getDatatable routine into my data access library to eliminate this problem.

Upvotes: 0

Steve
Steve

Reputation: 216290

It depends on what your function that retrieve the datatable does.
If that function use a DataAdapter and fills a dataset then you have the DataSet property of the table automatically assigned and you need to remove it before assigning a different DataSet

You could try this to remove the original DataSet and use your own

    DataTable generalAlertData = GetTable();
    DataSet u = generalAlertData.DataSet;
    u.Tables.Remove(generalAlertData.TableName);
    AlertSet.Tables.Add(generalAlertData);

So, if your hypothetical GetTable works in this way

public DataTable GetTable()
{
    ...
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    return ds.Tables[0];
}

then you have the DataSet assigned to the returned datatable.
Instead this code doesn't assign anything to the DataSet property

public DataTable GetTable()
{
    ...
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    return dt;
}

Upvotes: 8

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

The DataTable returned from the method call, was added to a DataSet. Make sure it's not added to a DataSet; just build a stand alone DataTable.

As a side-note, returning a DataTable from a method call that assigns to this one makes the line above it irrelevant (the new).

Upvotes: 2

Related Questions