Solved

C# Question...

Posted on 2004-07-30
2
321 Views
Last Modified: 2012-05-05
The following code class compiles correctly, it doesn't throw any exceptions, it, however, does not seem to be loading my XML document.  It loads...  just no questions (it is meant to generate a quiz based on an XML document).  Any suggestions?

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.XPath;


namespace Pace
{
      /// <summary>
      /// Summary description for quiz.
      /// </summary>
      public class quiz : System.Web.UI.Page{
                        
                  protected Button btnSubmit;
                  protected Label lblQuestion;
                  protected RadioButtonList rblAnswer;
                  protected Label lblTotalQuestion;
                  protected Label lblTimeSpent;
                  protected Label lblResult;
                  protected Panel QuizScreen;
                  protected Panel ResultScreen;
                  
                  XPathNavigator xNav;
                  XPathDocument xDoc;
                  XPathNodeIterator xNodeIterator;
                  System.Collections.ArrayList arrAnswerHistory = new ArrayList();
                  
                  int intTotalQuestion;
                  int intQuestionNo;
                  int intScore;
                  protected override void OnInit(EventArgs e) {
                        base.OnInit(e);
                        this.InitializeComponent();

                        string strXmlDoc = "default.xml";
                        string strXmlFilePath = Server.MapPath("~/media/quizzes/default.xml" );
                        XPathDocument xDoc = new XPathDocument(strXmlFilePath);
                        XPathNavigator xNav = xDoc.CreateNavigator();
                  }

                  private void InitializeComponent() {
                        this.btnSubmit.Click += new EventHandler(this.btnSubmit_Click);
                  }
                  
                  public static void Main() {
                                          
                  }

                  private void Page_Load(object sender, System.EventArgs e){
                        this.Main();

                        if(!Page.IsPostBack){
                              intTotalQuestion = xNav.Select("/quiz/mchoice").Count;
                              ViewState["StartTime"] = DateTime.Now;
                              ShowQuestion(intQuestionNo);
                        }
                  }

                  private void btnSubmit_Click(object src, EventArgs e) {

                        intTotalQuestion = (int)this.ViewState["TotalQuestion"];
                        intQuestionNo = (int)this.ViewState["QuestionNo"];
                        intScore = (int)this.ViewState["Score"];
                        arrAnswerHistory = (System.Collections.ArrayList)this.ViewState["AnswerHistory"];

                        if(rblAnswer.SelectedItem.Value == (string)this.ViewState["CorrectAnswer"]) {
                              intScore += 1;
                              arrAnswerHistory.Add(0);
                        }
                  
                        arrAnswerHistory.Add(rblAnswer.SelectedItem.Value);

                        if(intQuestionNo==intTotalQuestion) {

                              QuizScreen.Visible = false;
                              ResultScreen.Visible = true;
                              ShowResult();

                        }

                        QuizScreen.Visible = true;
                        ResultScreen.Visible = false;
                        intQuestionNo += 1;
                 
                        ShowQuestion(intQuestionNo);
                  }

                  private void ShowQuestion(int intQuestionNo) {
                        string strXPath;
                        int intLoop;
                        TimeSpan objTimeSpent;

                        strXPath = "/quiz/mchoice[intQuestionNo.ToString()]";

                        xNodeIterator = xNav.Select(strXPath + "/question");
                        xNodeIterator.MoveNext();
                        lblQuestion.Text = intQuestionNo.ToString() + ". " + xNodeIterator.Current.Value;

                        xNodeIterator = xNav.Select(strXPath + "/answer");

                        rblAnswer.Items.Clear();

                        intLoop = 0;
                        while(xNodeIterator.MoveNext()){
                              intLoop += 1;
                              rblAnswer.Items.Add(new ListItem(xNodeIterator.Current.Value, intLoop.ToString()));

                              if(xNodeIterator.Current.GetAttribute("correct","") == "yes") {
                                    ViewState["CorrectAnswer"] = intLoop;
                              }
                        }
                        lblTotalQuestion.Text = intTotalQuestion.ToString();

                        objTimeSpent = DateTime.Now.Subtract((DateTime)this.ViewState["StartTime"]);
                        lblTimeSpent.Text = objTimeSpent.Minutes.ToString() + ":" + objTimeSpent.Seconds.ToString();

                        ViewState["TotalQuestion"] = intTotalQuestion;
                        ViewState["Score"] = intScore;
                        ViewState["QuestionNo"] = intQuestionNo;
                        ViewState["AnswerHistory"] = arrAnswerHistory;

                  }

                  private void ShowResult() {
                        string strResult;
                        int intLoop;
                        string strXPath;
                        TimeSpan objTimeSpent;
                      
                        objTimeSpent = DateTime.Now.Subtract((DateTime)this.ViewState["StartTime"]);

                        strResult  = "<center>";
                        strResult += "<h3>Quiz Result</h3>";
                        strResult += "<p>Points: " + intScore.ToString() + " of " + intTotalQuestion.ToString();
                        strResult += "<p>Your Competency: " + (intScore/intTotalQuestion*100).ToString() + "%%";
                        strResult += "<p>Time Spent: " + objTimeSpent.Minutes.ToString() + ":" + objTimeSpent.Seconds.ToString();
                        strResult += "</center>";

                        strResult += "<h3>Quiz Breakdown:</h3>";
                        for(intLoop=1;intLoop<=intTotalQuestion;intLoop++) {
                              strXPath = "/quiz/mchoice[" + intLoop.ToString() + "]";
                              xNodeIterator = xNav.Select(strXPath + "/question");
                              xNodeIterator.MoveNext();
                              strResult += "<b>" + intLoop.ToString() + ". " + xNodeIterator.Current.Value + "</b><br>";
                              if((arrAnswerHistory.Count - 1) == 0) {
                                    strResult += "<b>Correct</b><br><br>";
                              } else {
                                    xNodeIterator = xNav.Select(strXPath + "/answer[" + (arrAnswerHistory.Count - 1).ToString() + "]");
                                    xNodeIterator.MoveNext();
                                    strResult += "<b>You answered:</b> " + xNodeIterator.Current.Value + "<br>";
                                    strResult += "<b>Incorrect</b></font><br><br>";
                              }
                        }

                        lblResult.Text = strResult;
                  }
            }
}
0
Comment
Question by:Praesidium
2 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11699572
what do you get when you run debug and look at the loading in oninit ?
0
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 11711266
c# can sometimes have a nasty little habit of erasing the page load initialization.
Add a line into your InitializeComponent() function:


private void InitializeComponent() {
  this.Load += new System.EventHandler(this.Page_Load);
  this.btnSubmit.Click += new EventHandler(this.btnSubmit_Click);
}
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now