0

I have a class containing two list prop and i want those list element to be in Session. So i did tried the below code. but i didn't how to get the asked property and skipped property of each person object.

class Interrogation
{
    //list contains asked questions
    private List<int> _asked = new List<int>();
    public List<int> asked
    {
        get { return _asked; }
        set { _asked = value; }
    }

    //list contains skipped questions
    private List<int> _skipped = new List<int>();
    public List<int> skipped
    {
        get { return _skipped; }
        set { _skipped = value; }
    }
}
   protected void UploadBtn_Click(object sender, EventArgs e)
   {
        testmulList();
        readSession();
   }
    protected void testmulList()
    {
        Interrogation person1 = new Interrogation();
        person1.asked.Add(8);
        person1.skipped.Add(7) = 67;
        Session["person1"] = person1;
    }
    protected void readSession()
    {
        var output = Session["person1"];
        Debug.WriteLine(output);
    }

When i tried Debug.WriteLine(output) i did not get any output.

Question: 1. How do i read the separate prop from the session object
2. What if i create a object list and perform the same. again leads to question 1.

4
  • You sample makes no sense for ASP.Net app so far - please explain how your class invoked during page rendering. Commented Feb 28, 2014 at 4:37
  • yes i perform this during a button click and i also have a button handler wrapped around this Commented Feb 28, 2014 at 4:39
  • inorder to make the question look simple i cropped the portion of the code involved and displayed in question Commented Feb 28, 2014 at 4:40
  • Does your code even compile? How can you call methods from a class definition. Commented Feb 28, 2014 at 4:41

1 Answer 1

2

The problem is an inconsistency between your session keys

Session["people1"]; vs Session["person1"];

They have to match. Use the same session key

To print you can do the following:

protected void readSession()
    {
        var output = (Interrogation)Session["people1"];

        foreach(var skipped in output.skipped)
        {
           Debug.WriteLine(skipped);
        }

        foreach(var asked in output.asked)
        {
           Debug.WriteLine(asked);
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

oops its just a typo, sorry.
I have added an example of how to print

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.