-3

The update simply doesn't work, the row stays the same .I get no error.Does anyone know the reason why it doesn't work ? I'd like to know firstly the cause of the problem but if you have any ideas, suggestions about how to improve my code, they we'll be welcomed.

 private void button2_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\C #\InsertDeleteUpdate-Login\InsertDeleteUpdate-Login\Database1.mdf;Integrated Security=True"))
        using (SqlCommand cmd = new SqlCommand("UPDATE info SET Password=@Password WHERE Id=@Id AND Password=@Password1",conn))
        {
            conn.Open();
            cmd.Parameters.AddWithValue("@Password", textBox4.Text);
            cmd.Parameters.AddWithValue("@Id", textBox3.Text);
            cmd.Parameters.AddWithValue("@Password1", textBox2.Text);
        }
    }
Lazu Razvan
  • 49
  • 2
  • 8

3 Answers3

3

You need to execute the query

cmd.Parameters.AddWithValue("@Password", textBox4.Text);
cmd.Parameters.AddWithValue("@Id", textBox3.Text);
cmd.Parameters.AddWithValue("@Password1", textBox2.Text);

cmd.ExecuteNonQuery(); // this is what was missing
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user1666620
  • 4,800
  • 18
  • 27
  • thx a lot, you know, in this app i had another query where i SELECT something and that statement works without executenonquery, that's why i believed it is not needed here too. – Lazu Razvan Aug 21 '15 at 12:55
  • A Select is executed implicitly while UPDATE and DELETE require an explicit execution statement. – Mike Devenney Aug 21 '15 at 13:04
3

You are not executing the query. You need to use:

cmd.ExcecuteNonQuery();

after adding the parameters.

Vahlkron
  • 464
  • 3
  • 15
2
private void button2_Click(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\C #\InsertDeleteUpdate-Login\InsertDeleteUpdate-Login\Database1.mdf;Integrated Security=True"))
    using (SqlCommand cmd = new SqlCommand("UPDATE info SET Password=@Password WHERE Id=@Id AND Password=@Password1",conn))
    {
        conn.Open();
        cmd.Parameters.AddWithValue("@Password", textBox4.Text);
        cmd.Parameters.AddWithValue("@Id", textBox3.Text);
        cmd.Parameters.AddWithValue("@Password1", textBox2.Text);
        cmd.ExecuteNonQuery();

    }
}
Akshay
  • 71
  • 1
  • 7