Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
The following code doesn't working to evaluate the returned value:
Outputs the default Label Name.

C#
SqlDataReader dr = cmd.ExecuteReader();
                
while (dr.Read())
{
..
sex = dr["sex"].ToString();
..
}
if (sex == "Male")
{
   Label1.Text = "Mark";
}
else if(sex == "Female")
{
   Label1.Text = "Marry";
}


How to do this?
Posted
Comments
[no name] 7-Sep-14 16:16pm    
Well since you did not tell us what is in your database, what is coming back or what happened when you ran your code through the debugger, I am going to go ahead and guess that you are not getting either "Male" or "Female" back from your query.
CPallini 7-Sep-14 16:22pm    
What's the purpose of the while statement?
manoj b arman 8-Sep-14 3:27am    
@CPallini
My query
Select * from tbl_name where col_name = 'value'

using datareader

based on the sex retrieved using dr["sex"] //dr my object of datareader class and col.name sex
i want to assign default profile picture according to the user sex

1 solution

Remember that this will be comparing only against the last matching row in the database that your query returns: and we don't know what your query is.
So if you have a dozen rows with "Male" and then one row with "Yes please" then the comparison will only compare "Yes please" against "Male" and "Female" and fail both. As a result, the current content of your label will not be changed.

So add a default for starters that keys you know that there is a problem:
C#
if (sex == "Male")
{
   Label1.Text = "Mark";
}
else if(sex == "Female")
{
   Label1.Text = "Marry";
}
else
{
   Label1.Text = "Unknown " + sex;
}

And from there you should get a better idea - but check your query returns only one row as well... Also remember that string comparisons are case sensitive...
 
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