Home All Groups Group Topic Archive Search About

Removing elements from a typed dataset

Author
10 Jan 2006 5:58 PM
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;

}


}

Author
10 Jan 2006 7:03 PM
Sericinus hunter
When you iterate a collection with foreach loop you cannot add or remove
collection elements. Try using 'for' loop instead, counting from Rows.Count-1 to zero.

Ayoa wrote:
Show quote
> 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;
>
> }
>
>
> }
>
>

AddThis Social Bookmark Button