C#if语句(从sql server DB读取参数 [英] C # if statement (parameter read from sql server DB

查看:134
本文介绍了C#if语句(从sql server DB读取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,



我是编程的初学者,我无法使下面的代码正常运行。



当我使用两个Response.write(角色)语句时,代码执行正常,但是当我评论它们并使用Response.write(users page)和Response.write(admins Page ),我总是收到回复:管理员页面。



有人可以帮忙解释为什么会发生这种情况吗?





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?


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");
 
                        }

推荐答案

1。在行上设置一个断点:if(role ==user)



2.运行你的应用程序,仔细检查变量'role的内容。内容出现是用户吗?



3.打开命令窗口并检查'角色的内容长度: ? role.Length;



很明显,'role的内容永远不会等于字符串user:变量'role中可能有一个特殊字符。如果'内容的长度'角色不等于#4,那将证实该假设。
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.


这篇关于C#if语句(从sql server DB读取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆