The code above uses ADO.NET to cycle through all the questions and choices and place them in the table on the WebForm. The program uses the Table, TableCell, TableRow WebControls to display the questions in a decent format.
After the user fills in the quiz, It's time to score the test. The results are compared against the QuestionTable's answers and then computed and printed during the PostBack( after the Score button is pressed). The program takes advantage of the nice features of the HttpRequest object to extract the test results. The HttpRequest has a Form property that contains all the Key-Value pair information in a nice hash table. The program goes through each of the Keys in the Request and hashes out the results in each Radio Group. The Value passed by the Request contains the testtakers answers:
private bool CalculateScore(HttpRequest r)
{
// initialize wrong answer array
WrongArray.Initialize();
// Load up statistic table to get Number of Questions
DataSet ds = new DataSet("StatsDS");
oleDbDataAdapter4.MissingSchemaAction = MissingSchemaAction.AddWithKey;
this.oleDbDataAdapter4.Fill(ds, "StatsTable");
DataTable StatsTable = ds.Tables["StatsTable"];
NumberOfQuestions = (int)StatsTable.Rows[0]["NumberOfQuestions"];
// Load up Questions Table to Get Answers to
// compare to testtaker
oleDbDataAdapter1.MissingSchemaAction = MissingSchemaAction.AddWithKey;
DataSet ds1 = new DataSet("questionsds");
oleDbDataAdapter1.Fill(ds1, "Questions");
DataTable QuestionTable = ds1.Tables["Questions"];
// Load up choices table to print out correct choices
DataSet ds2 = new DataSet("choicesDS");
oleDbDataAdapter2.MissingSchemaAction = MissingSchemaAction.AddWithKey;
oleDbDataAdapter2.Fill(ds2, "Choices");
DataTable ChoicesTable = ds2.Tables["Choices"];
// make sure all questions were answered by the tester
int numAnswered = CalcQuestionsAnsweredCount(r);
if (numAnswered != NumberOfQuestions)
{
return false;
}
NumberCorrect = 0;
NumberWrong = 0;
// initialize wrong answer array to empty string
for (int j = 0; j < NumberOfQuestions; j++)
{
WrongArray[j] = "";
}
// cycle through all the keys in the returned Http Request Object
for (int i = 0; i < r.Form.Keys.Count; i++)
{
string nextKey = r.Form.Keys[i];
// see if the key contains a radio button Group
if (nextKey.Substring(0, 5) == "Group")
{
// It contains a radiobutton, get the radiobutton ID from the hashed Value-Pair Collection
string radioAnswer = r.Form.Get(nextKey);
// extract the letter choice of the tester from the button ID
string radioAnswerLetter = radioAnswer[radioAnswer.Length - 1].ToString();
// extract the question number from the radio ID
string radioQuestionNumber = radioAnswer.Substring(5);
radioQuestionNumber = radioQuestionNumber.Substring(0, radioQuestionNumber.Length - 1);
int questionNumber = Convert.ToInt32(radioQuestionNumber, 10) + 1;
// now compare the testers answer to the answer in the database
DataRow dr = QuestionTable.Rows.Find(questionNumber);
if (radioAnswerLetter == dr["Answer"].ToString())
{
// tester got it right, increment the # correct
NumberCorrect++;
CorrectArray[questionNumber - 1] = true;
WrongArray[questionNumber - 1] = "";
}
else
{
// tester got it wrong, increment the # incorrect
CorrectArray[questionNumber - 1] = false;
// look up the correct answer
string correctAnswer = ChoicesTable.Rows.Find(dr["AnswerID"])["ChoiceText"].ToString();
// put the correct answer in the Wrong Answer Array.
WrongArray[questionNumber - 1] = "Question #" + questionNumber + " - <B>" + dr
["Answer"].ToString() + "</B>. " + correctAnswer + "<BR>\n";
// increment the # of wrong answers
NumberWrong++;
}
}
}
return true;
}
That's all there is to it! In part 2 I plan on adding a web form that gets the testers anonymous ID so that the scores can be collected for each test taker and a test curve can be generated.