473,406 Members | 2,894 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,406 software developers and data experts.

Deleting rows in ADO.NET

Hi!

I try to delete rows from the table in such way
foreach(DataRow row in MyDataSet.MyTable)
{
row.Delete();
}
and recive an error that collection is changed and enumeration is
impossible.
I guesss that foraech operator uses collection of undeleted rows and this
causes such behaviour.

Is there another approach to mark all rows in the table to deleted state?

Thanks
Dmitry
Nov 15 '05 #1
6 1709
accurate code is:

foreach(DataRow row in MyDataSet.MyTable.Rows)
{
row.Delete();
}

Dmitry
Nov 15 '05 #2
Dmitry,

The easier way would be to cycle through the rows backwards (using an
index), and then call the delete method on them while you cycle backwards,
like this:

// Cycle backwards.
for (int pintIndex = MyDataSet["MyTable"].Rows.Count - 1; pintIndex >= 0;
pintIndex--)
// Delete the row.
MyDataSet["MyTable"].Rows[pintIndex].Delete();

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dmitry Karneyev" <ka******@msn.com> wrote in message
news:ei*************@tk2msftngp13.phx.gbl...
Hi!

I try to delete rows from the table in such way
foreach(DataRow row in MyDataSet.MyTable)
{
row.Delete();
}
and recive an error that collection is changed and enumeration is
impossible.
I guesss that foraech operator uses collection of undeleted rows and this
causes such behaviour.

Is there another approach to mark all rows in the table to deleted state?

Thanks
Dmitry

Nov 15 '05 #3
Hi Nicholas,

Actualy your way would be the only correct.
The reason is that if row is in Added RowState and you call Delete() on it,
it will be *removed* from collection and cause an exception if you are
within foreach loop like Dmitry does or index out of range if within
positive for loop.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ot**************@TK2MSFTNGP10.phx.gbl...
Dmitry,

The easier way would be to cycle through the rows backwards (using an
index), and then call the delete method on them while you cycle backwards,
like this:

// Cycle backwards.
for (int pintIndex = MyDataSet["MyTable"].Rows.Count - 1; pintIndex >= 0;
pintIndex--)
// Delete the row.
MyDataSet["MyTable"].Rows[pintIndex].Delete();

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dmitry Karneyev" <ka******@msn.com> wrote in message
news:ei*************@tk2msftngp13.phx.gbl...
Hi!

I try to delete rows from the table in such way
foreach(DataRow row in MyDataSet.MyTable)
{
row.Delete();
}
and recive an error that collection is changed and enumeration is
impossible.
I guesss that foraech operator uses collection of undeleted rows and this causes such behaviour.

Is there another approach to mark all rows in the table to deleted state?
Thanks
Dmitry


Nov 15 '05 #4
Hi Dmitry,

This is not a safe way.
See Nicholas' reply and my reply to him.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"Dmitry Karneyev" <ka******@msn.com> wrote in message
news:ud**************@TK2MSFTNGP09.phx.gbl...
accurate code is:

foreach(DataRow row in MyDataSet.MyTable.Rows)
{
row.Delete();
}

Dmitry

Nov 15 '05 #5
What about this?

while (myTable.Rows.Count > 0)
{
myTable.Rows[0].Delete();
}

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #6
Frank,

That should work fine as well. The only problem with this is that you
have to be sure that the Count property is affected by the Delete, and not
just set so that the row is in a "delete" state.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Frank Oquendo" <fr****@acadxpin.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
What about this?

while (myTable.Rows.Count > 0)
{
myTable.Rows[0].Delete();
}

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)

Nov 15 '05 #7

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

Similar topics

3
by: ScottH | last post by:
I was looking for thw SQL to delete dupes from a table, and came across this. All who saw it agreed in principle, but I can't quite figure out the logic. If we are deleting all rows whose rowid...
5
by: Mojtaba Faridzad | last post by:
Hi, with SetDataBinding( ) a DataGrid shows a DataView. user can select some rows in the grid by holding cotrol key. when user clicks on Delete button, I should delete all selected rows. I am...
4
by: Jim Michaels | last post by:
Is it safe to do what is below? deleting from a resultset while you are processing it? I don't know how dynamic the SQL database is. I assume you get a cursor to live dataset. Even if it is a...
9
by: Hamed | last post by:
Hello I have a DataGrid that a is bound to a DataTable. Some of the rows in the DataTable should not be deleted. How can I prohibit deleting of some identified rows? The problem could be...
4
by: MiziaQ | last post by:
Hey, I'm using the following code to write entries to a data file and then read them in an msflexgrid. I now would like to add code under a delete button to use the table(grid) to delete rows from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.