views:

218

answers:

2

Hello, I'm getting this error: Hashtable insert failed. Load factor too high. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Hashtable insert failed. Load factor too high.

In my code I look for a key in a Dictionary if it's not there I add it. After some research I think the error above is because I'm trying to add the same key twice.

static Dictionary<string, string> settings = new Dictionary<string, string>();

 if ((!settings.ContainsKey(Key)) || (settings[Key] == null)) 
            settings.Add(Key, AltValue);//Changes by Reliance Consulting

Is there a safer way to do this?

thanks!

+3  A: 

You didn't say what framework, but I'm assuming .NET:

This error is almost always caused by multiple threads modifying the Hashtable at the same time. The fix is to insert locks before modifying the Hashtable, since Hashtable isn't multiple writer threadsafe.

For .NET 2.0 ASP.NET environments, there is a hot fix that might solve your problems: FIX: A System.InvalidOperationException exception occurs when you run a Web application that is based in the .NET Framework 2.0 SP2 or in the .NET Framework 3.5 SP1

Mitch Wheat
A: 

They now also have the fix for Windows Server 2003 x32/x64.

Garfield