Reputation:
I have a problem with my asp.net website, I am trying to copy a data row from one data table to another data table, but every time I am trying to do this i am getting an error:
This row already belongs to another table.
Upvotes: 16
Views: 37403
Reputation: 2422
For Example
You need to create a new Row
with the values from dr
first. A DataRow
can only belong to a single DataTable
.
You can also use Add which takes an array of values:
myTable.Rows.Add(dr.ItemArray)
Or probably even better:
myTable.ImportRow(dr);
Upvotes: 4
Reputation: 887433
As the error states, a DataRow
instance is tied to its owning DataTable
and cannot be added to another table.
Instead, use the ImportRow()
method to make an actual copy of the row.
Upvotes: 17