Walid Madrid
Walid Madrid

Reputation: 35

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();

Upvotes: 0

Views: 72

Answers (2)

H. Herzl
H. Herzl

Reputation: 3248

Your sql command't test would be:

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

Also, you will need to validate if conversion from string to int64 if fails or not.

Upvotes: 0

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.

Upvotes: 2

Related Questions