Exception In Hashtable with arrayList.
-
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
-
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
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.
-
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
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.