点击登录按钮登录无反应

sinat_16969897 2014-11-26 11:59:33
protected void btnEnter_Click(object sender, EventArgs e)
{
string UserName = this.UserName.Text.Trim();
string PassWord = this.PassWord.Text.Trim();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);
con.Open();
String str = "select * from Login where UserName='" + UserName + "'and PassWord='" + PassWord + "'";
SqlCommand SqlComm = new SqlCommand(str,con);

// SqlComm.CommandText = "select * from myusertable where text= ' " + UserName + " ' and password= ' " + PassWord + " ' ";
//"select * from Login where UserName = @UserName and PassWord = @PassWord ";
SqlDataReader thisReader = SqlComm.ExecuteReader();
if (thisReader.Read())
{
if (thisReader["UserName"].ToString().Trim() == UserName)
{

Response.Write("登录成功");
}
else
{
Response.Write("登录失败");
}
}
}
...全文
962 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
iiiu_2863645440 2014-11-27
  • 打赏
  • 举报
回复
Web用户登录示例 protected void Button1_Click(object sender, EventArgs e)//获取Web.config中配置的数据库连接字符串 { string sqlstr = System.Configuration.ConfigurationManager.AppSettings["con"].ToString(); SqlConnection conn = new SqlConnection(sqlstr);//连接数据库 conn.Open(); //打开数据库 SqlCommand cmd = new SqlCommand(); //创建SqlCommand对象 cmd.Connection = conn; //设置该对象使用conn连接数据库 cmd.CommandType = CommandType.Text; //设置类型 cmd.CommandText = "select count(*) from Tb_Login where UseName='" + txtUserName.Text.Trim() + "' and UsePassword='"+txtPwd.Text.Trim()+"'"; //设置sql语句 int flag = int.Parse(cmd.ExecuteScalar().ToString());//执行sql语句并获取返回值 if (flag > 0) //如果大于0 { Page.ClientScript.RegisterStartupScript(this.Parent.GetType(), "", "alert('登录成功');", true);//说明登陆成功 } else { Page.ClientScript.RegisterStartupScript(this.Parent.GetType(), "", "alert('登录失败');", true);//否则登陆失败 } conn.Close();//关闭连接 }
枫0子K 2014-11-27
  • 打赏
  • 举报
回复
1. 很笨,你没有注册点击事件 2. thisReader.Read()判断等于false 断点跟下面代码应该有问题。 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString); con.Open(); SqlCommand SqlComm = new SqlCommand(str,con); 3. 整一个事件try-catch看有没有捕获什么异常
蜗牛慢慢趴 2014-11-26
  • 打赏
  • 举报
回复
if (thisReader.Read()) 没读到值 肯定前面错了
exception92 2014-11-26
  • 打赏
  • 举报
回复
and PassWord 中and前加 个空格。
於黾 2014-11-26
  • 打赏
  • 举报
回复
断点跟,应该是报错了,仔细看报的什么错

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧