DataTable error – “This row already belongs to another table”

I was attempting to add a DataRow from an existing DataTable into a new DataTable. 

                            Dim TempResultsFile As New DataSet
                            TempResultsFile.ReadXml(“C:\MyFile.xml”)

                           For Each Row As DataRow In TempResultsFile.Tables(0).Rows
                                   ‘Snip excess code
                                    OeaaResults.Rows.Add(Row)
                            Next

At the OeaaResults.Rows.Add(Row) line, I received the message “This row already belongs to another table”. Well duh, of course it belongs to another table. What’s your point?

But that kind of brilliant arguing did not solve my problem. Instead, I found http://www.geekzilla.co.uk/View37EB5230-5B79-4D00-800C-52D7A46CFB15.htm which did solve my problem. In the article, Paul says to use the DataTable.Import method instead of DataTable.Add. So I changed

OeaaResults.Rows.Add(Row)

to

OeaaResults.ImportRow(Row)

and the error was resolved. Joy and happiness abound.

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: