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

Editors IDEsC#

Avatar of undefined
Last Comment
brandon-shelton
ASKER CERTIFIED SOLUTION
Avatar of pvginkel
pvginkel
Flag of Netherlands image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
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

Avatar of brandon-shelton

ASKER

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.
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo