brandon-shelton
asked on
C# runtime error
My program compiles correctly. However, when I run the program I get an error:
System.InvalidOperationExc eption was unhandled
Message="Collection was modified; enumeration operation may not execute."
Source="mscorlib"
StackTrace:
at System.Collections.Hashtab le.Hashtab leEnumerat or.MoveNex t()
at Nova.DataBank.StoreClient( ClientMana gement client) in C:\Users\TFSSETUP\Desktop\ C# Projects\Nova\CodeFile1.cs :line 396
at Nova.ClientAccount.EditCli ent() 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._nExecute Assembly(A ssembly assembly, String[] args)
at System.AppDomain.ExecuteAs sembly(Str ing assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.Hos tingProces s.HostProc .RunUsersA ssembly()
at System.Threading.ThreadHel per.Thread Start_Cont ext(Object state)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHel per.Thread Start()
InnerException:
Help! I'm not certain where to start. It only seems to do this after I select the edit option in this program.
System.InvalidOperationExc
Message="Collection was modified; enumeration operation may not execute."
Source="mscorlib"
StackTrace:
at System.Collections.Hashtab
at Nova.DataBank.StoreClient(
at Nova.ClientAccount.EditCli
at Nova.Main() in C:\Users\TFSSETUP\Desktop\
at System.AppDomain._nExecute
at System.AppDomain.ExecuteAs
at Microsoft.VisualStudio.Hos
at System.Threading.ThreadHel
at System.Threading.Execution
at System.Threading.ThreadHel
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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Works like a charm!
Thank you again for the help.
ASKER
Thanks again
Open in new window