Hashtable Error

asp.net , csharp Alandur, India
  • 8 years 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();
  • 8 years 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
  • 8 years 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).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Owning a computer without programming is like having a kitchen and using only the microwave oven” - Charles Petzold