Reputation: 197
Hi guys i came stuck again. I am wanting to edit a selected row from my datagridview and replace the data from dgv with the new information in the text fields. I have managed to get it to change all the data in the dataset. So what i am asking is for guidance on how to code it so it only edits the selected row.
private void btnEdit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constring);
SqlDataAdapter da = new SqlDataAdapter();
da.UpdateCommand = new SqlCommand(String cmdUpdate = @"update Customer set firstName = @firstName, surname = @surname, email = @email, phonenumber = @phone, mobileNumber = @mobile";
, con);
da.UpdateCommand.Parameters.Add("@firstName", SqlDbType.VarChar).Value = textFirstName.Text;
da.UpdateCommand.Parameters.Add("@surname", SqlDbType.VarChar).Value = textSurname.Text;
da.UpdateCommand.Parameters.Add("@email", SqlDbType.VarChar).Value = textEmail.Text;
da.UpdateCommand.Parameters.Add("@phone", SqlDbType.VarChar).Value = textPhone.Text;
da.UpdateCommand.Parameters.Add("@mobile", SqlDbType.VarChar).Value = textMobile.Text;
da.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int).Value = customerDataSet.Customer[0].ID;
con.Open();
da.UpdateCommand.ExecuteNonQuery();
MessageBox.Show("Customer Edited");
con.Close();
}
Upvotes: 0
Views: 2056
Reputation: 24144
You UPDATE query updates whole table you should use WHERE statement in this query to update the row with current ID
update Customer
set firstName = @firstName,
surname = @surname,
email = @email,
phonenumber = @phone,
mobileNumber = @mobile
WHERE ID=@ID
Upvotes: 1