I have a list that I put in session:
Session.Add("SessionList", mylist);
How to retrieve it back from the session?
Yes, you can store any object (I assume you are using ASP.NET with default settings, which is in-process session state): Session["test"] = myList; You should cast it back to the original type for use: var list = (List<int>)Session["test"]; // list.
In this post I am going to discuss about how you can get list of all active Session Variables in ASP.NET Application. The easiest way to get the details of session variable is using “Tracing” . If you enable the “ Tracing ” for your application, you can get list of all Active Session variables .
var list = Session["SessionList"] as List<whatevertypeYouUsed>;
if (list != null){
// blah...
}
I prefer to use the as
keyword since there is no 100% guarantee that the Session will contain the list (due to application pool refresh, website being restarted, etc). Gives you that extra bit of defence to avoid a NullReferenceException
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With