Reputation: 2021
The web application that I am developing right now has something called quiz engine which provides users with short quizzes which consist of one question or more. Now, I have a problem with taking/ answering the quiz: When the user finishes the quiz and he goes to the Result Page and he goes back to the Quiz page and answers the same question, he will get two results and I don't know why !!!
In brief, answering same question multiple times should not create multiple entries.
For creating the Quiz engine, I used the Toturial for the Quiz Engine in the ASP.NET website for creating what I have.
My Code-Behind:
This is the code that is responsible for saving the answers:
protected void nextButton_Click(object sender, EventArgs e)
{
// Save off previous answers
System.Data.DataRowView dr = (System.Data.DataRowView)questionDetails.DataItem;
// Create Answer object to save values
Answer a = new Answer();
a.QuestionID = dr["QuestionOrder"].ToString();
a.CorrectAnswer = dr["CorrectAnswer"].ToString();
a.UserAnswer = answerDropDownList.SelectedValue.ToString();
ArrayList al = (ArrayList)Session["AnswerList"];
al.Add(a);
Session.Add("AnswerList", al);
if (questionDetails.PageIndex == questionDetails.PageCount - 1)
{
// Go to evaluate answers
Response.Redirect("Results.aspx");
}
else
{
questionDetails.PageIndex++;
}
if (questionDetails.PageIndex == questionDetails.PageCount - 1)
{
nextButton.Text = "Finished";
}
}
So now how I can let the user be able to change his answer (or goes back to one of the answered questions) and this new answer will replace the previous one (not to be considered that the user answered the same question twice). How to do that?
EDIT:
The following code is responsible for saving the result:
resultGrid.DataSource = al;
resultGrid.DataBind();
// Save the results into the database.
if (IsPostBack == false)
{
// Calculate score
double questions = al.Count;
double correct = 0.0;
for (int i = 0; i < al.Count; i++)
{
Answer a = (Answer)al[i];
if (a.Result == Answer.ResultValue.Correct)
correct++;
}
double score = (correct / questions) * 100;
string username = HttpContext.Current.User.Identity.Name.ToString().Replace("ARAMCO\\", "");
SqlDataSource userQuizDataSource = new SqlDataSource();
userQuizDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ToString();
userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([QuizID], [DateTimeComplete], [Score], [Username]) VALUES (@QuizID, @DateTimeComplete, @Score, @Username)";
userQuizDataSource.InsertParameters.Add("QuizID", Session["QuizID"].ToString());
userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());
userQuizDataSource.InsertParameters.Add("Score", score.ToString());
userQuizDataSource.InsertParameters.Add("Username", username);
int rowsAffected = userQuizDataSource.Insert();
if (rowsAffected == 0)
{
// Let's just notify that the insertion didn't
// work, but let' s continue on ...
errorLabel.Text = "There was a problem saving your quiz results into our database. Therefore, the results from this quiz will not be displayed on the list on the main menu.";
}
}
Upvotes: 0
Views: 214
Reputation: 1402
I think When your logic is good.And also i apply same logic for this one.But when user click previous that time you have to clear that particular session and add new one. And also add finish button for exit.
Upvotes: 0
Reputation: 9680
Here what can you try
Before you add answer to AnswerList, check if that question already exists in the list, if yes replace that with the current. This you will have to do when you are saving the answer to DB. The code below will update the object in Session only.
// Create Answer object to save values
Answer a = new Answer();
a.QuestionID = dr["QuestionOrder"].ToString();
a.CorrectAnswer = dr["CorrectAnswer"].ToString();
a.UserAnswer = answerDropDownList.SelectedValue.ToString();
ArrayList al = (ArrayList)Session["AnswerList"];
var oldAnswer = al.ToArray().Where(ans => (ans as Answer).QuestionID == a.QuestionID);
if (oldAnswer.Count() != 0)
{
a = oldAnswer.FirstOrDefault() as Answer;
a.CorrectAnswer = dr["CorrectAnswer"].ToString();
a.UserAnswer = answerDropDownList.SelectedValue.ToString();
}
else
{
al.Add(a);
}
//Rest of your code
Hope this helps.
Upvotes: 2