When you try to read row from one table and add to another table, if you get the exception i.e. ""This row already belongs to another table". For example:
foreach (DataRow drWrite in dtRead.Rows)
{
dtWrite.Rows.Add(drWrite); //Error thrown here.
}
There are two solution for above exception:
- You need to create a "NewRow" with the values from reading row of reading table first. For example
DataRow drWrite = dtWrite.NewRow();
drWrite["Col1"] = dtRead.Rows[0]["Col1"];
drWrite["Col2"] = dtRead.Rows[0]["Col2"];
dtWrite.Rows.Add(drWrite);
-
You can also use "Add" which takes an array of values. For example
dtWrite.Rows.Add(drWrite.ItemArray);
or
dtWrite.ImportRow(drWrite);
Hope it helps. Happy Coding!!
0 Comment(s)