开发者

Radio Button Binding Problem

I am encountering a problem with the binding of database values to radio buttons on my form. I have a quiz, and in my database I have columns for each of the possible answers, as well as a column for the correct answer.

However, on every question that appears on my form, the correct answer is always th开发者_开发技巧e first radio button, even though the value in the 'correct answer' column isn't always the same as the value in the first radio button.

Here is my code:

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");
           }
           }
      }
  }  
}


It looks like you keep incrementing the recordCount variable instead of the questionNumber variable.

You don't even need the recordCount variable, just use the table.Rows.Count property.

I think that should basically solve your problem, although you have some unused foreach looping going on, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜