单选按钮绑定问题

时间:2011-08-01 19:52:17

标签: c# winforms

我遇到了将数据库值绑定到表单上的单选按钮的问题。我有一个测验,在我的数据库中,我有每个可能答案的列,以及正确答案的列。

但是,在我的表单上出现的每个问题上,正确答案始终是第一个单选按钮,即使“正确答案”列中的值并不总是与第一个单选按钮中的值相同。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Quiz_Test
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    String chosenAnswer, correctAnswer;
    DataTable table;
    int questionNumber = 0;
    int correctAnswers = 0;

    private void Form1_Load(object sender, EventArgs e)
    {
        string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\QuizQuestions.accdb";

        OleDbConnection conGet = new OleDbConnection(cnString);
        OleDbCommand cmdGet = new OleDbCommand();

        conGet.Open();
        cmdGet.CommandType = CommandType.Text;
        cmdGet.Connection = conGet;

        cmdGet.CommandText = "SELECT * FROM QuizQuestions ORDER BY rnd()";

        OleDbDataReader reader = cmdGet.ExecuteReader();
        table = new DataTable();
        table.Load(reader);
        int recordCount = table.Rows.Count;

        foreach (DataRow row in table.Rows)
        {

            labelQuestion.Text = table.Rows[questionNumber]["Question"].ToString();
            radioButton1.Text = table.Rows[questionNumber]["Answer 1"].ToString();
            radioButton2.Text = table.Rows[questionNumber]["Answer 2"].ToString();
            radioButton3.Text = table.Rows[questionNumber]["Answer 3"].ToString();
            radioButton4.Text = table.Rows[questionNumber]["Answer 4"].ToString();
            correctAnswer = table.Rows[questionNumber]["Correct Answer"].ToString();
            recordCount++;
        } 
    conGet.Close();
    }

    private void btnGoToNextOne_Click(object sender, EventArgs e)
    {
        String cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\QuizQuestions.accdb";

        OleDbConnection conGet = new OleDbConnection(cnString);
        OleDbCommand cmdGet = new OleDbCommand();
        int recordCount = table.Rows.Count;

        {
            conGet.Open();

            cmdGet.CommandType = CommandType.Text;
            cmdGet.Connection = conGet;

            cmdGet.CommandText = "SELECT * FROM QuizQuestions ORDER BY rnd()"; // select all columns in all rows

            OleDbDataReader reader = cmdGet.ExecuteReader();
            reader.Read();
        if (questionNumber < recordCount)
        {

           if (radioButton1.Checked)
           {
              chosenAnswer = reader["Answer 1"].ToString();
           }
           else if (radioButton2.Checked)
           {
              chosenAnswer = reader["Answer 2"].ToString();
           }
           else if (radioButton3.Checked)
           {
              chosenAnswer = reader["Answer 3"].ToString();
           }
           else if (radioButton4.Checked)
           {
              chosenAnswer = reader["Answer 4"].ToString();
           }

           if (questionNumber < recordCount)
           {

               if (chosenAnswer == reader["Correct Answer"].ToString())
               {
                   labelQuestion.Text = table.Rows[questionNumber]["Question"].ToString();
                   radioButton1.Text = table.Rows[questionNumber]["Answer 1"].ToString();
                   radioButton2.Text = table.Rows[questionNumber]["Answer 2"].ToString();
                   radioButton3.Text = table.Rows[questionNumber]["Answer 3"].ToString();
                   radioButton4.Text = table.Rows[questionNumber]["Answer 4"].ToString();
                   correctAnswer = table.Rows[questionNumber]["Correct Answer"].ToString();
                   questionNumber++;
                   correctAnswers++;
               }
               else
               {
                   MessageBox.Show("This is incorrect");
               }
               }
               recordCount++;
           }
           else
           {
               MessageBox.Show("You have finished the test. You have " + questionNumber + " answers correct");
           }
           }
      }
  }  
}

1 个答案:

答案 0 :(得分:0)

看起来你继续增加recordCount变量而不是questionNumber变量。

您甚至不需要recordCount变量,只需使用table.Rows.Count属性。

我认为这基本上可以解决你的问题,虽然你有一些未使用的foreach循环,等等。