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

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

delItemsDS.Tables("deleteItems").Rows.Add(prodDS.Tables(0).Rows(i))
LVL 2
ethnarchAsked:
Who is Participating?
 
rodmjayCommented:
try the importrow method instead of rows.add
0
 
ethnarchAuthor Commented:
how do i use that could you give me an example?
0
 
rubixxcubeCommented:
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 :

delItemsDS.Tables("deleteItems").Rows.Add(prodDS.Tables(0).Rows(i))

you should change it to the following:

delItemsDS.Tables("deleteItems").ImportRow(prodDS.Tables(0).Rows(i))

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.
Ruby
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
ethnarchAuthor Commented:
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?
0
 
rubixxcubeCommented:
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.
0
 
ethnarchAuthor Commented:
i see thanks for the explanation rubix
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.