Collection Was Modified; Enumeration May Not Execute Error When Removing a Listitem from a Listbox

Collection was modified; enumeration may not execute error when removing a ListItem from a LIstBox

It's not possible to modify a collection while you're enumerating it in .Net. You need to separate out your enumeration and remove code into different blocks. Here is a quick sample on how to do that in without LINQ

protected void btnAdd_Click(object sender, EventArgs e)
{
var selected = new List<ListItem>();
foreach (ListItem item in lstAvailableColors.Items)
{
if (item.Selected)
{
selected.Add(item);
lstSelectedColors.Items.Add(item);
}
}
foreach (ListItem item in selected)
{
lstAvailableColors.Items.Remove(item);
}
}

And here's a more concise version using LINQ

var selected = lstAvailableColors.Cast<ListItem>().Where(i => i.Selected).ToList();
selected.ForEach( x => { lstSelectedColors.Items.Add(x); });
selected.ForEach( x => { lstAvailableColors.Items.Remove(x);});

EDIT

The LINQ version works in two parts. The first part is the first line which finds the currently selected items and stores the value in a List<ListItem>. It's very important that the line contain the .ToList() call because that forces the query to execute immediately vs. being delayed executed.

The next two lines iterate through each value which is selected and remove or add it to the appropriate list. Because the selected list is already stored we are no longer enumerating the collection when we modify it.

During Deleting the Listbox item Collection was modified; enumeration operation may not execute

You are modifying a collection while iterating over it. I don't have an IDE but can you try:

protected void btn3_Click(object sender, EventArgs e)
{
lbltext.Visible = false;
if (lstBoxAreaOfResponsibility2.Items.Count != 0)
{
lstBoxAreaOfResponsibility2.Items.Clear();
}
else
{
lbltext.Visible = true;
lbltext.Text = "There Is No Item To Move";
}
}

protected void btn4_Click(object sender, EventArgs e)
{
lbltext.Visible = false;
if (lstBoxAreaOfResponsibility2.SelectedIndex >= 0)
{
ListItem itemToDelete = new ListItem();
foreach (ListItem Item in lstBoxAreaOfResponsibility2.Items)
{
if (Item.Selected == true)
{
itemToDelete = Item;
}
}
lstBoxAreaOfResponsibility2.Items.Remove(itemToDelete);
}
else
{
lbltext.Visible = true;
lbltext.Text = "Please select atleast one in Listbox2 to move";
}
}

Selecting Multiple Items From ListBox causes Collection was modified; enumeration operation may not execute

You can't remove items from an enumeration while you are looping through it using a foreach, which is what the error message is trying to tell you.

Switching to a straight for loop will get your code to execute:

protected void btnAdd_Click(object sender, EventArgs e)
{
for(var i=0; i<lstMain.Items.Count; i++)
{
var list = lstMain.Items[i];
lstFAvourite.ClearSelection();
lstFAvourite.Items.Add(list);
lstMain.Items.Remove(list);
i--;
}
}

However, I believe this code will add all items from lstMain to lstFAvourite. I think perhaps that we should be looking at the Selected property of the ListItem as well in the for loop. For example:

protected void btnAdd_Click(object sender, EventArgs e)
{
for (var i = 0; i < lstMain.Items.Count; i++)
{
var list = lstMain.Items[i];
if(!list.Selected) continue;
lstFAvourite.ClearSelection();
lstFAvourite.Items.Add(list);
lstMain.Items.Remove(list);

//Decrement counter since we just removed an item
i--;
}
}

Collection was modified; enumeration operation may not execute

What's likely happening is that SignalData is indirectly changing the subscribers dictionary under the hood during the loop and leading to that message. You can verify this by changing

foreach(Subscriber s in subscribers.Values)

To

foreach(Subscriber s in subscribers.Values.ToList())

If I'm right, the problem will disappear.

Calling subscribers.Values.ToList() copies the values of subscribers.Values to a separate list at the start of the foreach. Nothing else has access to this list (it doesn't even have a variable name!), so nothing can modify it inside the loop.

Disable drag and drop on listboxitem in a bound listbox; Collection was modified exception

Seems that binding the IsSelected to my model was causing the issue. Removing the binding fixed it and doesn't impact on the UI.

Remove item from Observable Collection in Nested Foreach

You need to add 'ToList' statements if you want to remove items in the collection :

foreach (var item1 in ocChoicesinItem.ToList())
{
foreach (var item2 in temp.ItemsInInvoiceChoices)
{
if (item1.ChoicesId == item2.ChoicesId)
ocChoicesinItem.Remove(item1);
}
}


Related Topics



Leave a reply



Submit