私は自分の役割に基づいて各ユーザーをリダイレクトしようとしています。私は同じページから両方のログインユーザーと管理者を持っていますが、私の場合、 「あなたは無効なユーザー名またはパスワードを入力しました」というメッセージが表示されます。何かご意見は。もっとして1つのまたは0の行がある場合のおかげロールに基づくログインは、管理者を認証しません
ここでは私のコードは
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();
}
ExecuteNonQueryはここでは間違った方法です。何をあなたはそれを選んだのですか?おそらく 'ExecuteScalar'を使用し、' select * 'ではなく 'select count(*)'で始まるSQL文を使うべきです。 https://stackoverflow.com/questions/4269548/executenonquery-for-select-sql-statement-returning-no-rows –