Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Question...

Posted on 2004-07-30
2
Medium Priority
?
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 23

Accepted Solution

by:
Snarf0001 earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

715 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