is it possible to store list to session variable in Asp.net C# ?
6 Answers
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.Add(something);
As Richard points out, you should take extra care if you are using other session state modes (e.g. SQL Server) that require objects to be serializable.
-
6Not exactly true. If you need to use a different session store than the default in proc, the class will need to be serializable. That's why it's best to mark any classes you shove in session as Serializable from the beginning- then you can change the mechanism without having to edit your code. Aug 11, 2009 at 12:09
-
Valid point but 1.
List<T>
is serializable ifT
is serializable. 2. The default session state mode is in-proc. Aug 11, 2009 at 12:22 -
@Mehrdad- I get what you meant and it is good you have a code sample. I'd just reword the "any object" in your answer. Aug 11, 2009 at 12:35
-
-
I get such error: Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode. Oct 26, 2016 at 14:01
Yes. Which platform are you writing for? ASP.NET C#?
List<string> myList = new List<string>();
Session["var"] = myList;
Then, to retrieve:
myList = (List<string>)Session["var"];
I found in a class file outside the scope of the Page, the above way (which I always have used) didn't work.
I found a workaround in this "context" as follows:
HttpContext.Current.Session.Add("currentUser", appUser);
and
(AppUser) HttpContext.Current.Session["currentUser"]
Otherwise the compiler was expecting a string when I pointed the object at the session object.
-
I get such error: System.Web.HttpException: Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode Oct 26, 2016 at 14:03
-
I get
'HttpContextBase' does not contain a definition for 'Current' and no extension method 'Current' accepting a first argument of type 'HttpContextBase' could be found (are you missing a using directive or an assembly reference?
– vapcguyOct 26, 2018 at 6:12
Try this..
List<Cat> cats = new List<Cat>
{
new Cat(){ Name = "Sylvester", Age=8 },
new Cat(){ Name = "Whiskers", Age=2 },
new Cat(){ Name = "Sasha", Age=14 }
};
Session["data"] = cats;
foreach (Cat c in cats)
System.Diagnostics.Debug.WriteLine("Cats>>" + c.Name); //DEBUGGG
public class ProductList
{
public string product{get;set;}
public List<ProductList> objList{get;set;}
}
ProductList obj=new ProductList();
obj.objList=new List<ProductList>();
obj.objList.add(new ProductList{product="Football"});
now assign obj to session
Session["Product"]=obj;
for retrieval of session.
ProductList objLst = (ProductList)Session["Product"];
-
-
While we appreciate you trying to answer questions, a completely solved decade-old question doesn't really need another answer unless there are groundbreaking changes. Nov 1, 2019 at 14:19