1

When I run my program and try to update it, it updates all the columns to the new data in textboxes I don't know how to tell it to do it for the certain username which is in a textbox (txtUsernameUser.Text) table name is UserData primary key is Username. I just want to say thanks in advance for your time

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\Graded unit Dev\BlackMarch\BlackMarch\bin\Debug\DataBaseBM.mdf;Integrated Security=True;Connect Timeout=30");

SqlCommand cmd = new SqlCommand("UPDATE UserData SET Username = @Username, Password = @Password, FirstName = @Firstname, Surname = @Surname, Age = @Age, Gender = @Gender, Mobile = @Mobile, Email = @Email", con);

con.Open();

cmd.Parameters.AddWithValue("@Username", txtUsernameUser.Text);
cmd.Parameters.AddWithValue("@Password", txtboxPass.Text);
cmd.Parameters.AddWithValue("@FirstName", txtboxFN.Text);
cmd.Parameters.AddWithValue("@Surname", txtboxSurname.Text);
cmd.Parameters.AddWithValue("@Age", txtboxAge.Text);
cmd.Parameters.AddWithValue("@Gender", txtboxGender.Text);
cmd.Parameters.AddWithValue("@Mobile", txtboxMobile.Text);
cmd.Parameters.AddWithValue("@Email", txtboxEmail.Text);

cmd.ExecuteNonQuery();

con.Close();  
4
  • 3
    you need to use WHERE clause inside your update Commented May 25, 2016 at 19:24
  • The only way that would work is if you had a WHERE statement. Which you don't. Commented May 25, 2016 at 19:27
  • Do you know the difference between MySql and Sql Server? Use the appropriate tags in your question Commented May 25, 2016 at 19:34
  • You should check out Can we stop using AddWithValue() already? and stop using .AddWithValue() - it can lead to unexpected and surprising results... Commented May 25, 2016 at 20:52

2 Answers 2

1

Add a WHERE clause to the UPDATE statement to identify the subset of records which are being identified. Additionally, you don't want to update the Primary Key (since it shouldn't change anyway). So you can use that parameter and just modify the query:

SqlCommand cmd = new SqlCommand("UPDATE UserData SET Password = @Password, FirstName = @Firstname, Surname = @Surname, Age = @Age, Gender = @Gender, Mobile = @Mobile, Email = @Email WHERE Username = @Username", con);

or, for readability in this post...

UPDATE
 UserData
SET
 Password = @Password,
 FirstName = @Firstname,
 Surname = @Surname,
 Age = @Age,
 Gender = @Gender,
 Mobile = @Mobile,
 Email = @Email
WHERE
 Username = @Username
Sign up to request clarification or add additional context in comments.

Comments

1

Add a proper where condition eg: if you can select the row with a YourID use it

"UPDATE UserData SET Username = @Username, 
  Password = @Password, 
 FirstName = @Firstname, 
 Surname = @Surname, 
 Age = @Age, 
 Gender = @Gender, 
Mobile = @Mobile, 
Email = @Email
WHERE YourID = @YourId"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.