<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Deleting dataRow from dataSet' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Deleting dataRow from dataSet' posted on the 'ASP.NET' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2008 Programmers Heaven</copyright>
    <pubDate>Sun, 06 Jul 2008 18:04:46 -0700</pubDate>
    <lastBuildDate>Sun, 06 Jul 2008 18:04:46 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Deleting dataRow from dataSet</title>
      <link>http://www.programmersheaven.com/mb/ASPNET/370618/370618/ReadMessage.aspx</link>
      <description>&lt;div class="BoardMessageAreaPrinterFriendly"&gt;
&lt;h2 class="BoardMessageSubjectPrinterFriendly"&gt;Deleting dataRow from dataSet&lt;/h2&gt;
&lt;div class="BoardMessageInfoPrinterFriendly"&gt;Posted by  on 26 Mar 2008 at 11:52 AM&lt;/div&gt;
&lt;div class="BoardMessageBodyPrinterFriendly"&gt;Hi guys, I'm having a bit of a problem here and it's driving me insane.&lt;br /&gt;
&lt;br /&gt;
I am looping through a dataset to check values against an array to see if they need to be deleted. If they exist in the array of items to be deleted, then I want to delete it -- seems easy enough right? I keep getting this damn error!!!&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;System.InvalidOperationException: Collection was modified; enumeration operation might not execute.&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
This is a snippet of the code:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
	' Break products that need to be deleted into an array
	Dim arrTrash() As String = prodList.Split(";")
	
	' Load XML file into dataset and delete relevant entries;
	Dim data As DataSet = New DataSet()
	data.ReadXml(xmlPath)
	
	For Each dr As DataRow In data.Tables(0).Rows
		If Array.IndexOf(arrTrash, dr("Affects")) &amp;gt; -1
			dr.Delete()
		End If
	Next
	data.Tables(0).AcceptChanges()
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Any help would be greatly appreciated!&lt;br /&gt;
&lt;pre class="sourcecode"&gt;&amp;lt;%
'// Programmed By: Zantos
'// VisualProgramming.NET
'// &lt;a href="http://vp.funurl.com/"&gt;http://vp.funurl.com/&lt;/a&gt;
'// visualprogramming@hotmail.com
%&amp;gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</description>
      <pubDate>Wed, 26 Mar 2008 11:52:52 -0700</pubDate>
      <category>ASP.NET</category>
    </item>
    <item>
      <title>Re: Deleting dataRow from dataSet</title>
      <link>http://www.programmersheaven.com/mb/ASPNET/370618/370620/ReadMessage.aspx#370620</link>
      <description>&lt;div class="BoardMessageAreaPrinterFriendly"&gt;
&lt;h2 class="BoardMessageSubjectPrinterFriendly"&gt;Re: Deleting dataRow from dataSet&lt;/h2&gt;
&lt;div class="BoardMessageInfoPrinterFriendly"&gt;Posted by  on 26 Mar 2008 at 12:21 PM&lt;/div&gt;
&lt;div class="BoardMessageBodyPrinterFriendly"&gt;If anyone can answer my question above, that would still be greatly appreciated; however, I finally figured out how to solve it -- reverse iteration. I simply swapped out the code above with the code below and it worked:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
For i As Integer = data.Tables(0).Rows.Count - 1 To 0 Step -1
	If Array.IndexOf(arrTrash, data.Tables(0).Rows(i)("Affects")) &amp;gt; -1
		data.Tables(0).Rows(i).Delete()
	End If
Next
data.Tables(0).AcceptChanges()
&lt;/pre&gt;&lt;br /&gt;
&lt;pre class="sourcecode"&gt;&amp;lt;%
'// Programmed By: Zantos
'// VisualProgramming.NET
'// &lt;a href="http://vp.funurl.com/"&gt;http://vp.funurl.com/&lt;/a&gt;
'// visualprogramming@gmail.com
%&amp;gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</description>
      <pubDate>Wed, 26 Mar 2008 12:21:04 -0700</pubDate>
      <category>ASP.NET</category>
    </item>
    <item>
      <title>Re: Deleting dataRow from dataSet</title>
      <link>http://www.programmersheaven.com/mb/ASPNET/370618/370621/ReadMessage.aspx#370621</link>
      <description>&lt;div class="BoardMessageAreaPrinterFriendly"&gt;
&lt;h2 class="BoardMessageSubjectPrinterFriendly"&gt;Re: Deleting dataRow from dataSet&lt;/h2&gt;
&lt;div class="BoardMessageInfoPrinterFriendly"&gt;Posted by  on 26 Mar 2008 at 1:13 PM&lt;/div&gt;
&lt;div class="BoardMessageBodyPrinterFriendly"&gt;: If anyone can answer my question above, that would still be greatly &lt;br /&gt;
: appreciated; however, I finally figured out how to solve it -- &lt;br /&gt;
: reverse iteration. I simply swapped out the code above with the code &lt;br /&gt;
: below and it worked:&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;: 
: For i As Integer = data.Tables(0).Rows.Count - 1 To 0 Step -1
: 	If Array.IndexOf(arrTrash, data.Tables(0).Rows(i)("Affects")) &amp;gt; -1
: 		data.Tables(0).Rows(i).Delete()
: 	End If
: Next
: data.Tables(0).AcceptChanges()
: &lt;/pre&gt;: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;: &amp;lt;%
: '// Programmed By: Zantos
: '// VisualProgramming.NET
: '// &lt;a href="http://vp.funurl.com/"&gt;http://vp.funurl.com/&lt;/a&gt;
: '// visualprogramming@gmail.com
: %&amp;gt;&lt;/pre&gt;: &lt;br /&gt;
&lt;br /&gt;
For Each always takes the next record in each iteration of the loop. This presents a problem if the table is modified during the iteration process. For example: Suppose in your table, the 3rd and 4th records are bound to be deleted. Once the For Each loop reaches the 3rd record, it is deleted. The 4th record becomes the 3rd, and 5th record becomes the 4th. Then it reads the next record (4th new count; 5th old count), thus stepping over one of the record, which needed to be deleted.&lt;br /&gt;
The problem becomes even more pronounced if someone sorts the records, while a For Each loop is iterating it. For example: suppose you have a lot of records holding expenses. You use a For Each loop to sum those expenses. What if someone sorts those simultaneously to find the highest expense. This would lead to a sum, which is useless.&lt;br /&gt;
To protect from these (and other) potential problems, most (if not all) For Each loop implementations will fail immediately if a modification in the data is detected, which is not instantiated by its own iterator.&lt;/div&gt;&lt;/div&gt;</description>
      <pubDate>Wed, 26 Mar 2008 13:13:57 -0700</pubDate>
      <category>ASP.NET</category>
    </item>
    <item>
      <title>Re: Deleting dataRow from dataSet</title>
      <link>http://www.programmersheaven.com/mb/ASPNET/370618/370651/ReadMessage.aspx#370651</link>
      <description>&lt;div class="BoardMessageAreaPrinterFriendly"&gt;
&lt;h2 class="BoardMessageSubjectPrinterFriendly"&gt;Re: Deleting dataRow from dataSet&lt;/h2&gt;
&lt;div class="BoardMessageInfoPrinterFriendly"&gt;Posted by  on 27 Mar 2008 at 7:59 AM&lt;/div&gt;
&lt;div class="BoardMessageBodyPrinterFriendly"&gt;: For Each always takes the next record in each iteration of the loop. &lt;br /&gt;
: This presents a problem if the table is modified during the &lt;br /&gt;
: iteration process. For example: Suppose in your table, the 3rd and &lt;br /&gt;
: 4th records are bound to be deleted. Once the For Each loop reaches &lt;br /&gt;
: the 3rd record, it is deleted. The 4th record becomes the 3rd, and &lt;br /&gt;
: 5th record becomes the 4th. Then it reads the next record (4th new &lt;br /&gt;
: count; 5th old count), thus stepping over one of the record, which &lt;br /&gt;
: needed to be deleted.&lt;br /&gt;
: The problem becomes even more pronounced if someone sorts the &lt;br /&gt;
: records, while a For Each loop is iterating it. For example: suppose &lt;br /&gt;
: you have a lot of records holding expenses. You use a For Each loop &lt;br /&gt;
: to sum those expenses. What if someone sorts those simultaneously to &lt;br /&gt;
: find the highest expense. This would lead to a sum, which is useless.&lt;br /&gt;
: To protect from these (and other) potential problems, most (if not &lt;br /&gt;
: all) For Each loop implementations will fail immediately if a &lt;br /&gt;
: modification in the data is detected, which is not instantiated by &lt;br /&gt;
: its own iterator.&lt;br /&gt;
&lt;br /&gt;
Well here's the kicker, I *have* code that works doing this (in another project), here it is:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
For Each dr As DataRow In data.Tables(0).Rows
	If dr(0).ToString().ToLower() = "name"
		dr.Delete()
	ElseIf dr(0).ToString().ToLower() = "image"
		dr.Delete()
	ElseIf dr(1).ToString().ToLower() = "not applicable"
		dr.Delete()
	End If
Next
	
' Display author information
tblAuthorDetails.DataSource = data.Tables(0)
tblAuthorDetails.DataBind()
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
and that works perfectly. So why would this code work and not the code above? Doesn't make sense, does it?&lt;br /&gt;
&lt;pre class="sourcecode"&gt;&amp;lt;%
'// Programmed By: Zantos
'// VisualProgramming.NET
'// &lt;a href="http://vp.funurl.com/"&gt;http://vp.funurl.com/&lt;/a&gt;
'// visualprogramming@gmail.com
%&amp;gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</description>
      <pubDate>Thu, 27 Mar 2008 07:59:24 -0700</pubDate>
      <category>ASP.NET</category>
    </item>
  </channel>
</rss>