Click here to Skip to main content
15,887,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to store class,arrays and List in session.Then retrieve them back from session in asp.net c#.Help guys!

What I have tried:

Give me an example guys.!In asp.net c#.I don't know how to do this.Googled many times but didn't get exact results.
Posted
Updated 24-Apr-17 1:20am

Assuming you have an array of your class elements:
C#
MyClass[] mca = new MyClass[100];
for (int i = 0; i < 100; i++)
   {
   mca[i] = new MyClass(i);
   }
Then storing it into the Session is trivial:
C#
Session["MyArray"] = mca;
And retrieving it is not complex either:
MyClass[] mca = Session["MyArray"] as MyClass[];
if (mca != null)
   {
   ...
   }
Lists, arrays, class instances - all the same technique.
 
Share this answer
 
v2
Comments
GrpSMK 24-Apr-17 7:32am    
Thank you
OriginalGriff 24-Apr-17 7:56am    
You're welcome!
List obj = new List();
obj.Add(1)

Session["Session"] = obj;

and typecast back to list where it requires.

same can be done for any complex object.
 
Share this answer
 
Comments
GrpSMK 24-Apr-17 7:31am    
how to do for array?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900