通过测验问题循环失败

时间:2011-07-25 12:19:30

标签: c# winforms

已编辑 - 我最初发布了我的代码的早期版本,现在正确的代码

我有一个我在表单中显示的测验问题的数据库表,但是,当我到达表中的最后一个问题时,我收到以下错误:

IndexOutOfRangeException was unhandled
There is no row at position 5

我的表结构是这样的:

问题编号|问题|答案1 |答案2 |答案3 |答案4 |正确答案

这是我的代码:

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.SqlClient;

namespace WindowsFormsApplication1
{
public partial class QuizQuestions : Form
{
    public WindowsAnalysisQuiz()
    {
        InitializeComponent();
    }
    //int questionNumber;
    String correctAnswer;
    private void WindowsAnalysisQuiz_Load(object sender, EventArgs e)
    {
        //declare connection string using windows security
        string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hanna\\Desktop\\QuizQuestions.accdb";

        //declare Connection, command and other related objects
        OleDbConnection conGet = new OleDbConnection(cnString);
        OleDbCommand cmdGet = new OleDbCommand();

        //try
        //{
        //open connection
        conGet.Open();
        //String correctAnswer;

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

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

        OleDbDataReader reader = cmdGet.ExecuteReader();

        reader.Read();
        label1.Text = reader["Question"].ToString();
        radioButton1.Text = reader["Answer 1"].ToString(); 
        radioButton2.Text = reader["Answer 2"].ToString();
        radioButton3.Text = reader["Answer 3"].ToString();
        radioButton4.Text = reader["Answer 4"].ToString();
        correctAnswer = reader["Correct Answer"].ToString();
        //questionNumber = 0;

        conGet.Close();

    }

    private void btnNextQuestion_Click(object sender, EventArgs e)
    {

        String cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\QuizQuestions.accdb";
        int questionNumber = 0;
        //declare Connection, command and other related objects
        OleDbConnection conGet = new OleDbConnection(cnString);
        OleDbCommand cmdGet = new OleDbCommand();

        //try
        {
            //open connection
            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();

            String chosenAnswer = "";
            //int chosenCorrectly = 0;
            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
            {
                chosenAnswer = reader["Answer 4"].ToString();
            }

            if (chosenAnswer == reader["Correct Answer"].ToString())
            {
                labelQuestion.Text = table.Rows[questionNumber]["Question"].ToString();
                //and show possible answers:
                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++; //got to next question! 
            }
            else
            {
                MessageBox.Show("That is not the correct answer");
            }
        }

    }
}

}

我想要做的是获得一个for循环,它会列出我的数据库中的问题,但是这样可以让我在最后一个问题得到解答后正确地回答所有答案,当然,没有得到我得到的错误

4 个答案:

答案 0 :(得分:1)

我认为questionNumber的起始值必须为0,因为Rows为零,否则table.Rows[questionNumber]将在最后一行失败。

答案 1 :(得分:0)

你应该将questionNumber的初始值设置为0,因为行基于零。并获取pageload中的问题计数,并将另一个变量设置为int questionCount 你可以在buttonClick中找到它。

按钮中的

单击用questionNumber =(questionNumber ++%questionCount)更改questionNumber ++;

它将转向问题结束的开头。

希望这会有所帮助。

答案 2 :(得分:0)

每次选择正确的答案时,btnGoToNextOne_Click中的代码都会无条件地增加questionNumber(questionNumber ++)。正确选择最后一个问题时,questionNumber索引超出范围,因此可能会在代码中的其他位置出现问题。

另一个是在Form1_Load中,你设置questionNumber = 1,假设你的数据库中至少有2个问题。除非由于某种原因将questionNumber设置为1,否则它应该设置为0(因为C#中的索引是从零开始的)。

在这段代码中你还有很多需要做的事情,重复字符串,OleDbX ...... X对象应该用(OleDbX ... X){}包装,以便它们被处理掉正确;为什么DataTable表不是局部变量,我不知道...

答案 3 :(得分:0)

如果这是最后一行,你应该告诉编译器结束,你应该添加它..

if (chosenAnswer == reader["Correct Answer"].ToString() && 
    questionNumber <= tables[your table].rows.count())