Link to home
Start Free TrialLog in
Avatar of brandon-shelton
brandon-sheltonFlag for United States of America

asked on

C# runtime error

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);
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of pvginkel
pvginkel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brandon-shelton

ASKER

Thanks for the quick response.  Thank you for your help.  I think I'll go with something like what I have below.  Let me know if you see any problem with that (I'm not at my computer at the moment so I can't test it).
Thanks again

 public void StoreClient(ClientManagement client)
        {
            Hashtable tempHashtable = new Hashtable();
            foreach (ClientManagement cl in clientHashtable.Values)
            {
                if (cl.GetGuid() == client.GetGuid() || (clientHashtable[client.GetName()] != null))
                {
                    tempHashtable.Add(cl.GetName(),cl);
                }
            }
            if (tempHashtable.Count > 0)
            {
                foreach (ClientManagement cl in tempHashtable.Values)
                {
                    clientHashtable.Remove(cl.GetName());
                }
            }
            clientHashtable.Add(client.GetName(), client);
        }

Open in new window

OK just remimbered that I left remote admin software running and the port was open on the router from something that I had done before.  So, I was able to try the new code. :)
Works like a charm!
Thank you again for the help.