Databindings fail to update db
-
Hi, I am using databindings to populate a combobox and several textboxes from a single datatable. When I select a record from the combobox, details of that record are displayed in the textboxes. This part of databinding works fine but when I make changes to the textboxes I want to update those changes back to the database. This update part is not working for me. No runtime errors but the db does not change. Can anyone help me? I am using a typed dataset "dsMySQL". My code is as follows: // code for form_load myDataAdapter.Fill(dsMySQL.DaySchoolsTbl); cbxDaySchools.DataSource = dsMySQL.DaySchoolsTbl; cbxDaySchools.DisplayMember = "SchoolName"; cbxDaySchools.ValueMember = "SchoolID"; tbxSchoolID.DataBindings.Add("Text",dsMySQL.DaySchoolsTbl,"SchoolID"); tbxSchoolName.DataBindings.Add("Text",dsMySQL.DaySchoolsTbl,"SchoolName"); cbxSchoolType.DataBindings.Add("SelectedIndex",dsMySQL.DaySchoolsTbl,"SchoolType"); // code for save_button myDataAdapter.Update(dsMySQL.DaySchoolsTbl); dsMySQL.DaySchoolsTbl.AcceptChanges();
-
Hi, I am using databindings to populate a combobox and several textboxes from a single datatable. When I select a record from the combobox, details of that record are displayed in the textboxes. This part of databinding works fine but when I make changes to the textboxes I want to update those changes back to the database. This update part is not working for me. No runtime errors but the db does not change. Can anyone help me? I am using a typed dataset "dsMySQL". My code is as follows: // code for form_load myDataAdapter.Fill(dsMySQL.DaySchoolsTbl); cbxDaySchools.DataSource = dsMySQL.DaySchoolsTbl; cbxDaySchools.DisplayMember = "SchoolName"; cbxDaySchools.ValueMember = "SchoolID"; tbxSchoolID.DataBindings.Add("Text",dsMySQL.DaySchoolsTbl,"SchoolID"); tbxSchoolName.DataBindings.Add("Text",dsMySQL.DaySchoolsTbl,"SchoolName"); cbxSchoolType.DataBindings.Add("SelectedIndex",dsMySQL.DaySchoolsTbl,"SchoolType"); // code for save_button myDataAdapter.Update(dsMySQL.DaySchoolsTbl); dsMySQL.DaySchoolsTbl.AcceptChanges();
Put a breakpoint on the row:
myDataAdapter.Update(dsMySQL.DaySchoolsTbl);
and check that the modifications are in the dataset and that the
RowStates
for the modified rows are notUnchaged
. One reason could be that you accept all changes somewhere in the code before updating the data adapter. Also you don't have to calldsMySQL.DaySchoolsTbl.AcceptChanges();
after update since if the update is succesful, it automatically accpets the changes. Actually, if something goes wrong during the update and you accept changes, the updates not yet taken to the db are lost.The need to optimize rises from a bad design.My articles[^]
-
Put a breakpoint on the row:
myDataAdapter.Update(dsMySQL.DaySchoolsTbl);
and check that the modifications are in the dataset and that the
RowStates
for the modified rows are notUnchaged
. One reason could be that you accept all changes somewhere in the code before updating the data adapter. Also you don't have to calldsMySQL.DaySchoolsTbl.AcceptChanges();
after update since if the update is succesful, it automatically accpets the changes. Actually, if something goes wrong during the update and you accept changes, the updates not yet taken to the db are lost.The need to optimize rises from a bad design.My articles[^]
Mika, thanks for your reply. I have done a debug as you suggested and found that the row item in question has changed (when I changed the tbxSchoolName) but the RowState remains "Unchanged". Do you know why? The test I am doing is simple enough, I display the form which loads the controls. I select a record from the Combobox which populates the Textboxes. I make a change to the Textbox. I click the 'Save' button to do the Update(). I have noticed that my changes in the Textbox 'tbxDaySchoolName' is not reflected in the Combobox 'cbxDaySchools'. I have done a search and AcceptChanges() is not used on this form any more. Obviously my 'select' command works but here is the code for the 'update' command: // The Update command handles updates to existing rows cmdUpdate = myConnection.CreateCommand(); cmdUpdate.CommandText = "UPDATE DaySchoolsTbl SET SchoolName=?SchoolName, SuburbID=?SuburbID, SchoolType=?SchoolType WHERE SchoolID=?SchoolID"; myParameter = cmdUpdate.Parameters.Add("?SchoolID", MySqlDbType.Int16, 40, "SchoolID"); myParameter = cmdUpdate.Parameters.Add("?SchoolName", MySqlDbType.String, 40, "SchoolName"); myParameter = cmdUpdate.Parameters.Add("?SchoolType", MySqlDbType.Byte, 1, "SchoolType"); myParameter = cmdUpdate.Parameters.Add("?SuburbID", MySqlDbType.Int16, 40, "SuburbID"); myParameter.SourceVersion = DataRowVersion.Original; myDataAdapter.UpdateCommand = cmdUpdate; Any idea what the problem is?? thanks
-
Mika, thanks for your reply. I have done a debug as you suggested and found that the row item in question has changed (when I changed the tbxSchoolName) but the RowState remains "Unchanged". Do you know why? The test I am doing is simple enough, I display the form which loads the controls. I select a record from the Combobox which populates the Textboxes. I make a change to the Textbox. I click the 'Save' button to do the Update(). I have noticed that my changes in the Textbox 'tbxDaySchoolName' is not reflected in the Combobox 'cbxDaySchools'. I have done a search and AcceptChanges() is not used on this form any more. Obviously my 'select' command works but here is the code for the 'update' command: // The Update command handles updates to existing rows cmdUpdate = myConnection.CreateCommand(); cmdUpdate.CommandText = "UPDATE DaySchoolsTbl SET SchoolName=?SchoolName, SuburbID=?SuburbID, SchoolType=?SchoolType WHERE SchoolID=?SchoolID"; myParameter = cmdUpdate.Parameters.Add("?SchoolID", MySqlDbType.Int16, 40, "SchoolID"); myParameter = cmdUpdate.Parameters.Add("?SchoolName", MySqlDbType.String, 40, "SchoolName"); myParameter = cmdUpdate.Parameters.Add("?SchoolType", MySqlDbType.Byte, 1, "SchoolType"); myParameter = cmdUpdate.Parameters.Add("?SuburbID", MySqlDbType.Int16, 40, "SuburbID"); myParameter.SourceVersion = DataRowVersion.Original; myDataAdapter.UpdateCommand = cmdUpdate; Any idea what the problem is?? thanks
dragon_n_me wrote:
row item in question has changed (when I changed the tbxSchoolName) but the RowState remains "Unchanged".
So the data in the column changes, but the row's RowState doesn't? Try to check the RowState immediately after you have modified the data (using tbxSchoolName). If the RowState is unchanged in that point, check the data bindings of the textbox. If it's changed, then you should investigate what clears the status after that.
The need to optimize rises from a bad design.My articles[^]
-
dragon_n_me wrote:
row item in question has changed (when I changed the tbxSchoolName) but the RowState remains "Unchanged".
So the data in the column changes, but the row's RowState doesn't? Try to check the RowState immediately after you have modified the data (using tbxSchoolName). If the RowState is unchanged in that point, check the data bindings of the textbox. If it's changed, then you should investigate what clears the status after that.
The need to optimize rises from a bad design.My articles[^]
-
I stopped at tbxSchoolName_Validating and neither data nor state has changed. I stopped at tbxSchoolName_Validated and only data has changed but RowState = "Unchanged". What can happen in between these 2 events?