Asked by:
Collection was modified; enumeration operation may not execute.

Question
-
User1215529056 posted
I have 2 Forms that I use to add or edit SQL data.
I am now getting this error when I add or edit data.
They were all working at one point.
They all work fine when I run the site from inside VS 2015 Debug Mode
Here's the code behind data. Any idea what needs to be edited?
Vendor Add code ----------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class VendorAdd : System.Web.UI.Page
{
public void dvAddVendor_ItemInserting(Object sender, DetailsViewInsertEventArgs e)
{
foreach (System.Collections.DictionaryEntry entry in e.Values)
{
e.Values[entry.Key] = System.Web.HttpUtility.HtmlEncode(entry.Value.ToString());
}
}protected void dvAddVendor_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "Cancel")
{
Response.Redirect("~/VendorsShow.aspx");}
}
}Vendor Edit Code ----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class VendorEdit : System.Web.UI.Page
{
public void dvEditVendor_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e)
{
foreach (System.Collections.DictionaryEntry entry in e.NewValues)
{
e.NewValues[entry.Key] = System.Web.HttpUtility.HtmlEncode(entry.Value.ToString());
}
}public void dvEditVendor_ItemUpdated(Object sender, DetailsViewUpdatedEventArgs e)
{
Response.Redirect("~/VendorsShow.aspx");
}protected void dvEditVendor_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "Cancel")
{
Response.Redirect("~/VendorsShow.aspx");
}
}}
Phone List Edit --------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class PhoneList : System.Web.UI.Page
{public void gvPhoneList_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
foreach (System.Collections.DictionaryEntry entry in e.NewValues)
{
e.NewValues[entry.Key] = System.Web.HttpUtility.HtmlEncode(entry.Value.ToString());
}
}protected void btnPrintPhoneList_Click(object sender, EventArgs e)
{
CrystalDecisions.CrystalReports.Engine.ReportDocument crd = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
crd.Load(Server.MapPath("PhoneList.rpt"));
crd.SetDatabaseLogon("sa", "fred", "sql-new", "PSI_DB_1");
MemoryStream mem = (MemoryStream)crd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.BinaryWrite(mem.ToArray());
}
}Thursday, May 9, 2019 9:21 PM
All replies
-
User839733648 posted
Hi RuthlessRoth,
Collection was modified; enumeration operation may not execute.In general,.Net collections do not support being enumerated and modified at the same time.
So when you're iterating on a collection, you cannot modify its elements, add new ones or delete any elements.
To fix the issue, you could copy collection to another.
foreach (System.Collections.DictionaryEntry entry in e.Values.ToArray())
For more about the error, you could refer to:
Best Regards,
Jenifer
Friday, May 10, 2019 6:28 AM -
User1215529056 posted
the odd things are:
1) It worked this way for several years until I moved it from XP machine to windows 7 pro. Same code
2) I can not reproduce this error in development. It only happens on the intranet website machine.
Friday, May 10, 2019 1:25 PM -
User753101303 posted
Hi,
Are you testing with a value that actually changes when it is HTML encoded? This kind of behavior usually happens when the error depends on your data. You could also instrument your app to see on which exact value it fails and test then with the same value.
Your intent was to save HTML Encoded values inside your DB ?
Friday, May 10, 2019 3:09 PM -
User1215529056 posted
For the phone list code I went in and edited an item and it saved without issue from Dev. Do the same on the site iteslef it fails with the error
Both Vendor Edit & Phone edit use a gridview.
Friday, May 10, 2019 5:57 PM -
User1120430333 posted
If you are using a foreach loop, then you cannot make changes to the object in the collection. If you were doing this and you got away with this previously, it is a mystery. But you can't do it now it seems.
You can change the object in the collection if you use a for loop using an index.
Friday, May 10, 2019 9:13 PM -
User1215529056 posted
Each time I modified the code I must be getting it wrong.
Could you show me the correct way to do it?
foreach (System.Collections.DictionaryEntry entry in e.Values)
{
e.Values[entry.Key] = System.Web.HttpUtility.HtmlEncode(entry.Value.ToString());
}Friday, May 10, 2019 9:18 PM -
User1120430333 posted
for (int i = 0; i < e.Values.count; i++) { e.Values[i].[entry.Key] = System.Web.HttpUtility.HtmlEncode(entry.Value.ToString()); }
It may work for you try it.
Friday, May 10, 2019 10:23 PM -
User1215529056 posted
did work
for (int i = 0; i < e.Values.count; i++)
{
e.Values[i].[entry.Key] = System.Web.HttpUtility.HtmlEncode(entry.Value.ToString());
}Values & entry both had red underlines. Would not run in debug
Severity Code Description Project File Line Suppression State
Error CS1061 'DetailsViewUpdateEventArgs' does not contain a definition for 'Values' and no extension method 'Values' accepting a first argument of type 'DetailsViewUpdateEventArgs' could be found (are you missing a using directive or an assembly reference?) 9ishNewInternal C:\Users\andy.NEYENESCH\Documents\Visual Studio 2015\WebSites\9ishNewInternal\VendorEdit.aspx.cs 14 ActiveSeverity Code Description Project File Line Suppression State
Error CS0103 The name 'entry' does not exist in the current context 9ishNewInternal C:\Users\andy.NEYENESCH\Documents\Visual Studio 2015\WebSites\9ishNewInternal\VendorEdit.aspx.cs 16 ActiveFriday, May 10, 2019 10:31 PM -
User1215529056 posted
Sorry Did Not work
Friday, May 10, 2019 10:32 PM -
User1120430333 posted
Sorry Did Not work
What is the purpose of the code that seems to be trying to loop and set keys to the same value?
Saturday, May 11, 2019 4:28 AM -
User1215529056 posted
It looks at one record, the one being edited and updates the fields in that record.
Saturday, May 11, 2019 11:49 AM -
User1120430333 posted
It looks at one record, the one being edited and updates the fields in that record.
If e.Values is some kind of a collection and only one item is in the collection, then maybe you can use a Linq.Single() on it instead of trying to loop.
e.Values[entry.Key].Single() = System.Web.HttpUtility.HtmlEncode(entry.Value.ToString());Saturday, May 11, 2019 3:17 PM -
User1215529056 posted
I almost have it. The only issue is if I remove all text from a field it will not update that field. It leaves the previous data in the field.
Current Code
IOrderedDictionary dic = e.NewValues;
for (int i = 0; i < dic.Keys.Count; i++)
{e.NewValues[i] = System.Web.HttpUtility.HtmlEncode(e.NewValues[i].ToString());
}
Tuesday, May 14, 2019 5:36 PM -
User1120430333 posted
If there is only one item in Keys, why do you even need the loop?
e.NewValues[0] = System.Web.HttpUtility.HtmlEncode(e.NewValues[0].ToString());
It also just looks like you are writing over the existing value with itself.
Tuesday, May 14, 2019 5:57 PM -
User1215529056 posted
True that does work. I didn't know it loaded all fields in the row.
Still have one issue of it not updating a previous field that had data in it to an empty filed.
Any ideas?
I have not done any web site stuff in 10 years so I'm REALLY rusty.
Tuesday, May 14, 2019 6:12 PM -
User1120430333 posted
Myself I would use the debugger and look at the content of e.Values and NewValues by using Visual Studio Quickwatch. You should be able to see public properties in the objects. And maybe you can do an intellisense on the object, like e.Valuses[0]. the "." does the expose and let Visual Stuido expose public properties or methods that you can use on the object inside Values.
Tuesday, May 14, 2019 8:14 PM