tags:

views:

137

answers:

2

hello. I write this code in my project but when I change a value in my application,appeared a exception and don't update my database,this code work for new but dont work for delete and edit 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();
    }

Exception :

System.Data.DBConcurrencyException was unhandled Message="Concurrency violation: the UpdateCommand affected 0 of the expected 1 records." Source="System.Data" RowCount=1

Sql query foe update :

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, theoretical_Unit = @theoretical_Unit, Lecture_Capacity = @Lecture_Capacity, Term = @Term WHERE (Lecture_Code = @Original_Lecture_Code) AND (Section_ID = @Original_Section_ID) AND (Lecture_Type_ID = @Original_Lecture_Type_ID) AND (Lecture_Name = @Original_Lecture_Name) AND (Practicable_Unit = @Original_Practicable_Unit) AND (theoretical_Unit = @Original_theoretical_Unit) AND (Lecture_Capacity = @Original_Lecture_Capacity) AND (Term = @Original_Term)

+2  A: 

I had the same problem once and as I recall it worked after changing relationship property in the database tables that were affected. I changed the Insert and Update Specification as Cascade and the problem never appeared again. Being probably not the perfect solution it worked though.

Izabela
A: 

Your update query contains a WHERE-clause that makes sure that a row in the database only gets updated or deleted, if it was not updated from a different source in the meantime.

I suspect you used the autogenerated sql queries provided by the strongly typed datase/tableadapters?
When specifying the SELECT query for the tableadapter, go to "Advanced Options". In the appearing window there is an option "Use optimistic concurrency". Uncheck this to allow the overwriting of values that have changed in the database since the data was loaded into the application. You will notice that the WHERE-clause of the update statement will become much smaller.

lowglider