Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
"Collection was modified; enumeration operation may not execute " Why this error is coming when execute my below code:

This is my Window Form Application in .Net

1.I am using a Global class for reading instance message and shown it to a Textbox
2.It is executing fine but some time showing error like "Collection was modified; enumeration operation may not execute " in second foreach looping like
C#
foreach (var values in dict[item])
                   {


3. My global Class is in which i am storing value in Dictionary object and retrived from that .
C#
public static class GlobalVariables  
    {  
            
       public static Dictionary<string,>> dictionary = new Dictionary<string,>>();
                
        public static void SetMessage(String DBName, String Message)
        {                       
                  

            if (dictionary.ContainsKey(DBName))
            {                
                //dictionary.Remove(DBName);
                List<string> list = new List<string>();
                list.Clear();
                list.Add(Message);
                dictionary[DBName]=list;          
                   
            }
            else
            {
                
                    List<string> list = new List<string>();
                    list.Add(DateTime.Now.ToString());
                    dictionary.Add("Process Started",list);
                

            }          
                       
            
        }   
                           
    }

4.Here we can show the message in textbox from Dictionary object and error shown in the second foreach line like
C#
foreach (var values in dict[item])
                    {



C#
public void setTextBoxValue()
      {
          //Thread.Sleep(1000);
          Dictionary<string, List<string>> dict = GlobalVariables.dictionary;
            //string strMessage = Convert.ToString(GlobalVariables.message);

            if (dict.Keys.Count > 0)
            {
                builder.Remove(0, builder.Length);

                foreach (var item in dict.Keys)
                {

                    foreach (var values in dict[item])
                    {

                       string message = item + ":" + values.ToString();

                       builder.AppendLine(message);

                       builder.Append("\n\n");

                      textBox1.Text = builder.ToString();


                    }


                }
                Application.DoEvents();

            }


        }
Posted
Updated 9-Aug-12 2:24am
v3

1 solution

Your question is a bit of a mess so it is difficult to be certain what is happening and when, but I suspect you are adding or removing an item from your list within the foreach loop. That is what the error message is telling you.
 
Share this answer
 
Comments
Kamalkant(kk) 9-Aug-12 8:39am    
Hi Sebastian, I am adding message into my dictionary as well as to list of Global class "GlobalVariables " ,When I am reading from them and shown into my text by using setTextBoxValue() method it will shown the above error.
Richard MacCutchan 9-Aug-12 8:48am    
1. My name is not Sebastian.
2. I have explained what you are (probably) doing wrong.
3. From what you have pasted above it is difficult to see where this is happening, use your debugger or eyes to check the code.
Kamalkant(kk) 9-Aug-12 8:55am    
problem in foreach loop ,when reading
Richard MacCutchan 9-Aug-12 12:49pm    
Yes, that's what I was suggesting. So now it's up to you to find the place where you are changing it and fix the bug.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900