PC Review


Reply
Thread Tools Rate Thread

Deleting a datarow from a dataset!

 
 
DBC User
Guest
Posts: n/a
 
      14th Jul 2006
I would like to delete a row from a dataset based on a condition. Here
is what I am doing

foreach(row in table1)
{
string key = row[columnname];
foreach(row1 in table2)
{
if (row1[columnname].tostring().equals(key))
row1.delete();
}
}

[I made it simple and please ignore the syntax, I have to code
correct]. right adter I do row1.delete on the next foreach loop of
table2, the program throws an exception saying the collection is
changed. How can I delete a row in table2 based on values from table1?

Thanks in advance.

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      14th Jul 2006
DBC User,

Yes, this is correct. You are not allowed to modify elements of an
enumeration while enumerating through them.

To get around this, use a for loop, and use the indexer on the
collection exposed by the Rows property.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"DBC User" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I would like to delete a row from a dataset based on a condition. Here
> is what I am doing
>
> foreach(row in table1)
> {
> string key = row[columnname];
> foreach(row1 in table2)
> {
> if (row1[columnname].tostring().equals(key))
> row1.delete();
> }
> }
>
> [I made it simple and please ignore the syntax, I have to code
> correct]. right adter I do row1.delete on the next foreach loop of
> table2, the program throws an exception saying the collection is
> changed. How can I delete a row in table2 based on values from table1?
>
> Thanks in advance.
>



 
Reply With Quote
 
DBC User
Guest
Posts: n/a
 
      14th Jul 2006

DBC User wrote:
> I would like to delete a row from a dataset based on a condition. Here
> is what I am doing
>
> foreach(row in table1)
> {
> string key = row[columnname];
> foreach(row1 in table2)
> {
> if (row1[columnname].tostring().equals(key))
> row1.delete();
> }
> }
>
> [I made it simple and please ignore the syntax, I have to code
> correct]. right adter I do row1.delete on the next foreach loop of
> table2, the program throws an exception saying the collection is
> changed. How can I delete a row in table2 based on values from table1?
>
> Thanks in advance.


I found the problem. I should have break out of the loop once I found
the key. Now it works.

 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      14th Jul 2006
Hi,

Rows is a collection you CANNOT modify any collection while being iterating.

what you can do is keep a reference to the rows you want to delete:

ArrayList todelete = new ArrayList();
foreach(row in table1)
{
string key = row[columnname];
foreach(row1 in table2)
{
if (row1[columnname].tostring().equals(key))
todelete.Add( row1);
}
}
foreach( row in todelete )
table2.Rows.Delete( row)


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"DBC User" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I would like to delete a row from a dataset based on a condition. Here
> is what I am doing
>
> foreach(row in table1)
> {
> string key = row[columnname];
> foreach(row1 in table2)
> {
> if (row1[columnname].tostring().equals(key))
> row1.delete();
> }
> }
>
> [I made it simple and please ignore the syntax, I have to code
> correct]. right adter I do row1.delete on the next foreach loop of
> table2, the program throws an exception saying the collection is
> changed. How can I delete a row in table2 based on values from table1?
>
> Thanks in advance.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Trouble deleting DataRow in DataSet Miha Markic [MVP C#] Microsoft ADO .NET 0 13th Dec 2006 07:36 PM
Dataset doesn't return true when dataset datarow has been modified =?Utf-8?B?QWxwaGE=?= Microsoft C# .NET 3 11th Nov 2005 09:16 PM
DataSet and DataRow Question Yosh Microsoft C# .NET 1 15th Jun 2005 12:38 AM
DataSet and DataRow Question Yosh Microsoft Dot NET 1 14th Jun 2005 08:45 PM
DataSet.Merge(DataRow) scott mesick Microsoft ADO .NET 1 13th Feb 2004 08:42 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:09 PM.