asp.net中的一个问题!

来源:百度知道 编辑:UC知道 时间:2024/05/21 11:57:46
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
//与数据库的连接
SqlConnection conn = GetConnection ();
conn.Open();
string sqlStr;
sqlStr = "select * from Tb_Login where UseName='" + Login1.UserName + "' and UsePassword='" + Login1.Password + "'";

SqlDataAdapter da = new SqlDataAdapter(sqlStr, conn);
DataSet ds = new DataSet();
da.Fill(ds);

if (!(ds.Tables.Count < 0))
{
Response.Redirect("NavigatePage.aspx?UserName=" + Login1.UserName.ToString ());
}
else
Response.Write("登录失败");
da.Dispose();
ds.Dispose();
conn.Close();
}
这个是LOGIN控件的authenticate事件
为什么不管我输入的是什么用户密码,数据库中有没有,都会跳转到NavigatePage.aspx页 我用response.write(ds.Tables.Count)测试了

ds.Tables.Count至少应该是1吧...即使你的记录为空,也肯定存在ds.Tables[0]这个DataTable,就比如说string str=null;这样声明一个字符串变量,你能说str不存在吗?
所以要判断应该这样
SQL语句改为
sqlStr = "select count(*) from Tb_Login where UseName='" + Login1.UserName + "' and UsePassword='" + Login1.Password + "'";
if(ds.Tables[0].Row[0][0].ToString()=="0")
{
//登陆失败
}
else{//登陆成功}

自己写登陆方法吧.
LOGIN并不好用.会生成很多无用的代码.并且出了问题难以控制.

/与数据库的连接
SqlConnection conn = GetConnection ();
conn.Open();
string sqlStr;
////到数据库测试一下
sqlStr = "select count(*) from Tb_Login where UseName='" + Login1.UserName + "' and UsePassword='" + Login1.Password + "'";
SqlCommand cmd=new SqlCommand(sqlStr,conn)
int ncont=Convert.ToInt32(cmd.ExecuteScalar());
if (!ncont==0))
{
Response.Redirect("NavigatePage.aspx?UserName=" + Login1.UserName.ToString ());
}
else