473,396 Members | 1,916 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,396 software developers and data experts.

How to delete rows?

i have a DataSet (TDS). It has 4 rows in it. i want to erase those. For the
life of me i can't figure out how

Option 1: foreach
foreach (DataRow row in dataSet.MY_TABLE.Rows) {
row.Delete();
}

This doesn't work. It erases the first row and then throws the exception
Someone Changed The Enumerator. No fun

Option 2: Clear()
dataSet.MY_TABLE.Clear();

This wipes out all the rows. Unfortunately, when i go to save the changes
the rows don't show up. Here's the update code:

foreach (dataSet.DataRow row in table.GetChanges().Rows)
{
switch (row.RowState)
{
case DataRowState.Deleted:
//--- Never get here

Apparently Clear() doesn't update whatever it is that leads to GetChanges
and RowState

So those are what i've tried and have had no luck with. Other ideas?

In experimenting, it looks like perhaps the proper way to do these things is
to use for i=0 to Rows.Count. That will leave the rows where they are but two
things will happen. First, the RowState will get set to Deleted. Second, any
attempt to read a deleted row will throw an exception. This bothers me
because it seems like calling Delete from for i= and from foreach results in
different behavior - if it's just marking rows it doesn't seem like calling
Delete should have screwed up the enumerator

i think i can hobble my code together but does anyone have any insight as to
how this works and why?

-baylor
Jul 22 '05 #1
4 10412
Baylor,
Have you tried:

foreach (DataRow row in dataSet.MY_TABLE.Select()) {
row.Delete();
}

The DataTable.Select method will return an array of DataRows, which the
foreach will iterate over, instead of accessing the DataRowCollection
(DataTable.Rows property) directly.

Hope this helps
Jay
"baylor" <ba****@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.com...
|i have a DataSet (TDS). It has 4 rows in it. i want to erase those. For the
| life of me i can't figure out how
|
| Option 1: foreach
| foreach (DataRow row in dataSet.MY_TABLE.Rows) {
| row.Delete();
| }
|
| This doesn't work. It erases the first row and then throws the exception
| Someone Changed The Enumerator. No fun
|
| Option 2: Clear()
| dataSet.MY_TABLE.Clear();
|
| This wipes out all the rows. Unfortunately, when i go to save the changes
| the rows don't show up. Here's the update code:
|
| foreach (dataSet.DataRow row in table.GetChanges().Rows)
| {
| switch (row.RowState)
| {
| case DataRowState.Deleted:
| //--- Never get here
|
| Apparently Clear() doesn't update whatever it is that leads to GetChanges
| and RowState
|
| So those are what i've tried and have had no luck with. Other ideas?
|
| In experimenting, it looks like perhaps the proper way to do these things
is
| to use for i=0 to Rows.Count. That will leave the rows where they are but
two
| things will happen. First, the RowState will get set to Deleted. Second,
any
| attempt to read a deleted row will throw an exception. This bothers me
| because it seems like calling Delete from for i= and from foreach results
in
| different behavior - if it's just marking rows it doesn't seem like
calling
| Delete should have screwed up the enumerator
|
| i think i can hobble my code together but does anyone have any insight as
to
| how this works and why?
|
| -baylor
Jul 22 '05 #2

I think you have to capture the rowcount in a separate variable and then use
a for or while loop that uses the variable. Something like
table.rows[i].delete();
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
Baylor,
Have you tried:

foreach (DataRow row in dataSet.MY_TABLE.Select()) {
row.Delete();
}

The DataTable.Select method will return an array of DataRows, which the
foreach will iterate over, instead of accessing the DataRowCollection
(DataTable.Rows property) directly.

Hope this helps
Jay
"baylor" <ba****@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.com...
|i have a DataSet (TDS). It has 4 rows in it. i want to erase those. For
the
| life of me i can't figure out how
|
| Option 1: foreach
| foreach (DataRow row in dataSet.MY_TABLE.Rows) {
| row.Delete();
| }
|
| This doesn't work. It erases the first row and then throws the exception
| Someone Changed The Enumerator. No fun
|
| Option 2: Clear()
| dataSet.MY_TABLE.Clear();
|
| This wipes out all the rows. Unfortunately, when i go to save the
changes
| the rows don't show up. Here's the update code:
|
| foreach (dataSet.DataRow row in table.GetChanges().Rows)
| {
| switch (row.RowState)
| {
| case DataRowState.Deleted:
| //--- Never get here
|
| Apparently Clear() doesn't update whatever it is that leads to
GetChanges
| and RowState
|
| So those are what i've tried and have had no luck with. Other ideas?
|
| In experimenting, it looks like perhaps the proper way to do these
things
is
| to use for i=0 to Rows.Count. That will leave the rows where they are
but
two
| things will happen. First, the RowState will get set to Deleted. Second,
any
| attempt to read a deleted row will throw an exception. This bothers me
| because it seems like calling Delete from for i= and from foreach
results
in
| different behavior - if it's just marking rows it doesn't seem like
calling
| Delete should have screwed up the enumerator
|
| i think i can hobble my code together but does anyone have any insight
as
to
| how this works and why?
|
| -baylor

Jul 22 '05 #3
Baylor,

When I am deleting in Net from a collection as you do now, do I take forever
the approach to do it bottom up using the for index

Maybe you can do in future C# versions something with the yield for that,
however that is just a guess of my.

I hope this helps,

Cor
Jul 22 '05 #4
Richard,
Why? The sample I gave works for DataTables! As does the more general
approach described by Cor.

Hope this helps
Jay
"Richard P" <or**@community.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
|
| I think you have to capture the rowcount in a separate variable and then
use
| a for or while loop that uses the variable. Something like
| table.rows[i].delete();
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
| news:%2******************@tk2msftngp13.phx.gbl...
| > Baylor,
| > Have you tried:
| >
| > foreach (DataRow row in dataSet.MY_TABLE.Select()) {
| > row.Delete();
| > }
| >
| > The DataTable.Select method will return an array of DataRows, which the
| > foreach will iterate over, instead of accessing the DataRowCollection
| > (DataTable.Rows property) directly.
| >
| > Hope this helps
| > Jay
| >
| >
| > "baylor" <ba****@discussions.microsoft.com> wrote in message
| > news:F7**********************************@microsof t.com...
| > |i have a DataSet (TDS). It has 4 rows in it. i want to erase those. For
| > the
| > | life of me i can't figure out how
| > |
| > | Option 1: foreach
| > | foreach (DataRow row in dataSet.MY_TABLE.Rows) {
| > | row.Delete();
| > | }
| > |
| > | This doesn't work. It erases the first row and then throws the
exception
| > | Someone Changed The Enumerator. No fun
| > |
| > | Option 2: Clear()
| > | dataSet.MY_TABLE.Clear();
| > |
| > | This wipes out all the rows. Unfortunately, when i go to save the
| > changes
| > | the rows don't show up. Here's the update code:
| > |
| > | foreach (dataSet.DataRow row in table.GetChanges().Rows)
| > | {
| > | switch (row.RowState)
| > | {
| > | case DataRowState.Deleted:
| > | //--- Never get here
| > |
| > | Apparently Clear() doesn't update whatever it is that leads to
| > GetChanges
| > | and RowState
| > |
| > | So those are what i've tried and have had no luck with. Other ideas?
| > |
| > | In experimenting, it looks like perhaps the proper way to do these
| > things
| > is
| > | to use for i=0 to Rows.Count. That will leave the rows where they are
| > but
| > two
| > | things will happen. First, the RowState will get set to Deleted.
Second,
| > any
| > | attempt to read a deleted row will throw an exception. This bothers me
| > | because it seems like calling Delete from for i= and from foreach
| > results
| > in
| > | different behavior - if it's just marking rows it doesn't seem like
| > calling
| > | Delete should have screwed up the enumerator
| > |
| > | i think i can hobble my code together but does anyone have any insight
| > as
| > to
| > | how this works and why?
| > |
| > | -baylor
| >
| >
|
|
Jul 22 '05 #5

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

Similar topics

0
by: Gordon | last post by:
I have 2 tables t and t1. In this case, t1 is a copy of t. I want to delete rows from t1 based on criteria on the t table and a relationship between t ad t1 (in this case the id column). In the...
1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
2
by: Bob Ganger | last post by:
Hello, I am working on a project using SQL Server 2000 with a database containing about 10 related tables with a lot of columns containing text. The total current size of the database is about...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
2
by: NoSpam | last post by:
Hi, I am working with C# and ASP.NET with code behind and a SQL Server. I'm making an e-shop. When clients see what they have in their basket, I added a function DELETE to delete a line. It took...
9
by: Dejan | last post by:
Hy, Sorry for my terreble english I have this simple code for deleting rows in mysql table... Everything works fine with it. So, what do i wanna do...: my sql table looks something like...
6
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called...
4
imarkdesigns
by: imarkdesigns | last post by:
Hello everyone.. a newbie programmer here wants to ask why is my codes for deleting rows from my databse won't work.... seems that this codes spread to the net and no one refuse to correct it... ...
3
by: Michel Esber | last post by:
Hello, Environment: DB2 LUW v8 FP15 / Linux I have a table with 50+ Million rows. The table structure is basically (ID - Timestamp). I have two main applications - one inserting rows, and the...
8
by: Michel Esber | last post by:
Hello, Env: DB2 V8 LUW FP16 running Linux create Table X (machine_id varchar(24) not null, ctime timestamp not null); create index iFoo on X (MACHINE_ID, CTIME) allow reverse scans; alter...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.