Answered by:
Radio button does not get selected

Question
-
I have some code, in which I want to select one of the two radio buttons on my form, based on the data in the database. The code is as below:
string gender; using (OleDbConnection cn = new OleDbConnection(connectionString)) { cn.Open(); string scmd = "select [Customer_Name], [Address], [Contact_No], [Gender], [Issued_By] from Regular_Customers where Customer_Name = '" + cBoxCustomers.SelectedItem.ToString() + "'"; OleDbCommand cmd = new OleDbCommand(scmd, cn); OleDbDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { txtName.Text = sdr[0].ToString(); ; txtAddress.Text = sdr[1].ToString(); txtContactNo.Text = sdr[2].ToString(); gender = sdr[3].ToString(); MessageBox.Show("Gender is: "+gender); if (gender == "M") { radioBtnMale.Select(); } if (gender == "F") { radioBtnFemale.Select(); } txtIssuedBy.Text = sdr[4].ToString(); } }
I have used this MessageBox to see what is getting stored in the variable gender. The value M/F are coming properly in the gender variable. But the radio buttons does not get selected. Can anyone suggest the solution?
- Edited by tapan.desai Monday, September 26, 2011 3:51 PM
Answers
-
Try this
if (gender == "M") { radioBtnMale.Checked = True; } if (gender == "F") { radioBtnFemale.Checked = True; }
Happy Coding, RDRaja- Edited by Dharmalinga Raja Tuesday, September 27, 2011 1:04 AM
- Proposed as answer by Louis.fr Wednesday, September 28, 2011 8:54 AM
- Marked as answer by tapan.desai Thursday, September 29, 2011 7:06 AM
All replies
-
Try this
if (gender == "M") { radioBtnMale.Checked = True; } if (gender == "F") { radioBtnFemale.Checked = True; }
Happy Coding, RDRaja- Edited by Dharmalinga Raja Tuesday, September 27, 2011 1:04 AM
- Proposed as answer by Louis.fr Wednesday, September 28, 2011 8:54 AM
- Marked as answer by tapan.desai Thursday, September 29, 2011 7:06 AM
-
-
-
This is a sign that you did not create the radio buttons properly.
The radio buttons need to be associates to each other.
You should use a panel, groupbox or create a radio button list in order to achieve this.
Noam B.
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you... -
If you are sure the gender is only F or M than only select one of those.
A radio button in a container which is set will direct deselect all other radio buttons in the containers.
If you want that you can have both F and M gender the same time use a checkbox with the radiobutton property for that set to true.
Success
Cor -
-
Hi tapan.desai,
Welcome to the MSDN forum!How is it going with the question?
Please refer to the following link to get more information about RadioButton.Checked Property:
http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.checked.aspxHave a nice day!
Yoyo Jiang[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
-
Congratulations!
Yoyo Jiang[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.