471,318 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

Removing elements from a typed dataset

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;

}
}
Jan 10 '06 #1
3 6746
Have you tried calling AcceptChanges() on the dataset to commit all the
changes that you have just made to it.

Emma

Jan 10 '06 #2
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[i];
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

Jan 10 '06 #3
Thank you,ABCL. It works!!

"ABCL" <oh****@hotmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
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[i];
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

Jan 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Trond | last post: by
3 posts views Thread by Freeon | last post: by
4 posts views Thread by Ronald S. Cook | last post: by
21 posts views Thread by Peter Bradley | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.