3

I know there are topics with similar heading, but this is kinda different.

First, in order to store multiple values in a single session, I have to use a List whereas I store the list with the values in the session, right ?

If so, when I want to add a value to the List, which is already in the session, then I retrieve the List from the session and add the value. But do I need to assign the List back to the session every time I have added/removed a value from the List? Or ,by default, it gets automatically updated in the session when I manipulate it as it was assigned at first in the session and after that.

UPDATE: providing sample code of my question

public void assignNewID(int currentID)
{
    if(Session["usersID"] == null)
    {
        Session["usersID"] = new List<int>();
    }

    if(currentID != null)
    {
        var list = (List<int>)Session["usersID"];
        list.Add(currentID);

        // now should I hereby assign the list back to
        // the session like:
        // Session["usersID"] = list;
        // or it gets automatically updated in the session by default ?
    }
}
2
  • Make it more precise by posting the actual List declaration and the add-to-Session code. Commented Aug 26, 2013 at 8:16
  • @HenkHolterman - I updated my post with sample code. :) Commented Aug 26, 2013 at 8:31

4 Answers 4

7

List is a reference type so you are storing a reference to your session which will be updated if the object gets updated ,

Session gets updated for reference type

Sign up to request clarification or add additional context in comments.

Comments

4

There is a catch here.

Your code is OK, no need to assign the list back (not that that would be much trouble).

But only as long as you run this on 1 Server ( <sessionstate mode="inproc" />).

If you scale this up to multiple servers you will run into the problem that List<> is not marked as [Serializable]. Your solution won't directly work.

A short fix would be to use:

[Serializable]
class MyList : List<int>
{
}

Comments

2

Try this:

// Saving in session
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            ht.Add("EmployeeName", "EmpName Value");
            ht.Add("Designation", "Designation Value");
            ht.Add("Department", "Department Value");
            Session["EmployeeInfo"] = ht;
            //Retrieve from session
            if (Session["EmployeeInfo"] != null)
            {
                string strEmployeeName = ht.ContainsKey("EmployeeName") ? Convert.ToString(ht["EmployeeName"]) : "";
                string strDesignation = ht.ContainsKey("Designation") ? Convert.ToString(ht["Designation"]) : "";
                string strDepartment = ht.ContainsKey("Department") ? Convert.ToString(ht["Department"]) : "";
            }

Comments

0

See this example

public static class SessionManager
{
    public static List<Entity.Permission> GetUserPermission
    {
        get
        {
            if (HttpContext.Current.Session["GetUserPermission"] == null)
            {
                //if session is null than set it to default value
                //here i set it 
                List<Entity.Permission> lstPermission = new List<Entity.Permission>();
                HttpContext.Current.Session["GetUserPermission"] = lstPermission;
            }
            return (List<Entity.Permission>)HttpContext.Current.Session["GetUserPermission"];
        }
        set
        {
            HttpContext.Current.Session["GetUserPermission"] = value;
        }
    }
 }

now in any event

    protected void chkStudentStatus_CheckedChanged(object sender, EventArgs e)
    {
        Entity.Permission objPermission=new Entity.Permission();
        SessionManager.GetUserPermission.Add(objPermission);
        SessionManager.GetUserPermission.RemoveAt(0);


    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.