Click here to Skip to main content
15,908,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (dt.Rows[0]["Gender"] == "Male")
                                   {
                                       rdomale.Checked = true;
                                   }
                                   else
                                   {
                                       rdofemale.Checked = true;
                                   }
Posted
Updated 7-May-20 9:17am

bring "Male" first in conditon
 
Share this answer
 
Hi,

Update your code as below.
C#
if(dt!=null &&dt.Rows.Count>0 && Convert.ToString(dt.Rows[0]["Gender"]).Equals("Male"))
{
 rdomale.Checked = true;
}
else
{
 rdofemale.Checked = true;
}


Here Convert.ToString also checks for null exception.

Hope this will help you.
 
Share this answer
 
v2
Hi
Try this.

C#
if (dt.Rows.Count>0 && dt.Rows[0]["Gender"].ToString().Equals("Male"))
{
  rdomale.Checked = true;
}
else
{
  rdofemale.Checked = true;
}
 
Share this answer
 
You are getting problem because table has no rows.
Try this:

C#
rdomale.Checked=(dt.Rows.Count>0 && dt.Rows[0]["Gender"].ToString() == "Male");
rdofemale.Checked = (!rdomale.Checked);
 
Share this answer
 
v3
dt.Rows[0]["Gender"] is an object - add .ToString() at the end to get the string value.
 
Share this answer
 
Comments
zeshanazam 20-Nov-12 4:41am    
nothing happend, same warning exists.
Pete O'Hanlon 20-Nov-12 4:45am    
Does your line now read if (dt.Rows[0]["Gender"].ToString() == "Male") ?
Pete O'Hanlon 20-Nov-12 4:46am    
If that's not working for you, change it to this: if ((string)dt.Rows[0]["Gender"] == "Male")

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