Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2006

Tip: Looking for answers? Try searching our database.

Hashtable.Add fails

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jochen Wezel - 27 Jul 2006 09:09 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.