I'm trying to create a login page and when i give executereader it works and when i give executenonquery it returns a -1 value instead of 1.
This return -1 on cmd.executenonquery()
SqlCommand cmd = new SqlCommand("select Count(*) from userDb where username= '"+txtusername.Text+"' and password= '"+txtpassword.Text+"'", con);
Below code with 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();
Code with 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();
ExecuteNonQuery
SqlCommand.ExecuteNonQuery MSDN Documentation
ExecuteReader
SqlCommand.ExecuteReader MSDN Documentation