i am using this code for changing password when i enter correct password its ok password is updated. but when i enter wrong password no error messag is shown whats wrong with code
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = (@"Data Source=heaven_prince20;Initial Catalog=final_pr;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT password FROM login WHERE Password='" +Convert.ToInt32( textBox1.Text )+ "'", con);
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if ((Convert.ToInt32( (dr["password"])) !=Convert.ToInt32( ( textBox1.Text))||Convert.ToInt32( textBox2.Text)!=Convert.ToInt32( textBox3.Text)))
{
MessageBox.Show("old password is worng");
}
else
{
dr.Close();
SqlCommand cmd1 = new SqlCommand("UPDATE login SET password='" + Convert.ToInt32(textBox2.Text) + "' WHERE password='" + textBox1.Text + "'", con);
cmd1.ExecuteNonQuery();
MessageBox.Show("Record Update Successfully");
}
Loading
Posted Aug 6, 2013, 4:35 AM
When there is no records, dr will not have any rows to process.
Before processing datareader, check dr.HasRows that will tell you, that the datareader has any rows to process or not.
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = (@"Data Source=heaven_prince20;Initial Catalog=final_pr;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT password FROM login WHERE Password='" +Convert.ToInt32( textBox1.Text )+ "'", con);
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
if ((Convert.ToInt32( (dr["password"])) !=Convert.ToInt32( ( textBox1.Text))||Convert.ToInt32( textBox2.Text)!=Convert.ToInt32( textBox3.Text)))
{
MessageBox.Show("old password is worng");
}
else
{
dr.Close();
SqlCommand cmd1 = new SqlCommand("UPDATE login SET password='" + Convert.ToInt32(textBox2.Text) + "' WHERE password='" + textBox1.Text + "'", con);
cmd1.ExecuteNonQuery();
MessageBox.Show("Record Update Successfully");
}
}
else
{
MessageBox.Show("No records found");
}
}
Iftikar HussainPosted Aug 6, 2013, 5:24 AM
Try like this
Regards,
Iftikar
ta muPosted Aug 6, 2013, 4:31 AM
Posted Aug 6, 2013, 4:27 AM
During the password change, you need to consider the user id also, but you're matching only the password. What in case, if multiple users have the same password ? It will change the password for all the users whose having the same password.
//Select query
SqlCommand cmd = new SqlCommand("SELECT password FROM login WHERE userid = " +
SqlCommand cmd1 = new SqlCommand("UPDATE login SET password='" + Convert.ToInt32(textBox2.Text) + "' WHERE password='" + textBox1.Text + "'" + "and id =" +
Hope this helps you.