Figure 1: Snapshot of the generated Web Quiz.
Just when you thought you'd never see another test again, your back in school, sharpening that #2 pencil and blowing away erasure particles. Well maybe not anymore thanks to the power of C# and .NET! You may still be taking tests, but you probably won't have to go out and buy a sharpener. This article describes how to create a web quiz from the information in a database. For the purposes of this article, I chose to use MS Access, but the code can be altered easy enough to use SqlServer, MySql, Oracle, or whatever your favorite provider happens to be. Below is the Database Schema for our Quiz:
Figure 2: This Access Database was reverse engineered using
WithClass 2000
The structure of the Database is fairly simple. The questions are stored in the Questions Table along with the answers. The Choices are stored in the Choices table and are pointed to by the QuestionID key. The StatsTable holds the title and NumberOfQuestions. Finally, the Testers table holds the information for the people taking the test. (This table is not utilized in this example, but will be in part II of this article.)
The web page for the quiz is constructed on the fly using Microsoft WebForms, Tables, TableRows, and TableCells. Below is the code that reads the MSAccess data using ADO.NET and creates the table. Note that all of this is done in the PageLoad Event:
- private void Page_Load(object sender, System.EventArgs e) {
- if (!IsPostBack) {
-
- ReadQuizTitle();
- ReadQuestionsIntoTable();
- AddSubmitButton();
- } else {
-
- HttpRequest r;
- r = this.Request;
- bool AreAllAnswered = CalculateScore(r);
- HttpResponse rs;
- rs = this.Response;
- if (AreAllAnswered == false) {
- rs.Write("You missed a few questions. Go back in your browser and answer them<P>");
- return;
- }
-
- rs.Write("Your score is " + NumberCorrect.ToString() + " out of " + NumberOfQuestions.ToString() +
- "<P>");
-
- for (int num = 0; num < NumberOfQuestions; num++) {
- if (WrongArray[num].Length > 0)
- rs.Write(WrongArray[num]);
- }
-
- rs.Write(GetRanking());
- }
- }
The first half of the if statement is handled when the page first loads. This part is responsible for drawing the quiz. The second half of the if statement is executed after the user presses the Score Button. This part of the if statement will score the test and output a page giving the score, the missed questions, and a ranking.
Unfortunately, you can't tell much from the code above. So let's delve into what's happening in the ReadQuestionsIntoTable Routine:
- private void ReadQuestionsIntoTable() {
-
- DataSet ds1 = new DataSet("questionsds");
- oleDbDataAdapter1.Fill(ds1, "Questions");
- oleDbDataAdapter2.Fill(ds1, "Choices");
- DataTable QuestionsTable = ds1.Tables["Questions"];
- DataTable ChoicesTable = ds1.Tables["Choices"];
-
-
- DataRelation QALink = new DataRelation("QuestionLink", QuestionsTable.Columns["QuestionID"],
- ChoicesTable.Columns["QuestionID"]);
- QuestionsTable.ChildRelations.Add(QALink);
- NumberOfQuestions = 0;
-
-
- foreach(DataRow dr in QuestionsTable.Rows) {
-
- TableRow tr = new TableRow();
- Table1.Rows.Add(tr);
- TableCell aCell = new TableCell();
-
- aCell.Text = dr["QuestionText"].ToString();
- tr.Cells.Add(aCell);
- AnswerArray[NumberOfQuestions] = dr["Answer"].ToString();
-
- int count = 0;
-
-
-
- foreach(DataRow choiceRow in dr.GetChildRows(QALink)) {
- TableRow tr2 = new TableRow();
- Table1.Rows.Add(tr2);
-
- TableCell aCell3 = new TableCell();
- aCell3.Width = 1000;
-
- aCell3.HorizontalAlign = HorizontalAlign.Left;
- tr2.Cells.Add(aCell3);
-
-
- RadioButton rb = new RadioButton();
-
- rb.GroupName = "Group" + choiceRow["QuestionID"].ToString();
-
-
- rb.Text = choiceRow["ChoiceLetter"].ToString() + ". " + choiceRow["ChoiceText"].ToString();
-
- rb.ID = "Radio" + NumberOfQuestions.ToString() + Convert.ToChar(count + 65);
- rb.Visible = true;
-
- aCell3.Controls.Add(rb);
- count++;
- }
-
-
- TableRow spacer = new TableRow();
- spacer.Height = 30;
- TableCell spacerCell = new TableCell();
- spacerCell.Height = 30;
- spacer.Cells.Add(spacerCell);
- Table1.Rows.Add(spacer);
-
- NumberOfQuestions++;
- }
- }
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) {
-
- WrongArray.Initialize();
-
- 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"];
-
-
- oleDbDataAdapter1.MissingSchemaAction = MissingSchemaAction.AddWithKey;
- DataSet ds1 = new DataSet("questionsds");
- oleDbDataAdapter1.Fill(ds1, "Questions");
- DataTable QuestionTable = ds1.Tables["Questions"];
-
- DataSet ds2 = new DataSet("choicesDS");
- oleDbDataAdapter2.MissingSchemaAction = MissingSchemaAction.AddWithKey;
- oleDbDataAdapter2.Fill(ds2, "Choices");
- DataTable ChoicesTable = ds2.Tables["Choices"];
-
- int numAnswered = CalcQuestionsAnsweredCount(r);
- if (numAnswered != NumberOfQuestions) {
- return false;
- }
- NumberCorrect = 0;
- NumberWrong = 0;
-
- for (int j = 0; j < NumberOfQuestions; j++) {
- WrongArray[j] = "";
- }
-
- for (int i = 0; i < r.Form.Keys.Count; i++) {
- string nextKey = r.Form.Keys[i];
-
- if (nextKey.Substring(0, 5) == "Group") {
-
- string radioAnswer = r.Form.Get(nextKey);
-
- string radioAnswerLetter = radioAnswer[radioAnswer.Length - 1].ToString();
-
- string radioQuestionNumber = radioAnswer.Substring(5);
- radioQuestionNumber = radioQuestionNumber.Substring(0, radioQuestionNumber.Length - 1);
- int questionNumber = Convert.ToInt32(radioQuestionNumber, 10) + 1;
-
- DataRow dr = QuestionTable.Rows.Find(questionNumber);
- if (radioAnswerLetter == dr["Answer"].ToString()) {
-
- NumberCorrect++;
- CorrectArray[questionNumber - 1] = true;
- WrongArray[questionNumber - 1] = "";
- } else {
-
- CorrectArray[questionNumber - 1] = false;
-
- string correctAnswer = ChoicesTable.Rows.Find(dr["AnswerID"])["ChoiceText"].ToString();
-
- WrongArray[questionNumber - 1] = "Question #" + questionNumber + " - <B>" + dr["Answer"].ToString() + "</B>. " + correctAnswer + "<BR>\n";
-
- 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.