I am getting this error "This row already belongs to another table."

Posted on 2005-05-13
Medium Priority
Last Modified: 2012-06-27
I have a database that is used for a retail store, and there are a ton of items in this database that are not sold anymore. So i am trying to compare this table to the sales invoices and pick out barcode numbers that don't appear in the invoice from the last say 5 or so months.  I figured out how to do all of that, but i decided that i can't just delete these items i will have to store them so that they can be looked at later and then deleted.  I chose to just use an access file so that i could copy it to a disk and bring it to different locations etc. The original Database is an sql server Microsoft to be precise.

And to just cut to the chase i keep getting this error

An unhandled exception of type 'System.ArgumentException' occurred in system.data.dll
Additional information: This row already belongs to another table.

on this line of code(which is the set of items i am choosing to place in the access database(aka to be deleted))

Question by:ethnarch
  • 3
  • 2

Accepted Solution

rodmjay earned 500 total points
ID: 13998426
try the importrow method instead of rows.add

Author Comment

ID: 13998442
how do i use that could you give me an example?

Assisted Solution

rubixxcube earned 400 total points
ID: 13998768
Here is what he is referring to for the importrow.  There is a method of the datatable object called importrow that takes a row object and adds it into the current table.  
For example, Your current line of code is :


you should change it to the following:


Your original code is trying to take a row that is in another table and add it to a different table, which is not allowed, ImportRow makes a copy of the passed in row and adds the copy to the table instead of trying to add the original row.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13999029
Import row works fine i actually figured it out on my own but thanks rubixx for the help anyway.

I do have a question for my own knowledge and understanding though, and that is So when you add a row from one database to another does it not allow this because it deletes it from it's original location?

Does anybody know what's going on behind the scenes here?

Expert Comment

ID: 13999500
Here is what i think is the reasone behind this.  DataRow objects have a table Property.  The table property is marked as read only publicly.  It seems to me that when a datarow is added to a table internally the table property of the DataRow is set and so when you try to add this row to another table since this proeprty already has a value set it throws the exception you recieve.

Microsoftto handle this suppied the ImportRow method which makes a copy of the row.  It seems to me to make sense, or else you can have a row belong to infinite tables and keeping track of what rows belong to each table becomes harder behind the scenes.  If the row is modified/updated/deleted in one table something has to be done in the other tables now as well.  Sounds much more complicated for multiple tables to share a row and track everything then to simply force users to make a copy of the row.

Author Comment

ID: 14000166
i see thanks for the explanation rubix

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question