自己做了一个登陆验证控件,大体上能验证通过,只不过还有一点异常不知道在哪里
下边是我的代码,主要是看红色部分
try
{
string constr = ConfigurationManager.ConnectionStrings["db1"].ConnectionString;
string sql = "select username from userlogin where id='" + TxtUserName.Text.Trim() + "' and password='" + TxtUserPwd.Text.Trim() + "'";
SqlConnection connection = new SqlConnection(constr);
//SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, connection); //创建适配器
DataSet ds = new DataSet(); //创建数据集
int count = da.Fill(ds, "table");
if (count > 0)
{
SqlCommand command = new SqlCommand(sql, connection);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
Session["user"] = dr["username"].ToString();
dr.Close();
Response.Redirect("~/Default.aspx");
} else
{
Label2.Text = "登录失败";
}
connection.Close();
}
catch
{
Response.Redirect("~/web/errormessage/loginerror.aspx");
}
当使用合法用户登录时,可以登陆进去,但是会跳转到catch语句里的界面,而输入错误用户信息时只显示“登陆失败”,不会跳转到catch里的页面,所以我想应该是红色部分出错了,哪位可以指点一下
------解决方案--------------------
你确认reader中能读到内容吗,你还是调试一下吧
------解决方案--------------------
第一次看见有人这么做验证。。。太雷
------解决方案--------------------
int count = da.Fill(ds, "table");有问题吧
------解决方案--------------------
看错
------解决方案--------------------
if (ds.Tables[0].Row.Couunt> 0)
{
if(dr.Read(){}
------解决方案--------------------
Response.Redirect("~/Default.aspx");
不能用这个, 因为你代码没有执行 完成 你就 跑了就有问题了
1. 你要不用 Response.Redirect(url,bool) 这个方法。
2. script 的 windows.loaction.href 或 windows.href