Hi Guys
I have got 4 radio buttons on my form and & i am trying to match the text for these radiobuttons with a record in one of the database tables. Essentially I would like when the submit button is clicked to match if the selected or clicked radiobutton Text with a database record. And if this matches a messagebox to be displayed saying "match" and if not matched the messagebox should show "no match found"

What I have done so far:

private void button1_Click(object sender, EventArgs e)
        {
         bool correct;
        String Option1 = "";
        String Option2 = "";
        String Option3  = "";
        String Option4 = ""; 



           Option1 = radioButton1.Text;
            Option2 =radioButton2.Text;
            Option3 = radioButton3.Text;
            Option4 = radioButton4.Text;

            Query = "SELECT * FROM Emp;";
             theReader = conn.ExecuteStatement(Query);
                while (theReader.Read())
                {


                        if (radioButton1.Checked ||radioButton2.Checked || radioButton3.Checked ||radioButton4.Checked)
                        {

                            Option1 = (theReader["Emp_1"].ToString());
                            Option2= (theReader["Emp_1"].ToString());
                            Option3= (theReader["Emp_1"].ToString());
                            Option4= (theReader["Emp_1"].ToString());

                            correct = true;
                            break;

                        }
                    }

                if (correct)
                {
                    MessageBox.Show("match");

                }
                else
                {
                     MessageBox.Show("no match found");
                }


                }


        }

The code below retrieves the Employees names and puts these names on to the radiobuttons.

         Query = "SELECT * FROM Emp;";
                        theReader = conn.ExecuteStatement(Query);
                        while (theReader.Read())
                        {

                            radioButton1.Text = theReader["Emp_1"].ToString();
                            radioButton2.Text = theReader["Emp_2"].ToString();
                            radioButton3.Text = theReader["Emp_3"].ToString();
                           radioButton4.Text = theReader["Emp_4"].ToString();


                        }

in this case the "Emp_1" is the matching record , when I run my project when other options or radio buttons are clicked it executes the messageBox showing that it matches even if its not a matching record.
What I am trying to say is that it does not execute the else {MessageBox.Show("no match found");} when records don't match.

can someone pls help.

In line 22 you stated that if any of the 4 checkboxes are checked then ....(line 30): correct = true.

In line 36, if correct is true, it is a match.

Edited 2 Years Ago by cgeier

Also, if only one radio button should be selected at a time group them together by placing them on a Panel or on a GroupBox.

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