kbvishnu
kbvishnu

Reputation: 15650

DataTable already belongs to another DataSet

This error is occuring while adding one datatable from a dataset to another ."DataTable already belongs to another DataSet."

dsformulaValues.Tables.Add(m_DataAccess.GetFormulaValues
(dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0))

Upvotes: 56

Views: 100173

Answers (11)

Praveen Prajapati
Praveen Prajapati

Reputation: 21

 DataSet ds = GetSpotQuery.PostOptimizedSpot(SelectedchannelId, SelectedDate, SU_selectedValues, User_ID);  DataSet dsConstraint = GetSpotQuery.ConstraintRules(SelectedchannelId, SelectedDate, User_ID);  //dsConstraint.Tables[0].TableName = "PostConstraintRules";

 DataTable[] dtArrays = new DataTable[ds.Tables.Count + dsConstraint.Tables.Count];

 ds.Tables.CopyTo(dtArrays, 0);  dsConstraint.Tables.CopyTo(dtArrays, ds.Tables.Count);

 dtArrays[0].TableName = "PostOptimizedSpot";  dtArrays[ds.Tables.Count].TableName = "ConstraintRules";

 ExportToExcel("OptimizedSpots", dtArrays);

Upvotes: 0

Amal Ps
Amal Ps

Reputation: 713

dtCopy = dataTable.Copy()

ds.Tables.Add(dtCopy)

We can Optimize the code like :

ds.Tables.Add(dataTable.Copy());

Upvotes: 0

Natarajan Ganapathi
Natarajan Ganapathi

Reputation: 595

The simple way is just merge the table as follows.

dsformulaValues.Merge(m_DataAccess.GetFormulaValues(dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0))

Upvotes: 0

Juan Garcia
Juan Garcia

Reputation: 1

I found one turn around I hope it can help

_DataTable.TableName = _TableName
If _DataTable.DataSet IsNot Nothing Then
    _DataSet = _DataTable.DataSet
Else
    _DataSet = New DataSet
    _DataSet.Tables.Add(_DataTable)
End If

Somehow even if _DataTable is new, but if it refers to a previous loaded physical table it becomes that DataTable inheriting the previous configuration.

Upvotes: 0

GMarco1989
GMarco1989

Reputation: 3

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

dsformulaValues.Tables.Add(m_DataAccess.GetFormulaValues
(dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0))

dsformulaValues.Tables.Remove(//I'm not sure to understand your code, so read this code line as only an input for your stuff. Please, consider my code below for more understanding.

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

Nathaniel Layton
Nathaniel Layton

Reputation: 421

The accepted answer isn't very good. Cloning should always be a last option.

Here's a way around the problem without incurring the overhead of cloning.

        DataSet ds = GetData1();

        DataSet ds2 = GetData2();

        //Assuming you know you've got good data

            DataTable dt = ds2.Tables[0];
            ds2.Tables.Remove(dt);
            dt.TableName = "PortedTable";//you may need to change the table name to prevent conflicts
            ds.Tables.Add(dt);

Upvotes: 42

Abbas
Abbas

Reputation: 6886

Try calling this method:

DataTable dt = dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0).Clone()

This will create a copy of the DataTable and assign it to the target DataSet:

ds.Tables.Add(dt)

Upvotes: 2

Jay Riggs
Jay Riggs

Reputation: 53603

Like the other responses point out, the error you're seeing is because the DataTable you're attempting to add to a DataSet is already a part of a different DataSet.

One solution is to Copy the DataTable and assign the copy to the other DataSet.

dtCopy = dataTable.Copy()
ds.Tables.Add(dtCopy)

The copied DataTable will have the structure and data of the copied DataTable.

If you only want the structure of the DataTable, call Clone instead.

dtCopy = dataTable.Clone()

Upvotes: 100

pistipanko
pistipanko

Reputation: 775

Try to copy the table with DataTable.Copy() method in case it's not a typed DataSet. This method creates a new instance of the same table so it not gonna belong to any DataSet:

dim newTable as DataTable = oldTable.Copy() dim dv as DataView = newTable.DefaultView

dsformulaValues.Tables.Add(m_DataAccess.GetFormulaValues(dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0))

Upvotes: 0

Gavin Fang
Gavin Fang

Reputation: 367

I think u should create a new DataTable and import the struct and data to the new one.

Upvotes: 0

zmbq
zmbq

Reputation: 39059

I guess this means the DataTable belongs to another DataSet...

You can serialze the DataTable to XML, then deserialize it into your target DataSet.

Upvotes: 0

Related Questions