Click here to Skip to main content
12,550,731 members (51,013 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET
How to store List object in session and how to retrieve that session?

List<string> objList = new List<string>();
objList.Add("Sri Lanka");
Session["Key"] = objList;
List<string> objlt = (List<string>)Session["Key"];
var list = (List<string>)Session["Key"];
for (int i = 0; i < objlt.Count; i++)

This code works for Arraylist but for list it's throwing an exception.

Here iam getting error as:
Object reference not set to an instance of an object.
Posted 8-Jan-13 4:27am
Updated 8-Jan-13 5:11am
Vani Kulkarni 10-Jan-13 1:52am
Your code works fine. I don't get any Object Reference error.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Ensure that the code written in section 1 where you set them in session gets executed first then the code for retrieving values from session is executed.
You might have placed the code in the wrong places, check the page events.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

see what is happening here.arraylist is a non generic collection so any type of data can be added and retrieved using its index.

but the list is generic collection so it need to have a specific type and even at the time of retrieval we need to know the type of data to be retrieved.

for the process which you are using.please use some class with the specific property and then add that property to your list.

so in this way ,you will get your data back like

public class mydata
public string country{get;set;}
public List<mydata> objList{get;set;}

now in list--

mydata obj=new mydata();
obj.objList=new List<mydata>();
obj.objList.add(new mydata{country="India"});

now assign this to session


for retrieval--

mydata objlt = (mydata)Session["Key"];
for (int i = 0; i <objlt.objlist.count;>{

please excuse my type mistake and the above code is just my suggestion.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

you can use a property...

// List in session
public List<string> SessionList
        var obj = this.Session["myList"];
        if(obj==null) { obj = this.Session["myList"] = new List<string>(); }
        return (List<string>)obj;
        this.Session["myList"] = value;

your code becomes

// part 1
this.SessionList.Add("Sri Lanka");

// part 2
foreach ( string s in this.SessionList)

in the GET part you can see if the session is null a new list is valorized and assigned, that avoid null references.

in case you reach the code for ddl binding before valorizing the session i think you could get the null reference..

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161021.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100