locked
how do I create a session variable for a list of class instance RRS feed

  • Question

  • User-1525562971 posted

    here is the declaration

    List<AlphaNumericKeyValuePairs> AlphaNumericKeyValue = new List<AlphaNumericKeyValuePairs>();


    How do I set it up as a session variable?

    Note: I have followed examples out there, but the issue seems to be that the

    list is of a class instance (as opposed to a primitive type - for example a list of type string)

    Thanks

    Gord

    Tuesday, September 6, 2011 11:52 AM

Answers

  • User-1952482173 posted

    Session["KeyValue"] = AlphaNumbericKeyValue;

    //Get the session value back to the list

    List<AlphaNumbericKeyValuePairs> newValue = (List<AlphaNumbericKeyValuePairs>)Session["KeyValue"]);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 6, 2011 1:32 PM
  • User-481631678 posted
            //Here is how you can store data in Session Variable
            List<MyEntity> MyList = new List<MyEntity>();
            
            MyEntity ps = new MyEntity();
            ps.FirstName = "Zeeshan";
            ps.LastName = "umar";
            MyList.Add(ps);
    
            Session["MyList"] = MyList;
    
            //Here is how you read information from session
            MyList = Session["MyList"] as List<MyEntity>;
    
            if (MyList != null)//Always add null check before reading data from session
            {
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 7, 2011 5:44 AM

All replies

  • User-1952482173 posted

    Session["KeyValue"] = AlphaNumbericKeyValue;

    //Get the session value back to the list

    List<AlphaNumbericKeyValuePairs> newValue = (List<AlphaNumbericKeyValuePairs>)Session["KeyValue"]);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 6, 2011 1:32 PM
  • User1613327603 posted

    Hello,

    // store in session

    List<AlphaNumericKeyValuePairs> AlphaNumericKeyValue = new List<AlphaNumericKeyValuePairs>();
    Session["AlphaKey"] = AlphaNumericKeyValue;

    //retrive from session

    if(Session["AlphaKey"]  != null)

    {

       List<AlphaNumericKeyValuePairs> AlphaNumericKeyValue = (List<AlphaNumericKeyValuePairs>)Session["AlphaKey"];

    }

    Thanks,

    Jayesh Goyani

    Wednesday, September 7, 2011 2:13 AM
  • User-204486215 posted

    Do it like this...

    List<AlphaNumericKeyValuePairs> AlphaNumericKeyValue = new List<AlphaNumericKeyValuePairs>();
    Session["alphaobject"] = AlphaNumericKeyValue ; //set

    List<AlphaNumericKeyValuePairs> AlphaNumericKeyValue = (List<AlphaNumericKeyValuePairs>)Session["alphaobject"];//get
    Wednesday, September 7, 2011 5:35 AM
  • User-481631678 posted
            //Here is how you can store data in Session Variable
            List<MyEntity> MyList = new List<MyEntity>();
            
            MyEntity ps = new MyEntity();
            ps.FirstName = "Zeeshan";
            ps.LastName = "umar";
            MyList.Add(ps);
    
            Session["MyList"] = MyList;
    
            //Here is how you read information from session
            MyList = Session["MyList"] as List<MyEntity>;
    
            if (MyList != null)//Always add null check before reading data from session
            {
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 7, 2011 5:44 AM
  • User-1525562971 posted

    Thanks everyone for the responses. I will try to apply them to my code

    Gord

    Wednesday, September 7, 2011 1:51 PM