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;
}
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.
asp.netSession
I'm afraid I no longer use this forum due to the new point allocation system.
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.
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?
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
}
Member
6 Points
13 Posts
Session gets overwritten.
Jan 19, 2015 08:01 AM|Fjalarsson|LINK
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.
asp.net Session
All-Star
37441 Points
9076 Posts
Re: Session gets overwritten.
Jan 19, 2015 08:12 AM|AidyF|LINK
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.
asp.net Session
All-Star
114593 Points
18503 Posts
MVP
Re: Session gets overwritten.
Jan 19, 2015 08:17 AM|Rion Williams|LINK
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 :
Likewise, you could do the same for a Dictionary that would map your question number (an integer) to an answer (a string) :
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.
asp.net Session
Member
6 Points
13 Posts
Re: Session gets overwritten.
Jan 19, 2015 09:16 AM|Fjalarsson|LINK
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?
asp.net Session
All-Star
114593 Points
18503 Posts
MVP
Re: Session gets overwritten.
Jan 19, 2015 09:23 AM|Rion Williams|LINK
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 :
asp.net Session