This Row already belongs to another table error when trying to add rows?
Answer 1: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:
// This works because the row was added to the original table.
myTable.ImportRow(dr);
// The following won't work. No data will be added or exception thrown.
var drFail = dt.NewRow()
drFail["CustomerID"] = "[Your data here]";
// dt.Rows.Add(row); // Uncomment for import to succeed.
myTable.ImportRow(drFail);
Answer 2: the key point is to use the importrow method
Try this:
DataTable dt = (DataTable)Session["dtAllOrders"];
DataTable dtSpecificOrders = dt.Clone();
DataRow[] orderRows = dt.Select("CustomerID = 2");
foreach (DataRow dr in orderRows)
{
dtSpecificOrders.ImportRow(dr);
}
Answer 3
yourTable.ImportRow(dataRow);
It’s because the row you’re copying doesn’t have the same TableName
:
For example, try:
Table1.TableName = "Table1";
Table2.TableName = "Table2";
Similar Posts:
- DataRow Write Data Error: column 0 or column 1 cannot be found
- [Solved] mybatis Bulk Insert Error: Column count doesn‘t match value count at row 1
- DB2 Errors and How to Solve them
- mysql Column count doesn’t match value count at row 1
- The solution of the table is full in MySQL
- SQL0668N Operation not allowed for reason code “3” on table “TEST”. SQLSTAT…
- How to Solve MyBatisPlus Error: Failed to process, please exclude the tableName or statementId
- With check option in SQL
- [Solved] MYSQL Error: Cannot delete or update a parent row: a foreign key constraint fails
- [Solved] Greenplum Use the Storage Error: function cannot execute on a QE slice because it accesses relation