Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
In my table there is a column name 'Gender' and say its value is Male stored in that column. And now i want to retrieve the value of that column in the RadioButton. And based on the value of Gender Column automatically correspond radio button get selected.
Here is some code for your references
C#
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCN"].ConnectionString))
                    {
        SqlCommand cmd = new SqlCommand("Select * from EmpRegister", con);
                        con.Open();
                        
                        SqlDataAdapter da = new SqlDataAdapter();
                        
                        DataTable dt = new DataTable();
                        da.SelectCommand = cmd;
                        da.Fill(dt);
                        if (dt != null && dt.Rows.Count > 0)
                        {
        
        
                            txtName.Text = dt.Rows[0]["EmpName"].ToString();
                            txtPassword.Text = dt.Rows[0]["Password"].ToString();
        
        string Gender = null;
                            if (radioButtonMale.Checked)
                            {
                                Gender = "Male";
                            }
                            else
                            {
                                Gender = "Female";
                            }
                            radioButtonMale.Checked = dt.Rows[0]["Gender"].ToString();  
        }

In last line i am getting error saying cannot implicitly convert string to bool.
Posted
Updated 11-Jul-14 1:33am
v2

.Checked on a radio button is either true or false. So, you can't set it's value to "Male".

Do this instead:

C#
radioButtonMale.Checked = dt.Rows[0]["Gender"].ToString().ToLower()=="male"; 
 
Share this answer
 
Comments
Member 10276989 12-Jul-14 1:54am    
Thank you RyanDev
I have not gone through the code but these lines makes no sense
C#
if (radioButtonMale.Checked)
{
  Gender = "Male";
}
else
{
  Gender = "Female";
}
radioButtonMale.Checked = dt.Rows[0]["Gender"].ToString();  

Instead of checking radioButtonMale.Checked, you should check for the received value. If its Male,set
C#
radioButtonMale.Checked=true;

It only accepts boolean values.

Regards..
 
Share this answer
 
v2
C#
if (dt.Rows[0]["Gender"].ToString() == "Male")
                    {
                        radioButtonMale.Checked = true;

                    }

                    else if (dt.Rows[0]["Gender"].ToString() == "Female")
                    {
                        radioButtonFemale.Checked = true;
                   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900