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
Barry Kelly - 27 Jul 2006 10:07 GMT
> 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

Signature
http://barrkel.blogspot.com/
Jochen Wezel - 27 Jul 2006 10:36 GMT
(What) Do I have to do something to synchronize the collection? What does it
means?
> > 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
[quoted text clipped - 12 lines]
>
> -- Barry
Laura T - 27 Jul 2006 10:54 GMT
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)
}
}
}
}
> (What) Do I have to do something to synchronize the collection? What does
> it
[quoted text clipped - 21 lines]
>>
>> -- Barry
Jochen Wezel - 27 Jul 2006 11:44 GMT
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
> Like this:
>
[quoted text clipped - 41 lines]
> >>
> >> -- Barry
Jochen Wezel - 27 Jul 2006 11:55 GMT
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
> I'm using a construct like the following in my ASP.NET page:
>
[quoted text clipped - 52 lines]
> > >>
> > >> -- Barry