注册 登录
编程论坛 C# 论坛

新手求教在c#窗体程序设计完成,运行程序出现当前上下文中不存在文件名txtUserName

EBC1 发布于 2015-04-16 09:51, 628 次点击
private void button1_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text.Trim() == "" || txtUserPassword.Text.Trim == "")
            {
                MessageBox.Show("用户名或密码不能为空!");

                txtUserName.Focus();
                return;
            
            
            }

            string connString = @"Data Source=windows-PC;Initial Catalog=AddressList;Integrated Security=True";
            string sqlStr = string.Format("select count(*) from [User] where UserName='{0}' and Password='{1}'",txtUserName.Text.Trim,txtUserPassword.Text.Trim());
            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlCommand amd = new SqlCommand(sqlStr, conn);
                conn.Open();
                int  n=Convert.ToInt32(cmd.ExecuteScalar());
                if (n == 1)
                {
                    MessageBox.Show("登录成功!");


                }
                else
                {
                    MessageBox.Show("用户名或密码错误!","错误");
                    txtUserName.Text = "";
                    txtUserPassword.Text = "";
                    txtUserName.Focus();
               
               
                 }
3 回复
#2
Maick2015-04-16 10:11
if (txtUserName.Text.Trim() == "" || txtUserPassword.Text.Trim == "")
            {
                MessageBox.Show("用户名或密码不能为空!");
                //注释掉吧.提示就没有这个名叫:txtUserName的textbox
               // txtUserName.Focus();
                return;
            
            
            }
#3
hellation2015-04-18 22:04
肯定是你最初的时候名命名的,然后删掉了,可是这样应该提示(下面有红线啊),何况这行代码没有什么意义   删了吧
#4
sun2824307242015-04-20 13:35
新手路过.楼主加油!!
1