Getting the Table Row Count C#
I have code for a quiz but am unsure on how I count the total number of questions in my database. I know that I need a count query but i'm not sure where to insert it. 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;
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);
foreach (DataRow row in table.Rows)
{
labelQuestion.Text = table.Rows[0]["Question"].ToString();
radioButton1.Text = table.Rows[0]["Answer 1"].ToString();
radioButton2.Text = table.Rows[0]["Answer 2"].ToString();
radioButton3.Text = table.Rows[0]["Answer 3"].ToString();
radioButton4.Text = table.Rows[0]["Answer 4"].ToString();
correctAnswer = table.Rows[0]["Correct Answer"].ToString(); ;
questionNumber = 0;
}
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();
{
conGet.Open();
cmdGet.CommandType = CommandType.Text;
cmdGet.Connection = conGet;
cmdGet.CommandText = "SELECT * FROM QuizQuestions ORDER BY rnd()";
OleDbDataReader reader = cmdGet.ExecuteReader();
reader.Read();
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 (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();开发者_C百科
questionNumber++;
}
else
{
MessageBox.Show("That is not the correct answer");
}
}
}
} }
I know I need to put in "SELECT count(*) from QuizQuestions" but I'm not sure how I can determine the 'position' in the set of questions so that I don't get this error:
IndexOutOfRangeException was unhandled
There is no row at position 5
If you're already planning on pulling back all of the records, you can just get a count from the DataTable after pulling back the record set. e.g.
_recordCount = table.Rows.Count;
Store this variable at a scope accessible to your class and then check against it before enumerating to the next record, e.g.
if(questionNumber+1<=_recordCount) {
_recordCount++;
}
else
{
// No more questions, do something else here.
}
As I just noticed that your table
variable is defined privately, you could also just check against the table.Rows.Count
directly, instead of storing a variable. e.g.
if(questionNumber+1<=_table.Rows.Count) {
_recordCount++;
}
else
{
// No more questions, do something else here.
}
are you just looking for table.Rows.Count? I only glanced at your code but it looks like you're using the first row (table.Rows[0]) for every iteration of your foreach loop.
It's the "table.Rows[questionNumber]" that is killing you, you need to do a
table.Rows.Count check in that area. Something like
if (questionNumber < table.Rows.Count )
精彩评论