判断用户名错误还是密码错误怎么会报错的呢
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Collections;
public partial class login : System.Web.UI.Page
{
protected void btnLogin_Click(object sender, EventArgs e)
{
//连接数据库
SqlConnection sqlconstr = new SqlConnection();
sqlconstr.ConnectionString = "server=localhost;database=employee;uid=sa;pwd='111111'";
sqlconstr.Open();
//Response.Write(sqlconstr.State.ToString());
//获取客户端的信息
string strUsername = this.Username.Text.Trim();
string strPassword = this.Password.Text.Trim();
//创建SQL语句
string mysql = "select * from empInfo where username='" + strUsername+ "' and password='" + strPassword + "'";
SqlCommand cmd = new SqlCommand(mysql, sqlconstr);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
//Response.Write("登录成功");
//Response.Write("<script>alert('登录成功');location='emplist'</script>");
// Response.Redirect("emplist.aspx");
string power=dr["per"].ToString();
if (this.userRadio.Checked == true && power == "2")
{
Response.Redirect("emplist.aspx");
}
else
{
if (this.admRadio.Checked == true && power == "1")
{
Response.Redirect("admManage.aspx");
}
else
{
this.logMessage.Visible = true;
this.logMessage.Text = "你的用户权限有误,请重新登录";
}
}
}
else
{
//Response.Write("用户名或密码不正确");
//this.Username.Text = "";
//判断用户名错误还是密码错误
if (dr["username"].ToString() != strUsername)
{
this.logMessage.Visible = true;
this.logMessage.Text = "用户名错误";
}
else
{
this.logMessage.Visible = true;
this.logMessage.Text = "密码错误!";
}
}
dr.Close();
sqlconstr.Close();
}
}
[解决办法]
因为打开的是同一个DataReader
等等,我给你写一下