C# 基于角色的登录不';t验证管理员

C# 基于角色的登录不';t验证管理员,c#,sql,asp.net,C#,Sql,Asp.net,因此,我试图根据每个用户的角色重定向他们。我让用户和管理员都从同一个页面登录,但在我的情况下,它只验证用户,而不是管理员。它显示我的“您输入了无效的用户名或密码”消息。任何想法。谢谢 这是我的密码 protected void Button1_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandType = CommandType.

因此,我试图根据每个用户的角色重定向他们。我让用户和管理员都从同一个页面登录,但在我的情况下,它只验证用户,而不是管理员。它显示我的“您输入了无效的用户名或密码”消息。任何想法。谢谢

这是我的密码

protected void Button1_Click(object sender, EventArgs e)
{
    con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select * from registration where email='"+ TextBox1.Text+"' and password='"+TextBox2.Text+"'";
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    tot = Convert.ToInt32(dt.Rows.Count.ToString());

    if (tot > 0)
    {
        if (Session["checkoutbutton"] == "yes")
        {
            Session["user"] = TextBox1.Text;
            Response.Redirect("update_order_details.aspx");
        }
        else
        {
            Session["user"] = TextBox1.Text;
            Response.Redirect("order_details.aspx");
        }

    }
    else
    {
        Label1.Text = "Invalid email or password";
    }
    con.Close();

    con.Open();
    SqlCommand cmd1 = con.CreateCommand();
    cmd1.CommandType = CommandType.Text;
    cmd1.CommandText = "select * from admin_login where username='" + TextBox1.Text + "' and password='" + TextBox2.Text + "' ";
    cmd1.ExecuteNonQuery();
    DataTable dt1 = new DataTable();
    SqlDataAdapter da1 = new SqlDataAdapter(cmd);
    da1.Fill(dt);
    i = Convert.ToInt32(dt.Rows.Count.ToString());
    if (i == 1)
    {
        Session["admin"] = TextBox1.Text;
        Response.Redirect("add_product.aspx");
    }
    else
    {
        Label1.Text = "you have entered invalid username or password";
    }
    con.Close();
}

是否检查了行数是否超过1行或0行?

您错误地将cmd对象传递给admin,它应该是cmd1。此外,对于admin,数据表应该是dt1而不是dt

int i;
con.Open();
SqlCommand cmd1 = con.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "select count(*) from admin_login where username='" + TextBox1.Text + "' and password='" + TextBox2.Text + "' ";
i = cmd1.ExecuteScalar();
if (i == 1)
{
    Session["admin"] = TextBox1.Text;
    Response.Redirect("add_product.aspx");
}
else
{
    Label1.Text = "you have entered invalid username or password";
}
con.Close();

基本上管理员有他自己的表,只有他的名字和密码,只有一个管理员,所以只有一行是的。另一方面,用户在另一个表上
ExecuteNonQuery
是这里使用的错误方法。是什么让你选择了那个?您可能应该使用
ExecuteScalar
,使用以
select count(*)开头的SQL语句,而不是
select*
。您甚至可以从ExecuteOnQuery中加载数据表吗?谢谢,我没有注意到这一点。现在更新答案。
int i;
con.Open();
SqlCommand cmd1 = con.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "select count(*) from admin_login where username='" + TextBox1.Text + "' and password='" + TextBox2.Text + "' ";
i = cmd1.ExecuteScalar();
if (i == 1)
{
    Session["admin"] = TextBox1.Text;
    Response.Redirect("add_product.aspx");
}
else
{
    Label1.Text = "you have entered invalid username or password";
}
con.Close();