Store hashtable in a session
How can I store HashTable collection to a Session variable in .NET. Unfor开发者_运维百科tunately I'm not able to store HashTable nor Dictionary.
Please help me in this.
I got the answer.. There was no problem with the code ie new keyword. In the above step after storing to Session i was clearing the HashTable items.
Hashtable ht = new Hashtable();
if(condition){
ht.Add("id", 1);
ht.Add("name", "ram");
Session["hashtab"] = ht;
}
ht.Clear();
this ht.Clear() was the problem. I removed it and have no issue with it. It was like this. when i removed that code its working fine for me.
I don't know why can't you able to store hashtable into session and get it back from session since it is as simple as storing table or any object in session. If i understood your problem correctly I can provide solution as below.
Do like this in the page where you want to store the hashtable into session.
Hashtable ht = new Hashtable();
ht.Add("id", 1);
ht.Add("name", "ram");
Session["hashtab"] = ht;
and get the hashtable in other page as below.
Hashtable ht = new Hashtable(); ht =(Hashtable)Session["hashtab"];
精彩评论