Quick tip: Troubleshoot “This row already belongs to another table” error message

If you’re working with Data Tables you might have tried to copy one table’s rows to another. However, using the .Copy method will result in this error message:

This row already belongs to another table.

So, how do you deal with that?

It’s not that hard really.

What you first need to make sure of is that the two Data Tables are identical in structure. In order to get the structure of a Data Table and apply it to another one without fetching the data rows as well, you will have to use the .Clone method instead of the .Copy method.  So, to create two identical Data Tables use the following:

DataTable dtAlphaClone = dtAlpha.Clone();

The above line will create a Data Table called dtAlphaClone which will be identical, in structure, to the dtAphla Data Table. There will be no data in the new Data Table. All the Data Columns will be there however. You can use the new Data Table to store rows from the old dtAlpha Data Table.

In order to pass rows from one table to another, you can’t use this: dtAlphaClone.Rows.Add(x);

Do the above and you will end up with the error message we’re solving here. Instead, use the Import method to import the Data Row from the original Data Table to the new clone.

dtDocumentsClone.ImportRow(x);

This will successfully import the correct Data Row to your new clone Data Table.

PS: it goes without saying that ‘x’ is a Data Row. In order for the above to work, you will probably need be in a foreach loop, running on the original Data Table, checking for some conditions and when they succeed, the Data Row will be imported to the new Data Table. (like so: foreach (DataRow x in dtAlpha.Rows))

MGR: the Intelogist

About MGR: the Intelogist

SharePoint Server developer, turned Sitefinity developer, turned Angular developer, turned SharePoint Online consultant, turned Unily consultant, turned O365 consultant... Never a dull moment!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.