简体   繁体   中英

Storing and retrieving multiple values in a single session

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 ?
    }
}

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

会话更新为参考类型

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>
{
}

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"]) : "";
            }

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);


    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM