473,549 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_TABL E.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_TABL E.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.DataRo w row in table.GetChange s().Rows)
{
switch (row.RowState)
{
case DataRowState.De leted:
//--- 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 10424
Baylor,
Have you tried:

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

The DataTable.Selec t method will return an array of DataRows, which the
foreach will iterate over, instead of accessing the DataRowCollecti on
(DataTable.Rows property) directly.

Hope this helps
Jay
"baylor" <ba****@discuss ions.microsoft. com> wrote in message
news:F7******** *************** ***********@mic rosoft.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_TABL E.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_TABL E.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.DataRo w row in table.GetChange s().Rows)
| {
| switch (row.RowState)
| {
| case DataRowState.De leted:
| //--- 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******** **********@tk2m sftngp13.phx.gb l...
Baylor,
Have you tried:

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

The DataTable.Selec t method will return an array of DataRows, which the
foreach will iterate over, instead of accessing the DataRowCollecti on
(DataTable.Rows property) directly.

Hope this helps
Jay
"baylor" <ba****@discuss ions.microsoft. com> wrote in message
news:F7******** *************** ***********@mic rosoft.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_TABL E.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_TABL E.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.DataRo w row in table.GetChange s().Rows)
| {
| switch (row.RowState)
| {
| case DataRowState.De leted:
| //--- 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******** ********@TK2MSF TNGP10.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******** **********@tk2m sftngp13.phx.gb l...
| > Baylor,
| > Have you tried:
| >
| > foreach (DataRow row in dataSet.MY_TABL E.Select()) {
| > row.Delete();
| > }
| >
| > The DataTable.Selec t method will return an array of DataRows, which the
| > foreach will iterate over, instead of accessing the DataRowCollecti on
| > (DataTable.Rows property) directly.
| >
| > Hope this helps
| > Jay
| >
| >
| > "baylor" <ba****@discuss ions.microsoft. com> wrote in message
| > news:F7******** *************** ***********@mic rosoft.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_TABL E.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_TABL E.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.DataRo w row in table.GetChange s().Rows)
| > | {
| > | switch (row.RowState)
| > | {
| > | case DataRowState.De leted:
| > | //--- 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
6444
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 results below, I would think that the delete should have deleted row 1 {1 5 me) and not row 3 (1 5 they) when I run this statement delete t1...
1
8868
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 automatically deletes also all those rows in the child table whose foreign key values are equal to the referenced key value in the parent row. However:
2
11785
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 2 Gig. When I delete data from the database, it takes a lot of system resources and monopolizes the database so that all other query requests are...
16
3855
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 table, or perhaps bytes, being updated where the engine just decides, screw it, i'll just make a new one. surfed this group and google, but...
2
5060
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 me hours to get it working in both the dataset and the database itself. It works now, but the code looks so ugly to me. Can someone tell me what I...
9
2784
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 this: id Name Surname pictr0 picrt1 pictr2 pictr3
6
3843
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 "Generations"), and it allows the user to add, edit and delete the various records of the table. "Generations" table has the following fields: "IDPerson",...
4
2202
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... can someone correct this... <!-- Editable Area --> <?php include ("../dbconnection/connect.php"); $tbl_name="schedule"; // Table name
3
12309
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 other reading/deleting rows. The 'deleter' application runs a MIN/MAX (timestamp) for each ID and, if the difference between min/max is greater...
8
2967
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 table X add primary key (MACHINE_ID, CTIME); Our C++ application inserts data into a table X using CLI array insert
0
7450
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7809
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6043
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.