Click here to Skip to main content
Rate this: bad
good
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("India");
            objList.Add("Sri Lanka");
            objList.Add("Zeebra");
            Session["Key"] = objList;
 
 
           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 8-Jan-13 4:27am
Edited 8-Jan-13 5:11am
v4
Comments
Vani Kulkarni at 10-Jan-13 1:52am
   
Your code works fine. I don't get any Object Reference error.
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
 
public class mydata
{
public string country{get;set;}
public List<mydata> objList{get;set;}
}</mydata>
 
now in list--
 
 
mydata obj=new mydata();
obj.objList=new List<mydata>();
obj.objList.add(new mydata{country="India"});
</mydata>
 
now assign this to session
 
Session["Key"]=obj;
 
for retrieval--
 
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

you can use a property...
 
 
        // 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
 
           // 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..
  Permalink  
v6

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

  Print Answers RSS
0 Maciej Los 404
1 OriginalGriff 379
2 CPallini 145
3 Sergey Alexandrovich Kryukov 142
4 Hard_Rockz 115
0 OriginalGriff 5,634
1 Sergey Alexandrovich Kryukov 4,763
2 Peter Leow 2,954
3 Maciej Los 2,614
4 DamithSL 2,465


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