troubleshooting Question

C# runtime error

Avatar of brandon-shelton
brandon-sheltonFlag for United States of America asked on
Editors IDEsC#
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);
        }

Open in new window

ASKER CERTIFIED SOLUTION
pvginkel

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 3 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 3 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004