Click here to Skip to main content
15,868,098 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when ever i display a result from my database, the radio buttons alway returns
"Female" even if it is "Male" in the database.
help me out


C#
if (person.sex.ToString() == "Male")
                    {
                        optMale.Checked = true;
                    }
                    else
                    {
                        optFemale.Checked = true;
                    }
Posted

1 solution

The problem is somewhere else. It does not mean that sex.ToString() returns "Female", it simply never returns "Male". Generally, comparison with a string is pretty sloppy anyway. Use the Debugger in all cases.

—SA
 
Share this answer
 
Comments
loctrice 15-Jan-12 23:01pm    
What language is this? If you can use .Equals() for strings, then it's probably not a bad idea. I've had string matching issues before because of this.
Sergey Alexandrovich Kryukov 15-Jan-12 23:15pm    
Bad idea, bad. Sex should a the enum. The language does not really matter; this is C#, see the question.
--SA
loctrice 15-Jan-12 23:30pm    
Yeah, I should have paid more attention to the language in the title... the language does in fact matter for string comparisons. Even if it 'should' be an enum, it 'is' a string
Sergey Alexandrovich Kryukov 15-Jan-12 23:41pm    
The language does not matter in that sense. String comparison is part of .NET library, not the language.
--SA
loctrice 15-Jan-12 23:49pm    
Yes, it was my mistake in not seeing the language in the post. I am new to answering questions on cp, and I was just viewing all unanswered questions.
.Net , it does not matter if you use == vs. .Equals(). Java, it matters.

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