用C#创建一个使用access数据库的在线测试
我需要创建一个包含 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
) 可以很好地显示问题。问题在于答案和单选按钮(rbAnswer1
、rbAnswer2
、rbAnswer3
)。另外,在数据库中,我有两个表 - 带有列的问题 - ID、QuestionBody、CorreAnswer 和带有列的答案 - ID、QuestionID、AnswerA、AnswerB、AnswerC。
I need to create an online test with 10 questions. I have 30 questions stored in a database and every time the test is started, 10 questions have to be randomly picked. I managed to display the questions on a label, but I am showing all of them. What code do I need so only 10 questions will be displayed? Also there, since are multiple choice answers, I need to assign every answer option to a radio button. Answers are also stored in the database. What I've done so far (I've posted the relevant code only):
Code in the DBConnection class:
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();
}
}
Code in the 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;
}
I got the following error on the list line
"Object reference not set to an instance of an object".
The label (lbwQuest
) displays questions just fine. The problem is with the answers and radio buttons (rbAnswer1
, rbAnswer2
, rbAnswer3
). Also, in the database I have two tables - Questions with columns - ID, QuestionBody, CorrectAnswer, and Answers with columns - ID, QuestionID, AnswerA, AnswerB, AnswerC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误意味着您正在尝试访问属性/尝试调用空对象的方法。
在 LoadQuestions 函数中,如果捕获到异常,则返回 null。
在不检查它是否为空的情况下,您尝试使用 QList[randomQuest].QuestionBody 访问它。我会添加空检查以使代码更加健壮。
在您的 LoadQuestions 和 LoadAnswers 方法中放置断点,并查看它在哪里中断。您还可以使用监视窗口查看对象是否为空
That error means You are trying to access properties / trying to call methods of a null object.
In your LoadQuestions function, if there is an exception caught, you are returning null.
Without checking if it is null or not, you are trying to access that with QList[randomQuest].QuestionBody. I would add a null checking to make the code more robust.
Put breakpoints in your LoadQuestions and LoadAnswers methods and see where it is breaking. You can see whether an object is null or not using watch window also