Walid Madrid Walid Madrid - 2 months ago 5
C# Question

C# SQL Server update doesn't work

I have this code, and when I execute it, it doesn't work

SqlCommand cmd = new SqlCommand("UPDATE etudient SET [nom etudient] = 'username', pass = '@password' where IDetudient='@ID ' ", con);

con.Open();

cmd.Parameters.AddWithValue("@username", text_name.Text);
cmd.Parameters.AddWithValue("@password",Convert.ToDecimal( textBox1.Text));
cmd.Parameters.AddWithValue("@ID", Convert.ToInt64( text_id.Text));

cmd.ExecuteNonQuery();

con.Close();

Answer

Try this way:

SqlCommand cmd = new SqlCommand("UPDATE etudient SET [nom etudient] = @username, pass = @password where IDetudient=@ID", con);

I had the same issue. The thing is, in the query you just pass the name of the parameter.

Comments