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. C#
  4. DataSet not Updating Database

DataSet not Updating Database

Scheduled Pinned Locked Moved C#
databasequestionannouncement
5 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.
  • O Offline
    O Offline
    Orville
    wrote on last edited by
    #1

    I have a dataset that is populated from an access database and displayed in a datagrid just fine. Then I added a button to the form for updating the database. Here is the code for the button. private void bntStUp_Click(object sender, System.EventArgs e) { int iRetVal = 0; try { oDS.AcceptChanges(); iRetVal = myAdapter.Update(oDS, "tblSteps"); MessageBox.Show("The update was successful. Rows Updated: " + iRetVal.ToString()); } catch (Exception ex) { // Display information about update errors. MessageBox.Show("Update Failed: " + ex.Message); } } It works fine the dataset and datagrid are updated, but the database is not updated. Is there something that I am missing? Huh?

    H 1 Reply Last reply
    0
    • O Orville

      I have a dataset that is populated from an access database and displayed in a datagrid just fine. Then I added a button to the form for updating the database. Here is the code for the button. private void bntStUp_Click(object sender, System.EventArgs e) { int iRetVal = 0; try { oDS.AcceptChanges(); iRetVal = myAdapter.Update(oDS, "tblSteps"); MessageBox.Show("The update was successful. Rows Updated: " + iRetVal.ToString()); } catch (Exception ex) { // Display information about update errors. MessageBox.Show("Update Failed: " + ex.Message); } } It works fine the dataset and datagrid are updated, but the database is not updated. Is there something that I am missing? Huh?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Because you're calling AcceptChanges. This removes all the change information and the DataAdapter will find none, thus it will not update the database. Just call DataAdapter.Update, which will accept changes when the database has been updated. Read the documentation on the AcceptChanges method (defined on both the DataSet and DataTable classes) in the .NET Framework SDK for more information.

      Microsoft MVP, Visual C# My Articles

      O 1 Reply Last reply
      0
      • H Heath Stewart

        Because you're calling AcceptChanges. This removes all the change information and the DataAdapter will find none, thus it will not update the database. Just call DataAdapter.Update, which will accept changes when the database has been updated. Read the documentation on the AcceptChanges method (defined on both the DataSet and DataTable classes) in the .NET Framework SDK for more information.

        Microsoft MVP, Visual C# My Articles

        O Offline
        O Offline
        Orville
        wrote on last edited by
        #3

        I take the AcceptChanges line of code and I get this error. "Update requires a valid UpdateCommand when passed DataRow collection with modified rows." Now what? Huh?

        H 1 Reply Last reply
        0
        • O Orville

          I take the AcceptChanges line of code and I get this error. "Update requires a valid UpdateCommand when passed DataRow collection with modified rows." Now what? Huh?

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          It should be obvious: you need to assign a command (like a SqlCommand, OleDbCommand, or whatever is appropriate) to the DataAdapter.UpdateCommand property, using whichever DataAdapter is appropriate. How do you expect the DataAdapter to update the database if it doesn't know how to update the database? If you use a simple SELECT statement, then you can also use a command builder like the SqlCommandBuilder to build the INSERT, UPDATE, and DELETE statements for a DataAdapter. See the .NET Framework SDK documentation for more details.

          Microsoft MVP, Visual C# My Articles

          O 1 Reply Last reply
          0
          • H Heath Stewart

            It should be obvious: you need to assign a command (like a SqlCommand, OleDbCommand, or whatever is appropriate) to the DataAdapter.UpdateCommand property, using whichever DataAdapter is appropriate. How do you expect the DataAdapter to update the database if it doesn't know how to update the database? If you use a simple SELECT statement, then you can also use a command builder like the SqlCommandBuilder to build the INSERT, UPDATE, and DELETE statements for a DataAdapter. See the .NET Framework SDK documentation for more details.

            Microsoft MVP, Visual C# My Articles

            O Offline
            O Offline
            Orville
            wrote on last edited by
            #5

            Thanks I will look at that. Huh?

            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