troubleshooting Question

C# runtime error

Avatar of brandon-shelton
brandon-sheltonFlag for United States of America asked on
C#Editors IDEs
3 Comments1 Solution532 ViewsLast Modified:
My program compiles correctly. However, when I run the program I get an error:
System.InvalidOperationException was unhandled
  Message="Collection was modified; enumeration operation may not execute."
  Source="mscorlib"
  StackTrace:
       at System.Collections.Hashtable.HashtableEnumerator.MoveNext()
       at Nova.DataBank.StoreClient(ClientManagement client) in C:\Users\TFSSETUP\Desktop\C# Projects\Nova\CodeFile1.cs:line 396
       at Nova.ClientAccount.EditClient() in C:\Users\TFSSETUP\Desktop\C# Projects\Nova\CodeFile1.cs:line 372
       at Nova.Main() in C:\Users\TFSSETUP\Desktop\C# Projects\Nova\CodeFile1.cs:line 567
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:


Help!  I'm not certain where to start. It only seems to do this after I select the edit option in this program.
public static void LoadData(string filename)
        {
            if (data.Load(filename))
            {
                Console.WriteLine("Loading Data Worked!");
            }
        }
 
        public bool LoadClient(string name)
        {
            try
            {
                client = new ClientManagement(data.FindClient(name).GetName(), data.FindClient(name).GetPhone(), data.FindClient(name).GetAddress(), data.FindClient(name).GetProductNumber(), data.FindClient(name).GetGuid());
                Console.Clear();
                Console.WriteLine("Running from LoadClient:\nName:\t" + client.GetName() + "\nPhone:\t" + client.GetPhone() +  "\nAddress:" +client.GetAddress()+"\nGUID:\t" + client.GetGuid());
            }
            catch
            {
                Console.WriteLine("Client not found.\n\n");
                return false;
            }
            return true;
        }
 
        public void EditClient()
        {
            ConsoleKeyInfo readKey;
            bool edit = true;
            try
            {
                while (edit)
                {
                    Console.Clear();
                    Console.WriteLine("Current Information:\nName:\t" + client.GetName() + "\nPhone:\t" + client.GetPhone() + "\nAddress:" + client.GetAddress() + "\n" + "GUID:\t:" + client.GetGuid() + "\n\n");
                    Console.WriteLine("Select What you would like to edit:\t(Press <E> to exit)\n<N>ame\t<P>hone\t<A>ddress\n\n");
                    readKey = Console.ReadKey();
 
                    switch (readKey.Key)
                    {
                        case ConsoleKey.N:
                            Console.Write("ame\nCurrent Name: " + client.GetName() + "\n");
                            Console.Write("Enter the new name: ");
                            client.SetName(Console.ReadLine());
                            break;
 
                        case ConsoleKey.P:
                            Console.Write("one\nCurrent phone number: " + client.GetPhone() + "\n");
                            Console.Write("Enter the new phone number: ");
                            client.SetPhone(Console.ReadLine());
                            break;
 
                        case ConsoleKey.A:
                            Console.Write("ddress\nCurrent address: " + client.GetAddress() + "\n");
                            Console.Write("Enter the new address: ");
                            client.SetAddress(Console.ReadLine());
                            break;
                        case ConsoleKey.E:
                            Console.Clear();
                            edit = false;
                            break;
                        /* default:
                             Console.WriteLine("\nInvalid Command");
                             break;*/
                    }
                }
 
                Console.WriteLine("Edited Information:\nName:\t" + client.GetName() + "\nPhone:\t" + client.GetPhone() + "\nAddress:" + client.GetAddress() + "\nGUID:\t" + client.GetGuid());
                /*Console.ReadLine();
                data.StoreClient(client); 
                Console.ReadLine();*/
            }
            catch
            {
                Console.Clear();
                Console.WriteLine("No Client Loaded.\n");
            }
            finally
            {
                data.StoreClient(client); /*this is the part that seems to give me the problem it runs fine without this, however, It doesn't save any data*/
            }
        }
 
 
 
 
 
 
class DataBank : IClientData
    {
        Hashtable clientHashtable = new Hashtable();
        Hashtable productHashtable = new Hashtable();
 
        public ClientManagement FindClient(string name)
        {
            return clientHashtable[name] as ClientManagement;
        }
 
        public void StoreClient(ClientManagement client)
        {
 
            foreach (ClientManagement cl in clientHashtable.Values)/*the "in" in this line is where it gives me the Exception unhandled error*/
            {
                if (cl.GetGuid() == client.GetGuid() || (clientHashtable[client.GetName()] != null))
                {
                    RemoveClient(cl.GetName());
                }
            }
 
            clientHashtable.Add(client.GetName(), client);
        }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros