Hello. I am very new to database programming (still learning the basics of it), so when I have read up on this issue elsewhere, I have not been able to use the information given to resolve this bug. In my program's update and delete functions, I am getting this class error message:
Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
It will happen if and only if the record that it's supposed to update/delete has been added to the database during that program's execution. I have read up on this problem and have gathered some information about it, but I really need to ask somebody to please show me a couple of lines through which to correct my code to resolve this error. After I see that, it will help me learn how to correct/avoid this error in the future, but please show me the actual code to correct this. Here is the code for the add, update, and delete functions:
Code:private void btnAdd_Click(object sender, EventArgs e) { con.Open(); DataRow row = dataset.Tables["Contacts"].NewRow(); row[1] = tbFirstName.Text; row[2] = tbMiddleName.Text; row[3] = tbLastName.Text; row[4] = tbSuffix.Text; row[5] = tbHomePhone.Text; row[6] = tbCellPhone.Text; row[7] = tbOtherPhone.Text; row[8] = tbStreetAddress.Text; row[9] = tbCityAndState.Text; row[10] = tbCountry.Text; row[11] = tbEmail.Text; currentRow = row; dataset.Tables["Contacts"].Rows.Add(row); dAdapter.Update(dataset, "Contacts"); recordShown = true; con.Close(); }Code:private void btnUpdate_Click(object sender, EventArgs e) { if (recordShown) { con.Open(); currentRow[1] = tbFirstName.Text; currentRow[2] = tbMiddleName.Text; currentRow[3] = tbLastName.Text; currentRow[4] = tbSuffix.Text; currentRow[5] = tbHomePhone.Text; currentRow[6] = tbCellPhone.Text; currentRow[7] = tbOtherPhone.Text; currentRow[8] = tbStreetAddress.Text; currentRow[9] = tbCityAndState.Text; currentRow[10] = tbCountry.Text; currentRow[11] = tbEmail.Text; SqlCommandBuilder updateBuilder; updateBuilder = new SqlCommandBuilder(dAdapter); dAdapter.Update(dataset, "Contacts"); con.Close(); } else { MessageBox.Show("Please locate/add a record first."); } }Thanks!Code:private void btnDelete_Click(object sender, EventArgs e) { con.Open(); currentRow.Delete(); dAdapter.Update(dataset, "Contacts"); clearTextBoxes(); recordShown = false; con.Close(); }



LinkBack URL
About LinkBacks


