2017-09-26 16 views
-1

私は自分の役割に基づいて各ユーザーをリダイレクトしようとしています。私は同じページから両方のログインユーザーと管理者を持っていますが、私の場合、 「あなたは無効なユーザー名またはパスワードを入力しました」というメッセージが表示されます。何かご意見は。もっとして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(); 
} 
+0

ExecuteNonQueryはここでは間違った方法です。何をあなたはそれを選んだのですか?おそらく 'ExecuteScalar'を使用し、' select * 'ではなく 'select count(*)'で始まるSQL文を使うべきです。 https://stackoverflow.com/questions/4269548/executenonquery-for-select-sql-statement-returning-no-rows –

答えて

0

あなたは間違ってadminのcmdオブジェクトを渡していますが、cmd1にする必要があります。また、データテーブルはdt1でなければなりません。

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(); 
+1

executenonqueryからデータテーブルを読み込むことができますか? –

+0

ありがとう、私はそれに気付かなかった。今すぐ答えを更新しました。 –

+0

ありがとうございました –

1
if (i == 1) 
    { 
     Session["admin"] = TextBox1.Text; 
     Response.Redirect("add_product.aspx"); 
    } 
    else 
    { 
     Label1.Text = "you have entered invalid username or password"; 
    } 

であるあなたはチェックしましたか?

+0

基本的にadminには自分の名前とパスワードのみのテーブルがあり、管理者は1人だけですはい。一方のユーザーは別のテーブルにあります –

関連する問題