Removing elements from a typed dataset

  • Thread starter Thread starter Ayoa
  • Start date Start date
A

Ayoa

I have the following code whose purpose is to take 2 typed datasets (of type
DSAccounts ) & remove all elements of one from the other. and return what
is left.

However I am getting the error message "The given datarow is not in the
current DataRowCollection" when the "dtAll.tblAccounts.Rows.Remove(dr);"
line executes. I have debugged and found that the data to be deleted is
definitely present in the master dataset.

What am i doing wrong?? Please help!



private DSAccounts RemoveFromDS(DSAccounts dtAll, DSAccounts dtRemove ) {


try {

string account;

foreach (DSAccounts.tblAccountsRow dr in dtRemove.tblAccounts.Rows) {

account = dr[0].ToString();

if (dtAll.tblAccounts.Rows.Contains(account))

dtAll.tblAccounts.Rows.Remove(dr);

}


return dtAll;

}

catch (Exception e) {

throw e;

}


}
 
Have you tried calling AcceptChanges() on the dataset to commit all the
changes that you have just made to it.

Emma
 
You are Checking DataRow by Key and trying to delete Row. even though
rows in dtAll and dtRemove have same Key Both are diffrent. So you
need to Select Row from dtAll by Key and then try to remove.

e.g Column "Id" has primary Key So select Row by Key and then delete
it

try
{
string account;
for(int i =0; i< dtRemove.DSAccountdt.Rows.Count; i++ )
{
DSAccount.DSAccountdtRow dr =
(DSAccount.DSAccountdtRow)dtRemove.DSAccountdt;
account = dr[0].ToString();
if (dtAll.DSAccountdt.Rows.Contains(account))
{
DataRow[] dr1 = dtAll.DSAccountdt.Select("Id=" + account);
dtAll.DSAccountdt.Rows.Remove(dr1[0]);
}

}
return dtAll;
}


Hope It wil work

ABCL
 
Back
Top