locked
Session gets overwritten. RRS feed

  • Question

  • User1750060072 posted

    We're building a quizsite where you should be able to answer questions, but also go back and see your selected value in the radiobuttonlist. We're encountering the problem that when you select a value, it does get saved into a sessionvariable, but when you select another value on the next question, this variable gets overwritten and so on. Any ideas of the problem? We have a deadline on thursday and are pretty stuck. Notice that we don't use multiple pages, we use our context to get the next ID in the querystring instead. The answers can be dynamically added to the page so we cannot hardcode a number of sessions in a list.

    Thanks in advance. 

     try
                {         
                    _quizId = int.Parse(Request.QueryString["id"]);
                    int questionNumber = 1;
    
    
                    if (Session["questionNumber"] != null)
                    {
                        questionNumber = (int) Session["questionNumber"];
                        btnPrevQuestion.Visible = true;
                    }
    
                    if (!Page.IsPostBack)
                    {
                        if (_quizId > 0)
                        {
    
                            var question = _service.GetQuestion(_quizId, questionNumber);                       
                            var nextQuestion = _service.GetQuestion(_quizId, questionNumber + 1);
                            var prevQuestion = _service.GetQuestion(_quizId, questionNumber - 1);
    
                            if (question != null)
                            {
                                lblQuestion.Text = question.Text;
                                lblQuizNummer.Text = question.QuestionNumber.ToString();
                                lblQuizTotalNummer.Text = _service.CountQuestion(_quizId).ToString();
    
                                rbListQuiz.DataTextField = "Text";
                                rbListQuiz.DataValueField = "Id";
                                rbListQuiz.DataSource = question.Answers;
                                rbListQuiz.DataBind();
                                
                                Session["questionNumber"] = question.QuestionNumber;
                                if (Session["AnswerValue"] == null)
                                {
                                    Session["AnswerValue"] = rbListQuiz.SelectedValue;
                                }
                                else
                                {
                                    var i = HttpContext.Current.Session["AnswerValue"];
                                    rbListQuiz.SelectedValue = i.ToString();
                                }
    
                            }
    
                            else if (nextQuestion == null)
                            {
                                lblQuestion.Text = "Du har nu svarar på samtliga frågor! <br />" +
                                                   "Tryck på rätta för att rätta dina svar eller tryck ";
                                btnNextQuestion.Visible = false;
                                btnComplete.Visible = true;
                                lblQuizNummer.Visible = false;
                                lblQuizTotalNummer.Visible = false;
                                lblAv.Visible = false;
    
                            }
                            if (prevQuestion == null)
                            {
                                btnPrevQuestion.Visible = false;
                            }
    

     

     

    Monday, January 19, 2015 8:01 AM

Answers

  • User281315223 posted

    Yeah we could use a list or dictionary, but we've gotten instructions to use sessions/cookies. Is there any way to use it and how would that code be builded?

    The Session is capable of storing any type of objects, they would just need to be casted to the appropriate types when being read from the Session. For example, a reading and writing example using a Dictionary within the Session might look like :

    // Check if you have a Dictionary in the Session
    if(Session["Answers"] != null)
    {
        // Something exists within the Session, so try and cast it as a Dictionary<int, string>
        var answers = Session["Answers"] as Dictionary<int,string>;
    
        // Now that you have a Dictionary, you can read the individual values from it, or store new values in it
        var answerOne = answers[1];
    
        // Store another item in the Dictionary
        answers.Add(42, "The Answer to Life, The Universe and Everything");
    
        // Update the Dictionary in the Session
        Session["Answers"] = answers;
    }
    else
    {
        // If it doesn't exist, create it
        Session["Answers"] = new Dictionary<int, string>();
    
        // Similar code to above
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 19, 2015 9:23 AM

All replies

  • User-760709272 posted

    You're re-using the same Session variable so every time someone answers a question, the latest answer will be stored in your Session variable, overwriting the previous one.  You probably need to maintain a List of answers in the session, not just the last answer.

    Monday, January 19, 2015 8:12 AM
  • User281315223 posted

    Have you considered simply storing a data structure like a List or a Dictionary that would keep track of all of your answers? This would be a rather basic way of being able to keep track of all of your specific values as they were answered and give you a mechanism to access previous answers :

    // Store a list of strings or integers in the Session
    List<string> answers = new List<string>();
    
    // Each time the user answers, add the item to the list
    answers.Add(UserAnswer.Text);
    
    // If you need to access an answer, you could simply access it via a query
    var secondAnswer = answers[indexOfQuestion];

    Likewise, you could do the same for a Dictionary that would map your question number (an integer) to an answer (a string) :

    // Store a Dictionary of strings and integers in the Session
    Dictionary<int, string> answers = new List<int, string>();
    
    // Each time the user answers, add the question and response to the Dictionary
    answers.Add(questionNumber, UserAnswer.Text);
    
    // If you need to access an answer, you could simply access it via a query
    var secondAnswer = answers[2];

    These are obviously just examples, but in the case of the List, you could simply check the last item in the list to see what the previous value / answer was.

    Monday, January 19, 2015 8:17 AM
  • User1750060072 posted

    Thank you for your answer. 
    Yeah we could use a list or dictionary, but we've gotten instructions to use sessions/cookies. Is there any way to use it and how would that code be builded?

    Monday, January 19, 2015 9:16 AM
  • User281315223 posted

    Yeah we could use a list or dictionary, but we've gotten instructions to use sessions/cookies. Is there any way to use it and how would that code be builded?

    The Session is capable of storing any type of objects, they would just need to be casted to the appropriate types when being read from the Session. For example, a reading and writing example using a Dictionary within the Session might look like :

    // Check if you have a Dictionary in the Session
    if(Session["Answers"] != null)
    {
        // Something exists within the Session, so try and cast it as a Dictionary<int, string>
        var answers = Session["Answers"] as Dictionary<int,string>;
    
        // Now that you have a Dictionary, you can read the individual values from it, or store new values in it
        var answerOne = answers[1];
    
        // Store another item in the Dictionary
        answers.Add(42, "The Answer to Life, The Universe and Everything");
    
        // Update the Dictionary in the Session
        Session["Answers"] = answers;
    }
    else
    {
        // If it doesn't exist, create it
        Session["Answers"] = new Dictionary<int, string>();
    
        // Similar code to above
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 19, 2015 9:23 AM