I am changing the value in my application but not updating in my database
I am writing this code in my project, but when I change the value in my application, an exception pops up and does not update my database, this code works for new ones, but does not work for deleting and editing data.
CODE:
private void btnLecOk_Click(object sender, EventArgs e)
{
DataRow dr = (lecturesBindingSource.Current as DataRowView).Row;
dr["Section_ID"] = Convert.ToInt32(cmbLec_Section_Name.SelectedValue);
dr["Lecture_Type_ID"] = Convert.ToInt32(cmbLec_Lecture_Type.SelectedValue);
dr["Practicable_Unit"] = numLec_Practicable_Unit.Value;
dr["theoretical_Unit"] = numLec_Theoretical_Unit.Value;
dr["Term"] = numLec_Term.Value;
lecturesBindingSource.EndEdit();
lecturesTableAdapter.Update(dsSchedule.Lectures);
EnableLectureControls();
}
An exception:
System.Data.DBConcurrencyException error was unhandled Message = "Concurrency violation: UpdateCommand affected 0 of the expected 1 records." Source = "System.Data" RowCount = 1
Sql Query Refresh:
UPDATE Lectures SET Lecture_Code = @Lecture_Code, Section_ID = @Section_ID, Lecture_Type_ID = @Lecture_Type_ID, Lecture_Name = @Lecture_Name, Practicable_Unit = @Practicable_Unit, Tercture_Unit = @Practicable_Unit, Thecture_Unit (Section_ID = @Original_Section_ID) AND (Lecture_Type_ID = @Original_Lecture_Type_ID) AND (Lecture_Name = @Original_Lecture_Name) AND (Practical_Unit = @Original_Practicable_Unit) AND (Theor_Unacity_Practicable_Unit) AND (Theor_Unacity_Practicable_Unit)
a source to share
I had the same problem and as I remember it worked after changing the relationship property on the database tables that were affected. I changed the Insert and Update spec to be Cascade and the problem didn't appear anymore. This is probably not the perfect solution that worked.
a source to share
Your update request contains a WHERE clause that ensures that a row in the database will be updated or deleted if it hasn't been updated from another source in the meantime.
I suspect you were using the autogenerated sql queries provided by strongly typed datase / tableadapters?
When specifying a SELECT query for the tableadapter, go to Advanced Options. In the window that appears, there is an option "Use optimistic concurrency". Clear this check box to allow overwriting of values that have changed in the database because the data was loaded into the application. You will notice that the WHERE clause of the update statement is much smaller.
a source to share