Hashtable.Add fails

G

Guest

Hi all,

I want to assign a new key value to my hash table (okay, I use the
Specialized.NameValueCollection, but this collection is based again on the
HashTable). When the key already exists, then the value of the key shall be
replaced. That's why I use the Add method which also replaces key values
instead of Insert which fires exceptions when a key already exist.

Now, I've got a very strange exception:
----SNIP----
System.ArgumentException: Das Element wurde bereits hinzugefügt. Schlüssel
im Wörterbuch: "color_anchor_NavHeader2_all" Hinzuzufügender Schlüssel:
"color_anchor_NavHeader2_all"
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
at System.Collections.Hashtable.Add(Object key, Object value)
at System.Collections.Specialized.NameObjectCollectionBase.BaseAdd(String
name, Object value)
at System.Collections.Specialized.NameObjectCollectionBase.BaseSet(String
name, Object value)
at System.Collections.Specialized.NameValueCollection.Set(String name,
String value)
at System.Collections.Specialized.NameValueCollection.set_Item(String name,
String value)
at
BOMAG.CorporateWebSite.DealerCustomizing.LoadDealerConfigurationSetting(String propertyName, DealerLookupModes dealerLookupMode)
at _ASP.style_aspx.__Render__control1(HtmlTextWriter __output, Control
parameterContainer)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Control.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Page.ProcessRequestMain()
----/SNIP----

The add method is called, it identifies the key as missing and that's why it
calls the Insert method internally. Then, the Insert method wants to insert
the key - but by whatever reason, the insert fails because the key already
exist. Somewhere between the execution of the IF in the Add method and the
key-insertion in Insert, the key must have been created.

So, I can only imaging that another process (very normal for web solutions)
might be more fast and inserted the key value in the meanwhile.

Is this a bug or is it a feature?

Thanks for any advise!

Regards
Jochen
 
B

Barry Kelly

Jochen Wezel said:
I want to assign a new key value to my hash table (okay, I use the
Specialized.NameValueCollection, but this collection is based again on the
HashTable). When the key already exists, then the value of the key shall be
replaced. That's why I use the Add method which also replaces key values
instead of Insert which fires exceptions when a key already exist.
at System.Collections.Specialized.NameValueCollection.set_Item(String name,
String value)
at
BOMAG.CorporateWebSite.DealerCustomizing.LoadDealerConfigurationSetting(String propertyName, DealerLookupModes dealerLookupMode)
at _ASP.style_aspx.__Render__control1(HtmlTextWriter __output, Control
parameterContainer)

Are you synchronizing access to the name value collection?

-- Barry
 
L

Laura T

Like this:

class HashTest
{
object hashLock=new object(); // synchronization object for serialized
hashtable access
HashTable myTable=new HashTable(); // the table itself

public void AddItem(string Key,object Value)
{
lock(hashLock) // Synchronize - 1 thread at a time
{
if(myTable.ContainsKey(Key)==false)
{
myTable.Add(Key,Value)
}
}
}
}
 
G

Guest

I'm using a construct like the following in my ASP.NET page:

// Add item to cache
Cache["myKey"] = myValue;

Shouldn't the ASP.NET engine prepare the cache object by itself for
multiple, parallel access?

Regards
Jochen
 
G

Guest

Sorry, last posting was not correct.

I'm using a NameValueCollection stored in the Cache. And this
NameValueCollection doesn't run synchronouly.

So, I'd better write (VB.NET style):
Application.Lock
CType(Cache("mycachekey"), NameValueCollection)("mykey") = myValue
Application.Unlock


Jochen Wezel said:
I'm using a construct like the following in my ASP.NET page:

// Add item to cache
Cache["myKey"] = myValue;

Shouldn't the ASP.NET engine prepare the cache object by itself for
multiple, parallel access?

Regards
Jochen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top