Solved

C# Question...

Posted on 2004-07-30
2
324 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

774 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