Click here to Skip to main content
16,017,954 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
How to store List object in session and how to retrieve that session?


C#
List<string> objList = new List<string>();
objList.Add("India");
objList.Add("Sri Lanka");
objList.Add("Zeebra");
Session["Key"] = objList;

C#
List<string> objlt = (List<string>)Session["Key"];
var list = (List<string>)Session["Key"];
for (int i = 0; i < objlt.Count; i++)
{
    DropDownList1.Items.Add(objlt[i].ToString());
}


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
Updated 8-Jan-13 5:11am
v4
Comments
Vani Kulkarni 10-Jan-13 1:52am    
Your code works fine. I don't get any Object Reference error.

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.
 
Share this answer
 
hi,
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

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


now in list--

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


now assign this to session

C#
Session["Key"]=obj;


for retrieval--

C#
mydata objlt = (mydata)Session["Key"];

for (int i = 0; i <objlt.objlist.count;>{
DropDownList1.Items.Add(objlt.objList[i].country);
}


please excuse my type mistake and the above code is just my suggestion.
 
Share this answer
 
you can use a property...

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

    set
    {
        this.Session["myList"] = value;
    }
}


your code becomes

C#
// part 1
this.SessionList.Add("India");
this.SessionList.Add("Sri Lanka");
this.SessionList.Add("Zeebra");


// part 2
DropDownList1.Items.Clear();
foreach ( string s in this.SessionList)
{
    DropDownList1.Items.Add(s);
}


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..
 
Share this answer
 
v6

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