DataTable already belongs to another DataSet
Asked Answered
H

11

55

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))
Hyperion answered 12/1, 2012 at 6:15 Comment(0)
D
100

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()
Diagraph answered 12/1, 2012 at 6:30 Comment(3)
do you know why this is the case ? Meaning e.g. List<string> myl = new List<string>(); and then i add 3 items and i do this myl[0] = myl[1] and this works fine. I have already added the myl[1] to the list. Question is more generic on Datatable on why and how this check is done if the table is already belongs to DatasetHowlond
Copy has a lot of overhead... see Nathaniel Layton's answer below for a better solution.Frijol
This works but it shouldn't be the accepted answer...see Nathaniel Layton's answer.Irreverent
A
42

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);
Armada answered 20/8, 2013 at 22:33 Comment(3)
@Layton May I know what is the issue with Clone ?Hyperion
This should be the accepted answer... copy has a lot of overhead.Frijol
This is like a cut and paste. You remove it from one DataSet and then add it to another. Is there a way in which we can keep the same table in both datasets without cloning?Myrica
T
2

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)

Terrenceterrene answered 12/1, 2012 at 6:26 Comment(2)
Thanks abbas I have got it :)Hyperion
Clone will copy only the structure. .Copy() will copies schema as well as dataHyperion
S
0

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

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

Sunfast answered 12/1, 2012 at 6:16 Comment(3)
i am just adding one datatable of a dataset to another dataset. for this we need to use serializtion ?Hyperion
A DataTable can't reside in two DataSets. Maybe you can remove it from the first one and add it to the second. I've never tried it.Sunfast
dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0).Clone()) is worked for me Thanks for ur replyHyperion
P
0

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

Paradies answered 12/1, 2012 at 6:22 Comment(0)
S
0

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))

Skylight answered 12/1, 2012 at 6:30 Comment(0)
C
0

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.

Censor answered 18/9, 2015 at 0:38 Comment(1)
Sorry, myDataSet.Tables.Remove(myTable);//This works line code (in my code above) must be AFTER myDataSet.WriteXml("myTable.xml"); I confirm that the code works.Censor
I
0

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.

Inconsonant answered 17/11, 2015 at 14:47 Comment(0)
P
0

The simple way is just merge the table as follows.

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

Pleach answered 9/1, 2017 at 13:1 Comment(0)
I
0

dtCopy = dataTable.Copy()

ds.Tables.Add(dtCopy)

We can Optimize the code like :

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

Investiture answered 31/1, 2019 at 6:17 Comment(0)
D
0
 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);
Drusy answered 24/4 at 6:37 Comment(1)
Hi @Praveen appreciate your efforts. Your answer should be more readable if you could share the code in a more readable manner. It seems there are extra codes which may not be relevant to this question. Happy Programming with Stack Overflow.Hyperion

© 2022 - 2024 — McMap. All rights reserved.