下面我的代码非常好并且工作但是我想检查connec.reader.HasRows然后将用户重定向到page.aspx否则弹出一条消息“你还没有注册”。请任何人都可以帮助我,为什么这不起作用。感谢帮助。
此代码来自我的经理类。
public static void Insert(string _username, string _password)
{
Connection connec = new Connection();
connec.SqlQuery("select * from manager where UserName='@username' AND Password='@password'");
connec.cmd.Parameters.AddWithValue("@username", _username);
connec.cmd.Parameters.AddWithValue("@password", _password);
connec.QueryReader();
}
以下代码来自page.aspx页面以对该方法采取行动。
protected void Button1_Click(object sender, EventArgs e)
{
ManagerClass connec = new ManagerClass();
ManagerClass.Insert(TextBox1.Text, TextBox2.Text);
}
答案 0 :(得分:0)
if (TextBox1.Text != string.Empty && TextBox2.Text != string.Empty)
{
Connection connec = new Connection();
SqlCommand cmd = new SqlCommand("SELECT * FROM manager WHERE userName=@userName and password=@password", connec);
cmd.Parameters.AddWithValue("@userName", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
// do something here. (ex: get logs/date/session datas)
}
else
{
// you have not registered! (Show your warning message here)
Response.Redirect("register.aspx"); //(or use your popup here)
}
con.Close();
cmd.Dispose();
Response.Redirect("page.aspx");
}
else
{
// _userName and _password cannot be null! (your message here)
}
答案 1 :(得分:0)
您需要纠正的代码结构。您应该使用方法void Insert
,而不是bool IsValidUser
方法。此方法也不一定是静态方法。它应该是实例方法。
public bool IsValidUser(string _username, string _password)
{
Connection connec = new Connection();
connec.SqlQuery("select COUNT(*) from manager where UserName=@username AND Password=@password");
connec.cmd.Parameters.AddWithValue("@username", _username);
connec.cmd.Parameters.AddWithValue("@password", _password);
int userCount = (int) sqlCommand.ExecuteScalar();
con.Close();
cmd.Dispose();
return userCount != 0;
}
现在在您的页面中调用此方法并检查方法返回的值。如果它为假,则显示消息,然后将用户重定向到page.aspx。
protected void Button1_Click(object sender, EventArgs e)
{
ManagerClass managerObj = new ManagerClass();
if(managerObj.IsValidUser(TextBox1.Text, TextBox2.Text))
{
Response.Redirect("page.aspx");
}
else
{
//Show message "you have not registered";
}
}