Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Databindings fail to update db

Databindings fail to update db

Scheduled Pinned Locked Moved .NET (Core and Framework)
databasehelpquestionannouncement
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dragon52
    wrote on last edited by
    #1

    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();

    W 1 Reply Last reply
    0
    • D dragon52

      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();

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      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 not Unchaged. One reason could be that you accept all changes somewhere in the code before updating the data adapter. Also you don't have to call dsMySQL.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[^]

      D 1 Reply Last reply
      0
      • W Wendelius

        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 not Unchaged. One reason could be that you accept all changes somewhere in the code before updating the data adapter. Also you don't have to call dsMySQL.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[^]

        D Offline
        D Offline
        dragon52
        wrote on last edited by
        #3

        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

        W 1 Reply Last reply
        0
        • D dragon52

          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

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          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[^]

          D 1 Reply Last reply
          0
          • W Wendelius

            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[^]

            D Offline
            D Offline
            dragon52
            wrote on last edited by
            #5

            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?

            W 1 Reply Last reply
            0
            • D dragon52

              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?

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              Have you checked the binding collection's DefaultDataSourceUpdateMode also did yuo set the BindingContext somewhere in the code?

              The need to optimize rises from a bad design.My articles[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups