ログインページを作成しようとしていますが、executereader を指定すると機能し、executenonquery を指定すると 1 ではなく -1 の値が返されます。
これは、cmd.executenonquery() で -1 を返します。
SqlCommand cmd = new SqlCommand("select Count(*) from userDb where username= '"+txtusername.Text+"' and password= '"+txtpassword.Text+"'", con);
executereader() を使用した以下のコード
SqlCommand cmd = new SqlCommand("select Count(*) from userDb where username= @p1 and password= @p2", con);
**Complete Code**
SqlCommand cmd = new SqlCommand("select Count(*) from userDb where username= @p1 and password= @p2", con);
cmd.Parameters.AddWithValue("@p1", txtusername.Text);
cmd.Parameters.AddWithValue("@p2", txtpassword.Text);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read()==true)
{
FormsAuthentication.RedirectFromLoginPage(txtusername.Text, CheckBox1.Checked);
}
else
{
lbldisplay.Text = "Username and Password Do not Match";
}
con.Close();
executenonquery を使用したコード
SqlCommand cmd = new SqlCommand("select Count(*) from userDb where username= '"+txtusername.Text+"' and password= '"+txtpassword.Text+"'", con);
con.Open();
int i = executenonquery();
if (i == 1)
{
FormsAuthentication.RedirectFromLoginPage(txtusername.Text, CheckBox1.Checked);
}
else
{
lbldisplay.Text = "Username and Password Do not Match";
}
con.Close();