Display using hashtable

Posted by Klbaiju under ASP.NET on 3/7/2011 | Points: 10 | Views : 4663 | Status : [Member] | Replies : 4
I want to display fields of hashtable using response.write
this showing error

out put should be
1 Hellow
2 World
3 Hellow

how it is possible
Regards
K L BAIJU

Hashtable myHash = new Hashtable();


myHash.Add("1", "Hello");

myHash.Add("2", "World");

myHash.Add("3", "Hello");


foreach (DictionaryEntry de in myHash)
{
Response.Write(de.Value+,de.Key + +"<br>");



}




Responses

Posted by: T.saravanan on: 3/7/2011 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Try this....
Hashtable myHash = new Hashtable();

myHash.Add("1", "Hello");
myHash.Add("2", "World");
myHash.Add("3", "Hello");

foreach (DictionaryEntry de in myHash)
{
Response.Write(de.Value+","+de.Key +"<br/>");
}


But result will display in Last in First Out(LIFO) order.
If you using SortedList means its automatically sort the value and display in ascending order.

Cheers :)


Thanks,
T.Saravanan

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gsolvers on: 3/7/2011 [Member] Starter | Points: 25

Up
0
Down
Issue is in your response statement
Response.Write(de.Value+,de.Key + +"<br>");
You need to rewrite it as
Response.Write(de.Value +"," + de.Key + "<br>");



Best Regards,

VG
www.TeacherJi.com

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 3/7/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hashtable hash = new Hashtable();

hash.Add("1", "Hello");
hash.Add("2", "Dotnet");
hash.Add("3", "funda");

List<int> lst = new List<int>();

foreach (var key in hash.Keys)
{
lst.Add(int.Parse(key.ToString()));
}

lst.Sort();

foreach (var item in lst)
{
Console.WriteLine(string.Format("{0},{1}", item, hash[item.ToString()]));
}

Console.Read();


this works

Thanks and Regards
Madhu

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinay13mar on: 10/27/2012 [Member] Starter | Points: 25

Login to post response