473,322 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Deleting a datarow from a dataset!

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.

Jul 14 '06 #1
3 2825
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]
- mv*@spam.guard.caspershouse.com

"DBC User" <db*****@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
>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.

Jul 14 '06 #2

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.

Jul 14 '06 #3
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" <db*****@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
>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.

Jul 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christoph | last post by:
I'm delving into using ADO.NET for the first time. In fact, I've never done any database work using C# at all so all of this is new to me. Please bear that in mind if I am asking stupid questions....
4
by: Michael Carr | last post by:
I have a function that populates a class with values from a database. I'd like to pass into the function either a SqlDataReader or a DataRow, depending on which mechanism I'm using to retrieve data...
3
by: marshallarts | last post by:
Hello, I have a datagrid (grdShots) on a form, and a button to allow the user to delete records from the dataset underlying the grid. My code appears to work, because the row disappears from...
1
by: Arpan | last post by:
This is how I am dynamically adding a table to a DataSet: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) 'Create an empty DataSet Dim dSet As New DataSet ...
5
by: samoore33 | last post by:
I use the code below to return rows matching the state in the theState variable. I want to know if it is possible to search through the DataRow that I am returning with the search. I understand...
5
by: samoore33 | last post by:
I use the code below to search through a DataSet: Dim t As DataTable t = result.Tables("State") Dim strExpr As String strExpr = "id = '" & theState.ToString() & "'" Dim foundRows() As DataRow...
7
by: Susan Mackay | last post by:
I have a data table that is connected to a database table with a data adapter in the 'standard' manner. However I want to be able to remove selected rows from the data table (i.e. no longer...
2
by: sherifffruitfly | last post by:
Hi, I want to delete rows from my database. So I download them to a dataset, and then discover the following. This does not work: dataset.Rows.Clear(); dataadapter.Update(dataset,...
8
by: David | last post by:
Hi all, Using C# 1.1 I have a dataset loaded from an XML file. The idea being that if the app (a winform app that relies on a webservice) is offline, then I save data locally in XML and...
3
by: laurasaur | last post by:
Hi everyone, I have 2 listboxes that I need to move items between, they are both bound to DataTables which get populated from the database with a list of clients. Im getting a few problems,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.