DataSet ds = DAL.GetData();
DataSet dsInvitee = null;
DataTable dt = ds.Tables[0].Copy();
IEnumerable<DataRow> q1 = dt.AsEnumerable().Skip(5).Take(10);
dsInvitee = new DataSet();
DataTable dtNew = new DataTable();
dtNew.TableName = "DTInv";
dtNew = q1.CopyToDataTable<DataRow>();
dsInvitee.Tables.Add(dtNew.Copy());
dsInvitee.AcceptChanges();
dtNew = null;
dtNew = new DataTable();
dtNew.TableName = "DTTags";
dtNew = ds.Tables[1].Copy();
dsInvitee.Tables.Add(dtNew.Copy());
I am getting error in the last line as “A DataTable named ‘Table1’ already belongs to this DataSet.”… Please help.
Your issue is because of the line
dtNew = q1.CopyToDataTable<DataRow>();, because theCopyToDataTableextension method:This means that the table name of “DTInv” gets blown away as after the call to CopyToDataTable,
dtNewno longer refers to the sameDataTable. Move thedtNew.TableName = "DTInv";to after the call toCopyToDataTable: