Community discussion forum

Hashtable Error

  • 1 year ago
    i wanna use the hashtabl concept to fill the dropdownlist and use the hashtable in the selected item change event of the dropdownlist .. but wenever i refresh the page it shows the error "Item has already been added. Key in dictionary: "10" Key being added: "10" " and i chked the databse and i dont have the duplicate keys ... but i still get this error ..can anyonehelp wat cud be the reason and how to solve it and the code snippet is (in the page load event ) OleDbDataAdapter ada = new OleDbDataAdapter("Select * from HOSP",oledb); DataTable Dhosp=new DataTable(); ada.Fill(Dhosp); hHospital2.Clear(); foreach(DataRow row in Dhosp.Rows) { if (hHospital2.Contains(row["HOSPID"].ToString())) { Response.Write(row["HOSPID"].ToString()); } else { hHospital2.Add(row["HOSPID"].ToString()+"~"+row["HOSPADD"],row["HOSPNAME"].ToString()); hLH.Add(row["HOSPNAME"],row["LOCATION"]); } } HospId.DataSource = Dhosp; HospId.DataMember="Hosp"; HospId.DataTextField="HOSPNAME"; HospId.DataValueField="HOSPID"; HospId.DataBind();
    Post was edited on 31/10/2008 10:14:01 Report abuse
  • 1 year ago
    Hi there The Hashtable will only error when it has that key, so we just need to work out why! :) Have you tried checking what value is in the Hashtable with the duplicate key? (you can do that from the Watch window in Visual Studio) Also, you don't seem to be checking for the right condition in your loop? For instance, you're checking if hHospital2.Contains(row["HOSPID"].ToString() but then if it doesn't contain that, you're adding hHospital2.Add(row["HOSPID"].ToString()+"~"+row["HOSPADD"], ... as the key. Are you sure that's what you're wanting to do? Hope that helps
  • 10 months ago
    hashtable http://vb.net-informations.com/collections/vb.net_HashTable.htm

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!