December 29, 2008

This row already belongs to another table Exception in dataset, datatable, ado.net

Error / Exception:
This row already belongs to another table

This error occur when a row from a datatable is added to another dataTable.

as a row can only belong to only one table. so this exception occurs.
Source row may be selected using dataTable.Select() or find. or direct dtTable.Rows[0] etc.

in every case it will generate error. so to avoid this error.
You can use following to methods

1. add a new row in destination table and copy field one by one from source to destination.

2. using ImportRow
firsly clone the table.

DataTable dest = source.Clone() // it will copy the structure to the dest table.

now dest.ImportRow(dest.Rows[0]);

it will run sucessfully.

any doubt please comment

1 comment:

ali0482 said...

Thanks ! worked perfectly!