Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / June 2005

Tip: Looking for answers? Try searching our database.

How to delete rows?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
baylor - 29 Jun 2005 23:27 GMT
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
Jay B. Harlow [MVP - Outlook] - 29 Jun 2005 23:57 GMT
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

|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
[quoted text clipped - 37 lines]
|
| -baylor
Richard P - 30 Jun 2005 08:26 GMT
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();

> Baylor,
> Have you tried:
[quoted text clipped - 64 lines]
> |
> | -baylor
Jay B. Harlow [MVP - Outlook] - 30 Jun 2005 13:22 GMT
Richard,
Why? The sample I gave works for DataTables! As does the more general
approach described by Cor.

Hope this helps
Jay

| 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
[quoted text clipped - 68 lines]
| > |
| > | -baylor
Cor Ligthert - 30 Jun 2005 09:29 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.