Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store List to session

is it possible to store list to session variable in Asp.net C# ?

like image 731
Avinash Avatar asked Aug 11 '09 12:08

Avinash


People also ask

How do I store a list in 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.

Can we store dataset in session?

Solution 4. Session variable can store large amount of data, including class, dataset.

Can we store object in session in C#?

It means you need persist your session in database at appropriate point. You may use out of process session storage (database or different server) - you have to mark your shopping cart class as serializable in such case. There is performance cost to out-of-process sessions.

Where is session state stored?

Session state can be stored in one of the following modes: In - Process: Stored in the same ASP.Net Process. State Server: Stored in the some other system. SQL Server: Stored in the SQLServer database.


2 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.

like image 82
mmx Avatar answered Oct 05 '22 05:10

mmx


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"]; 
like image 28
Paul McLean Avatar answered Oct 05 '22 07:10

Paul McLean