This row already belongs to another table


Occurs when you copy a DataRow from one DataTable to another throwing an ArgumentException

The error occurs because the same datarow object could not be added to different datatables.The best solution is using
the DataTable.ImportRow method which creates an exact copy of the row and adds it to the other table. However, in order to use this function
, both tables should have the same schema which is achieved using a method call to DataTable.Clone() to copy shema only or DataTable.Copy() to copy both data and schema. The code below shows how to do this:

Dim dt As New DataTable

'dt = FillRowFromDataBase

Dim dtClone As DataTable = dt.Clone

For Each row As DataRow In dt.Rows

dtClone.ImportRow(row)Next

3 Responses to This row already belongs to another table

  1. haiduong87 says:

    Hi Ali Tarhini,
    Can you tell me why “same datarow object could not be added to different datatables”? This is because of .Net Framework or something else?
    Thanks alot

  2. Ali Tarhini says:

    this is by design, a datarow cannot be in two places at the same time because of update conflicts

  3. haiduong87 says:

    Ah I see, thank you

Leave a Reply

Please log in using one of these methods to post your comment:

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: