Home All Groups Group Topic Archive Search About

Hashtable.Add fails

Author
27 Jul 2006 8:09 AM
Jochen Wezel
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

Author
27 Jul 2006 9:07 AM
Barry Kelly
Jochen Wezel <JochenWe***@discussions.microsoft.com> wrote:

> 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

Author
27 Jul 2006 9:36 AM
Jochen Wezel
(What) Do I have to do something to synchronize the collection? What does it
means?

Show quote
"Barry Kelly" wrote:

> Jochen Wezel <JochenWe***@discussions.microsoft.com> wrote:
>
> > 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
>
> --
> http://barrkel.blogspot.com/
>
Author
27 Jul 2006 9:54 AM
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)
                 }
          }
    }
}


Show quote
"Jochen Wezel" <JochenWe***@discussions.microsoft.com> ha scritto nel
messaggio news:43E148CA-55CE-42EF-BCDF-F990D4B96417@microsoft.com...
> (What) Do I have to do something to synchronize the collection? What does
> it
> means?
>
> "Barry Kelly" wrote:
>
>> Jochen Wezel <JochenWe***@discussions.microsoft.com> wrote:
>>
>> > 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
>>
>> --
>> http://barrkel.blogspot.com/
>>
Author
27 Jul 2006 10:44 AM
Jochen Wezel
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



Show quote
"Laura T" wrote:

> 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)
>                  }
>           }
>     }
> }
>
>
> "Jochen Wezel" <JochenWe***@discussions.microsoft.com> ha scritto nel
> messaggio news:43E148CA-55CE-42EF-BCDF-F990D4B96417@microsoft.com...
> > (What) Do I have to do something to synchronize the collection? What does
> > it
> > means?
> >
> > "Barry Kelly" wrote:
> >
> >> Jochen Wezel <JochenWe***@discussions.microsoft.com> wrote:
> >>
> >> > 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
> >>
> >> --
> >> http://barrkel.blogspot.com/
> >>
>
>
>
Author
27 Jul 2006 10:55 AM
Jochen Wezel
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


Show quote
"Jochen Wezel" wrote:

> 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
>
>
>
> "Laura T" wrote:
>
> > 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)
> >                  }
> >           }
> >     }
> > }
> >
> >
> > "Jochen Wezel" <JochenWe***@discussions.microsoft.com> ha scritto nel
> > messaggio news:43E148CA-55CE-42EF-BCDF-F990D4B96417@microsoft.com...
> > > (What) Do I have to do something to synchronize the collection? What does
> > > it
> > > means?
> > >
> > > "Barry Kelly" wrote:
> > >
> > >> Jochen Wezel <JochenWe***@discussions.microsoft.com> wrote:
> > >>
> > >> > 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
> > >>
> > >> --
> > >> http://barrkel.blogspot.com/
> > >>
> >
> >
> >

AddThis Social Bookmark Button