i makinh MCQs question as my mini project in windows form application i m facing few problem plzz solve that

1. to count the score of total correct question and show at the last in windows form

2. when i selct any option(radiobutton) then next question appears with same number of option as marked e.g if i selct option 2 in 1st question then 2nd question 2nd option appears as selected .. i wanted that options appears randmoly or blank.

private void Form1_Load(object sender, EventArgs e)
{
string constring ="Server=TAHIRA-7B0ECEAE\\SQLEXPRESS;integrated security=SSPI;database=MCQ";
SqlConnection conn = new SqlConnection (constring);
conn.Open();
string Sql = "select * from table_1";
SqlCommand cmd = new SqlCommand(Sql,conn);
SqlDataReader reader = cmd.ExecuteReader(); 
[B]Int score= 0;[/B]
while(reader.Read());

label1.Text=reader[0];
radioButton1.Text=reader[1].Tostring;
radioButton2.Text=reader[2].Tostring;
radioButton3.Text=reader[3].To.string;
[B]if(radiobutton1.checked=true)
{
score +1;
}
cmd.ExecuteNonQuery();

it must count score ov all question.. in which button i ve to code

Edited 5 Years Ago by __avd: Added [code]..[/code] tags.

The answer of second question is that before loading next question refresh radio buttons by explicitly setting the checked states to false... Try this code and add a column answer in you table which contains the answer of each question

String sa = "";
     int score = 0;
        RadioButton rdanswer = new RadioButton();
        private void Form1_Load(object sender, EventArgs e)
        {
string constring ="Server=TAHIRA-7B0ECEAE\\SQLEXPRESS;integrated security=SSPI;database=MCQ";
SqlConnection conn = new SqlConnection (constring);
conn.Open();
string Sql = "select * from table_1";
SqlCommand cmd = new SqlCommand(Sql,conn);
SqlDataReader reader = cmd.ExecuteReader(); 
     LoadQuestion()
        }

        private void button1_Click(object sender, EventArgs e)
        {


            if (radioButton1.Checked == true)
                rdanswer = radioButton1;

            else if (radioButton2.Checked == true)
                rdanswer = radioButton2;
            else if (radioButton3.Checked == true)
                rdanswer = radioButton3;

            if (rdanswer.Text == sa)
                    score++;
LoadQuestion()
        }
public void LoadQuestion()
{
radioButton1.Checked=true;// with every question the radiobutton 1 will be selected
if(reader.Read())
{
label1.Text=reader[0];
radioButton1.Text=reader[1].ToString;
radioButton2.Text=reader[2].ToString;
radioButton3.Text=reader[3].ToString;
sa=reader["answer"].ToString();//the column which carries true answer of the question
}

}
        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("your score is " + score);
        }

Edited 5 Years Ago by abelLazm: n/a

radioButton1.Checked=true;// with every question the radiobutton 1 will be selected

Edited 5 Years Ago by IT seeker: double reply

radioButton1.Checked=true;// with every question the radiobutton 1 will be selected

with every question radiobutton1 should not be slected ... ... there must be appear blank or any other radiobuuton should be slected

Ok then try this code to load data it will make all the radiobuttons unchecked every time

public void LoadQuestion()
{
radioButton1.Checked=false;
radioButton2.Checked=false;
radioButton3.Checked=false;
if(reader.Read())
{
label1.Text=reader[0];
radioButton1.Text=reader[1].ToString;
radioButton2.Text=reader[2].ToString;
radioButton3.Text=reader[3].ToString;
sa=reader["answer"].ToString();//the column which carries true answer of the question
}

}
public void LoadQuestion()
{
radioButton1.Checked=false;
radioButton2.Checked=false;
radioButton3.Checked=false;
if(reader.Read())

..... to do in property window nad to write the code as above are same or differnt impact on OUTPUT .. because i do that in property window of radiobuttons it blank all the buttons but when we want to select any option it did not marked ... that's why im asking ...

write this in code .... can you please show me your work at the current stage so that able to help you

yeah i wanti to show here but when i "print screen"and paste in paint for screen shots... that did not upload here becoz of page size ...

any other method...

Check the images i have attached and follow this method to upload snapshots of your project

Attachments 1.png 14.1 KB 2.png 34.83 KB

i write the following code . but eception comes at "reader.reard".. 1st question retrive but when i click on next button it give exception..plz check it

public partial class Form1 : Form
    {
        SqlDataReader reader;
        public Form1()
        {
            InitializeComponent();
        }
        string sa = "";
        int score = 0;
        RadioButton rdanswer = new RadioButton();

        private void Form1_Load(object sender, EventArgs e)
        {
            string constring = "Server=TAHIRA-7B0ECEAE\\SQLEXPRESS;integrated security=SSPI;database=stu";
            SqlConnection conn = new SqlConnection(constring);
            conn.Open();
            string Sql = "select * from Table_1";
            SqlCommand cmd = new SqlCommand(Sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();

            if (radioButton1.Checked == true)
                rdanswer = radioButton1;
            else if (radioButton2.Checked == true)
                rdanswer = radioButton2;
            else if (radioButton3.Checked == true)
                rdanswer = radioButton3;

            if (rdanswer.Text == sa)
                score++;
            if(reader.Read())
            {
                label1.Text = reader[0].ToString();
            radioButton1.Text = reader[1].ToString();
            radioButton2.Text = reader[2].ToString();
            radioButton3.Text = reader[3].ToString();
            sa = reader["sa"].ToString();

          
        }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
                rdanswer = radioButton1;
            else if (radioButton2.Checked == true)
                rdanswer = radioButton2;
            else if (radioButton3.Checked == true)
                rdanswer = radioButton3;

            if (rdanswer.Text == sa)
                score++;
            if
            (reader.Read())
            {

                label1.Text = reader[0].ToString();
                radioButton1.Text = reader[1].ToString();
                radioButton2.Text = reader[2].ToString();
                radioButton3.Text = reader[3].ToString();
                sa = reader["sa"].ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("your score is" + score);
        }
    }
}
(reader.Read())ecxeption come on this line 
            {
 
                label1.Text = reader[0].ToString();
                radioButton1.Text = reader[1].ToString();
                radioButton2.Text = reader[2].ToString();
                radioButton3.Text = reader[3].ToString();
                sa = reader["sa"].ToString();
            }
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("your score is" + score);
        }
    }
}

plz solve it

Exception Handling

in which no error and no warning appear , a yellow box point out the specefic line....

i only know this .............

Add a try catch block around your code and like this

try
{
//your code
}
catch( Exception str)
{
MessageBox.Show(str.Message + " : Exception raised in " + str.TargetSite.ToString() , str.Source.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
}

it will tell you exactly where the exception occured

This article has been dead for over six months. Start a new discussion instead.