I want to update a Table in Database for every row in the DataGridView by using a Update button. Here's The following Update Button Code :
private void btUpdate_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(con);
cn.Open();
SqlCommand cmdUpdate = new SqlCommand("UPDATE OINV SET U_IDU_HFP = @HFP, U_IDU_FAKTURPAJAK = @FP, U_IDU_FPDATE = @FPDATE WHERE DocEntry = @DocEntry;", cn);
foreach (DataGridViewRow item in dgvInputPKP.Rows)
{
cmdUpdate.Parameters.AddWithValue("@HFP", item.Cells[10].Value);
cmdUpdate.Parameters.AddWithValue("@FP", item.Cells[11].Value);
cmdUpdate.Parameters.AddWithValue("@FPDATE", item.Cells[9].Value);
cmdUpdate.Parameters.AddWithValue("@DocEntry", item.Cells[1].Value);
cmdUpdate.ExecuteNonQuery();
}
MessageBox.Show("Record Updated Successfully");
cn.Close();
}
When I press the Update Button only the 1st row from DataGridViews was Updated Succesfully in the Database, but not with the other rows. It also return some error with following detail:
The variable name '@HFP' has already been declared. Variable names must be unique within a query batch or stored procedure.
I was try to searching and googling but never find the right one.
Please help me to fix this issue. Thanks.