I have a script that select from database in random order.
The script is a simulation of auto exam. The thing I want is that if a question has already been selected, to not be extracted again.
How can I do that? Other thing, if no one of those 3 checkboxes were selected, when message box appear, it selects another question. Select() initialize labels with value from database and I use it in Load form.
Here is the Code what i have tried so far:
private void select()
{
string dataA = "SELECT * FROM questions order by rand()";
MySqlCommand cmd = new MySqlCommand(dataA, index.connect);
cmd.CommandType = CommandType.Text;
using (index.connect)
{
index.connect.Open();
MySqlDataReader rdr = cmd.ExecuteReader();
try
{
if (rdr.Read())
{
label2.Text = rdr["question"].ToString();
label2.AutoSize = true;
label2.UseCompatibleTextRendering = true;
label3.Text = rdr["answer1"].ToString();
label4.Text = rdr["answer2"].ToString();
label5.Text = rdr["answer3"].ToString();
option1 = checkBox1.Checked;
option2 = checkBox2.Checked;
option3 = checkBox3.Checked;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
index.connect.Close();
}
}
}
private void button1_Click(object sender, EventArgs e)
// in cazul in care raspunzi la o intrebare,
//iar aceasta ramane, orice a-i raspunde, pune la incorect
{
string dataA = "SELECT * FROM questions order by rand()";
MySqlCommand cmd = new MySqlCommand(dataA, index.connect);
cmd.CommandType = CommandType.Text;
using (index.connect)
{
index.connect.Open();
MySqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read())
{
label2.Text = rdr["question"].ToString();
label3.Text = rdr["answer1"].ToString();
label4.Text = rdr["answer2"].ToString();
label5.Text = rdr["answer3"].ToString();
option1 = checkBox1.Checked;
option2 = checkBox2.Checked;
option3 = checkBox3.Checked;
if (checkBox1.Checked == false &&
checkBox2.Checked == false &&
checkBox3.Checked == false)
{
MessageBox.Show("Bifati minim o casuta!");
//imi selecteaza alta intrebare
return;
}
else
{
if ((option1.ToString() == rdr["option1"].ToString()) &&
(option2.ToString() == rdr["option2"].ToString()) &&
(option3.ToString() == rdr["option3"].ToString()))
{
corect++;
label10.Text = corect.ToString();
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
}
else
{
incorect++;
label12.Text = incorect.ToString();
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
}
}
}
}