Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Exception In Hashtable with arrayList.

Exception In Hashtable with arrayList.

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdatabasedata-structureshelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    r aa j
    wrote on last edited by
    #1

    I am developing website using C# with ASP.net. one page I am inserting data into hashtable after reach 1000(>1000),Add it into Array list than Insert into Database...here sometimes saying Exception like Item has already been added. Key in dictionary: '12' Key being added: '12' ...Please help me with this.....How to avoid this Eception... and I am using following code.. public static Hashtable ht = new Hashtable(); public string Crid=""; Crid = ht.Count.ToString(); string[] st = new string[4]; st[0] = Crid; st[1] = "5654546456"; st[2] = "0"; st[3] = "1"; if (ht.Count<1000) { ht.Add(ht.Count, st); } else { ArrayList list = new ArrayList(); foreach (int key in ht.Keys) { list.Add(ht[key]); } ht.Clear(); ht = new Hashtable(); ht.Add(ht.Count, st); for (int j = 0; j < list.Count; j++) { //here Inserting data into Database } }

    Raaj

    C G 2 Replies Last reply
    0
    • R r aa j

      I am developing website using C# with ASP.net. one page I am inserting data into hashtable after reach 1000(>1000),Add it into Array list than Insert into Database...here sometimes saying Exception like Item has already been added. Key in dictionary: '12' Key being added: '12' ...Please help me with this.....How to avoid this Eception... and I am using following code.. public static Hashtable ht = new Hashtable(); public string Crid=""; Crid = ht.Count.ToString(); string[] st = new string[4]; st[0] = Crid; st[1] = "5654546456"; st[2] = "0"; st[3] = "1"; if (ht.Count<1000) { ht.Add(ht.Count, st); } else { ArrayList list = new ArrayList(); foreach (int key in ht.Keys) { list.Add(ht[key]); } ht.Clear(); ht = new Hashtable(); ht.Add(ht.Count, st); for (int j = 0; j < list.Count; j++) { //here Inserting data into Database } }

      Raaj

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Isn't it obvious ? Don't add a key that is already there. So, check if the key exists and if it does, decide if you want to change the value that's there, or leave it.

      Christian Graus Driven to the arms of OSX by Vista.

      1 Reply Last reply
      0
      • R r aa j

        I am developing website using C# with ASP.net. one page I am inserting data into hashtable after reach 1000(>1000),Add it into Array list than Insert into Database...here sometimes saying Exception like Item has already been added. Key in dictionary: '12' Key being added: '12' ...Please help me with this.....How to avoid this Eception... and I am using following code.. public static Hashtable ht = new Hashtable(); public string Crid=""; Crid = ht.Count.ToString(); string[] st = new string[4]; st[0] = Crid; st[1] = "5654546456"; st[2] = "0"; st[3] = "1"; if (ht.Count<1000) { ht.Add(ht.Count, st); } else { ArrayList list = new ArrayList(); foreach (int key in ht.Keys) { list.Add(ht[key]); } ht.Clear(); ht = new Hashtable(); ht.Add(ht.Count, st); for (int j = 0; j < list.Count; j++) { //here Inserting data into Database } }

        Raaj

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        If you are using a static variable in a web application, you have to synchronise all access to it. The reason for the error is that two users are adding items at the same time. You are lucky that you got such a descriptive error message, you could just as well have corrupted the HashTable and got some internal error in the HashTable code... Use a List instead of a HashTable. You are only using the key of the HashTable as an index anyway. Instead of copying the items one by one into a new collection, just keep them in the collection where they are.

        Despite everything, the person most likely to be fooling you next is yourself.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups