2012-01-07 6 views
0

私が今開発しているWebアプリケーションには、クイズエンジンと呼ばれるものがあります。クイズエンジンは、1つ以上の質問で構成される短いクイズをユーザーに提供します。今、グリッドビューに の質問番号、ユーザーの回答と結果を表示するRESULTSページに問題があります。また、このページには、DetailsViewに質問、4つの可能な回答、正解と解答の説明が表示されます。私が持っている主な問題は次のとおりです。 データベースの正解をnvarcharデータ型に設定しました。ユーザがそれらのうちの1つ(例えばC)を選択すると、A、B、C、DのようにPossibleの回答を列挙し、GridViewはその結果(ユーザの回答)を文字ではなく3の数字として表示します。 DetailsViewはCとして正解を表示します。理由はわかりません。ユーザーが選択した値を数値ではなく文字として表示するにはどうすればよいですか?

クイズエンジンを作成するために、私はそれを作成するためにthe Toturial in the ASP.NET websiteを使用しました。

私のASP.NETコード:私のコードビハインド

<asp:GridView ID="resultGrid" runat="server" DataKeyNames="QuestionID" SelectedIndex="0" 
        AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateSelectButton="True" OnSelectedIndexChanged="resultGrid_SelectedIndexChanged" Width="555px"> 
         <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
         <RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" HorizontalAlign="Center" /> 
         <Columns> 
          <asp:BoundField DataField="QuestionID" HeaderText="Question" /> 
          <%--<asp:BoundField DataField="CorrectAnswer" HeaderText="Correct Answer" />--%> 
          <asp:BoundField DataField="UserAnswer" HeaderText="Your Answer" /> 
          <asp:BoundField DataField="Result" HeaderText="Result" /> 
         </Columns> 
         <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
         <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
         <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" CssClass="boldtext" /> 
         <EditRowStyle BackColor="#999999" /> 
         <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
        </asp:GridView> 

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
         SelectCommand="SELECT [Question], [Answer1], [Answer2], [Answer3], [QuestionID], [QuestionOrder], [Answer4], [CorrectAnswer], [AnswerExplanation], [QuizID] FROM [Question] WHERE ([QuizID] = @QuizID) ORDER BY [QuestionOrder]"> 
         <SelectParameters> 
          <asp:SessionParameter Name="QuizID" SessionField="QuizID" Type="Int32" /> 
         </SelectParameters> 
        </asp:SqlDataSource> 

<asp:DetailsView ID="answerDetails" runat="server" CellPadding="4" ForeColor="#333333" 
         GridLines="None" Height="45px" Width="552px" DataSourceID="SqlDataSource1" 
         AutoGenerateRows="False" DataKeyNames="QuestionID"> 

         <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
         <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" /> 
         <RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" /> 
         <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" CssClass="boldtext" Width="100px" /> 
         <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
         <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
         <EditRowStyle BackColor="#999999" /> 
         <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
         <Fields> 
          <asp:BoundField DataField="Question" HeaderText="Question" 
           SortExpression="Question" /> 
          <asp:BoundField DataField="Answer1" HeaderText="A" 
           SortExpression="Answer1" /> 
          <asp:BoundField DataField="Answer2" HeaderText="B" 
           SortExpression="Answer2" /> 
          <asp:BoundField DataField="Answer3" HeaderText="C" 
           SortExpression="Answer3" /> 
          <asp:BoundField DataField="Answer4" HeaderText="D" 
           SortExpression="Answer4" /> 
          <asp:BoundField DataField="CorrectAnswer" HeaderText="Correct Answer" 
           SortExpression="CorrectAnswer" HeaderStyle-BackColor="lightgreen" /> 
          <asp:BoundField DataField="AnswerExplanation" HeaderText="Explanation" 
           SortExpression="AnswerExplanation" HeaderStyle-BackColor="lightgreen" /> 
         </Fields> 
        </asp:DetailsView> 

protected void Page_Load(object sender, EventArgs e) 
    { 
     ArrayList al = (ArrayList)Session["AnswerList"]; 

     if (al == null) 
     { 
      Response.Redirect("default.aspx"); 
     } 

     resultGrid.DataSource = al; 
     resultGrid.DataBind(); 

     // Save the results into the database. 
     if (IsPostBack == false) 
     { 
      // Calculate score 
      double questions = al.Count; 
      double correct = 0.0; 


      for (int i = 0; i < al.Count; i++) 
      { 
       Answer a = (Answer)al[i]; 
       if (a.Result == Answer.ResultValue.Correct) 
        correct++; 
      } 

      double score = (correct/questions) * 100; 
      string username = HttpContext.Current.User.Identity.Name.ToString().Replace("ARAMCO\\", ""); 
      SqlDataSource userQuizDataSource = new SqlDataSource(); 
      userQuizDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ToString(); 
      userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([QuizID], [DateTimeComplete], [Score], [Username]) VALUES (@QuizID, @DateTimeComplete, @Score, @Username)"; 

      userQuizDataSource.InsertParameters.Add("QuizID", Session["QuizID"].ToString()); 
      userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString()); 
      userQuizDataSource.InsertParameters.Add("Score", score.ToString()); 
      userQuizDataSource.InsertParameters.Add("Username", username); 

      int rowsAffected = userQuizDataSource.Insert(); 
      if (rowsAffected == 0) 
      { 
       // Let's just notify that the insertion didn't 
       // work, but let' s continue on ... 
       errorLabel.Text = "There was a problem saving your quiz results into our database. Therefore, the results from this quiz will not be displayed on the list on the main menu."; 


      } 

     } 


    } 

    protected void resultGrid_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     SqlDataSource1.FilterExpression = "QuestionOrder=" + resultGrid.SelectedValue; 
    } 

ので、どのようにこの問題を解決するには?

+0

Answer.CorrectAnswerプロパティの実際の値は何ですか?数字か手紙ですか? – alexm

+0

A、B、C、Dなどの文字 – user1093651

+0

申し訳ありませんが、データベースでは、Answer1、Answer2、Answer3、Answer4、Correct Answerの5つの列があります。最初の4つの答えでは、任意のテキストを置くことができます。正解の場合は、A、B、CまたはDを入力するだけです。これは前述のようにGridViewのUser Answerフィールドの下に表示するものです – user1093651

答えて

2

を私に尋ねますすべての質問と回答を1つのデータテーブル/配列リストで取得します。 そしてそれを一つずつフォームにバインドします。あなたが最初の質問にあって、正しい答えがDならば、ユーザがBを選択し、最初の質問の時間フラグが偽であると仮定してください。答えが正しい場合は、フラグをtrueに設定します。すべての質問でこのプロセスを行います。試験の終了後、そのフラグを使用して結果を作成できます。

0

質問と回答の配列を維持する必要があると思います。質問、回答、選択回答の配列を作成する必要があります。ユーザーがオプションを変更してその時刻を送信すると、選択されたオプションが正しくまたは間違っています。回答が正しい場合は、フラグ(選択された回答)をtrueに設定します。そして試験の完了後、その配列を使って結果を生成する必要があります。

私もこの方法を使用してこのアプリケーションを行っている....

をあなたにこの助けとなることがあります... あなたが任意のクエリを持っている場合は、あなたが持っている.....

+0

ユーザーの回答にA、B、C、Dを表示しますフィールドはあなたが上記のことをすべて行う必要はありません。 – user1093651

+0

デフォルトのFalseであるFlagにもう1つの列を追加し、回答を送信した後に変更します。 –

+0

より多くの情報を私に提供してもらえますか? – user1093651

関連する問題