使用C#中的访问数据库创建在线测试

时间:2012-01-07 02:32:47

标签: c# database generic-list

我需要创建一个包含10个问题的在线测试。我有30个问题存储在数据库中,每次测试开始时,必须随机挑选10个问题。我设法在标签上显示问题,但我展示了所有问题。我需要什么代码,因此只会显示10个问题?此外,由于是多项选择答案,我需要将每个答案选项分配给单选按钮。答案也存储在数据库中。到目前为止我做了什么(我只发布了相关代码):

DBConnection类中的代码:

public static List<Questions> LoadQuestions()
{
    List<Questions> quest = new List<Questions>();
    OleDbConnection myConnection = GetConnection();
    string myQuery = "SELECT * FROM Questions";
    OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);

    try
    {
        myConnection.Open();
        OleDbDataReader reader = myCommand.ExecuteReader();

        while (reader.Read())
        {
            Questions q = new Questions(Int32.Parse(reader["ID"].ToString()),                
                                        reader["QuestionBody"].ToString(),
                                        reader["CorrectAnswer"].ToString());
            quest.Add(q);
        }

        return quest;
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception in DBHandler" + ex);
        return null;
    }
    finally
    {
        myConnection.Close();
    }
}

public static List<Answers> LoadAnswers()
{
    List<Answers> answers = new List<Answers>();
    OleDbConnection myConnection = GetConnection();

    string myQuery = "SELECT * FROM Answers";
    OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);

    try
    {
        myConnection.Open();
        OleDbDataReader reader = myCommand.ExecuteReader();
        while (reader.Read())
        {
            Answers a = new Answers(Int32.Parse(reader["ID"].ToString()),
                                    reader["AnswerA"].ToString(),
                                    reader["AnswerB"].ToString(),
                                    reader["AnswerC"].ToString(),
                                    (Int32.Parse(reader["QuestionId"].ToString())));
            answers.Add(a);
        }

        return answers;
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception in DBHandler" + ex);
        return null;
    }
    finally
    {
        myConnection.Close();
    }
}

Test.aspx中的代码:

protected void Page_Load(object sender, EventArgs e)
{
    List<Questions> QList = DatabaseConnecter.LoadQuestions();
    Random rndNumber = new Random();
    int randomQuest = rndNumber.Next(30);
    lblQuest.Text = QList[randomQuest].QuestionBody;

    List<Answers> AList = DatabaseConnecter.LoadAnswers();
    int a = 30;
    rbAnswer1.Text = AList[a].AnswerA;
}

我在列表行中出现以下错误

  

“对象引用未设置为对象的实例”。

标签(lbwQuest)显示问题就好了。问题在于答案和单选按钮(rbAnswer1rbAnswer2rbAnswer3)。此外,在数据库中我有两个表 - 带列的问题 - ID,QuestionBody,CorrectAnswer和带有列的答案 - ID,QuestionID,AnswerA,AnswerB,AnswerC。

1 个答案:

答案 0 :(得分:1)

该错误意味着您正在尝试访问属性/尝试调用null对象的方法。

在LoadQuestions函数中,如果捕获到异常,则返回null。 在不检查它是否为null的情况下,您尝试使用QList [randomQuest] .QuestionBody访问它。我会添加一个空检查,以使代码更健壮。

    List<Questions> QList = DatabaseConnecter.LoadQuestions();
    Random rndNumber = new Random();
    int randomQuest = rndNumber.Next(30);
    if((QList!=null) && (QList .Count>0))
    {
       lblQuest.Text = QList[randomQuest].QuestionBody;
    }
    else
    {
       lblQuest.Text = "Questions are not loaded!";
    }


    List<Answers> AList = DatabaseConnecter.LoadAnswers();
    int a = 30;
    if((AList!==null) && (AList.Count>0))
    {
       rbAnswer1.Text = AList[a].AnswerA;
    }
    else
    {
       rbAnswer1.Text = "Answers are not loaded!";
    }

在LoadQuestions和LoadAnswers方法中放置断点并查看它的中断位置。您还可以使用监视窗口查看对象是否为空