Click here to Skip to main content
15,896,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day all,

I am a beginner in programing and I can't get the below code to function properly.

when I use the two Response.write(role)statements, the code executes fine but when I comment them and use Response.write("users page") and Response.write("admins Page"), I always get the response: "admins page".

can someone please help and explain why this is happening?


C#
myCon.Open();
 
                        SqlCommand com3 = new SqlCommand("checkRole", myCon);
                        com3.CommandType = CommandType.StoredProcedure;
                        com3.Parameters.AddWithValue("@uName", TBoxUName.Text);
                        String role = com3.ExecuteScalar().ToString();
 
                        myCon.Close();
 

 
                        if (role == "user")
                        {
 
                            Response.Write("Users Page");
 
                            //Response.Write(role);

                        }
 
                        else
                        {
 
                            //Response.Write(role);
                            Response.Write("Admins Page");
 
                        }
Posted
Updated 28-Feb-14 9:08am
v2
Comments
vv_shameer 28-Feb-14 15:06pm    
Are you sure? I don't think there is a problem with your code
Pls make sure your stored procedure is not giving the same vslue everytime.
If everything is fine from SP and this is the code you are tesing with, it should work perfectly

1 solution

1. Put a break-point on the line: if (role == "user")

2. Run your application, and carefully examine the contents of the variable 'role. Do the contents appear to be "user" ?

3. Open a command window and examine the length of the contents of 'role: ? role.Length;

It's obvious that the contents of 'role are never equal to the string "user" : there could be a special character in the variable 'role. If the 'Length of the contents of 'role is not equal to #4, that would confirm that hypothesis.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Feb-14 15:50pm    
Good advice, a 5.
—SA

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