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. Grid Controls Committing Changes [modified]

Grid Controls Committing Changes [modified]

Scheduled Pinned Locked Moved C#
cssdatabasesql-serversysadminquestion
7 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.
  • T Offline
    T Offline
    tjschilling
    wrote on last edited by
    #1

    I have some grid controls on a form that are using data adaptors to fill the grid directly from the SQL Server data base. I have added an even handler on exit of the form to commit changes but the changes are not getting written back to the data base. Here is my code - this.speciesTableAdapter.Fill(this.markingDBDataSet3.Species); private void MasterData_FormClosing(object sender, FormClosingEventArgs e) { SpeciesGrid.CommitEdit(DataGridViewDataErrorContexts.Commit); SpeciesGrid.Update(); } I must be missing something or I am totally on the wrong track. Any ideas? -- modified at 18:42 Monday 22nd May, 2006

    S 1 Reply Last reply
    0
    • T tjschilling

      I have some grid controls on a form that are using data adaptors to fill the grid directly from the SQL Server data base. I have added an even handler on exit of the form to commit changes but the changes are not getting written back to the data base. Here is my code - this.speciesTableAdapter.Fill(this.markingDBDataSet3.Species); private void MasterData_FormClosing(object sender, FormClosingEventArgs e) { SpeciesGrid.CommitEdit(DataGridViewDataErrorContexts.Commit); SpeciesGrid.Update(); } I must be missing something or I am totally on the wrong track. Any ideas? -- modified at 18:42 Monday 22nd May, 2006

      S Offline
      S Offline
      Sean89
      wrote on last edited by
      #2

      Ok. To update the database based on changes you have made in the datagrid you must be calling the DataAdapter.Update() method. This takes two arguments: Your datatable/dataset (this will be the datasource of your grid), and (if you hand it a dataset) the name of the table. Ex. dAdapter.Update(somedataset, "Table"); or dAdapter.Update(sometable); But you do need to specify an insert command in order for this to work. If a standard insert query is fine, you can use the command builder class. So, if you are using OleDb it would be like this: OleDbCommandBuilder cmdBuild = new OleDbCommandBuilder(dAdapter); dAdapter.Update(somedataset, "Table"); Hope that helps! ;P


      T 1 Reply Last reply
      0
      • S Sean89

        Ok. To update the database based on changes you have made in the datagrid you must be calling the DataAdapter.Update() method. This takes two arguments: Your datatable/dataset (this will be the datasource of your grid), and (if you hand it a dataset) the name of the table. Ex. dAdapter.Update(somedataset, "Table"); or dAdapter.Update(sometable); But you do need to specify an insert command in order for this to work. If a standard insert query is fine, you can use the command builder class. So, if you are using OleDb it would be like this: OleDbCommandBuilder cmdBuild = new OleDbCommandBuilder(dAdapter); dAdapter.Update(somedataset, "Table"); Hope that helps! ;P


        T Offline
        T Offline
        tjschilling
        wrote on last edited by
        #3

        I am using a SqlDatatAdaptor and when I try this solution, it builds without any errors but when the update is executed, I get the following exception error - Update requires a valid UpdateCommand when passed DataRow collection with modified rows. This is my code - SqlDataAdapter dAdapter = new System.Data.SqlClient.SqlDataAdapter(); dAdapter.Update(markingDBDataSet3, "Species");

        S 1 Reply Last reply
        0
        • T tjschilling

          I am using a SqlDatatAdaptor and when I try this solution, it builds without any errors but when the update is executed, I get the following exception error - Update requires a valid UpdateCommand when passed DataRow collection with modified rows. This is my code - SqlDataAdapter dAdapter = new System.Data.SqlClient.SqlDataAdapter(); dAdapter.Update(markingDBDataSet3, "Species");

          S Offline
          S Offline
          Sean89
          wrote on last edited by
          #4

          You need to include the SqlCommandBuilder to make your update command for you:

          SqlDataAdapter dAdapter = new SqlDataAdapter();

          SqlCommandBuilder cmdBuild = new SqlCommandBuilder(dAdapter); // add this

          dAdapter.Update(markingDBDataSet3, "Species");

          Without this, the update will not work.


          T 1 Reply Last reply
          0
          • S Sean89

            You need to include the SqlCommandBuilder to make your update command for you:

            SqlDataAdapter dAdapter = new SqlDataAdapter();

            SqlCommandBuilder cmdBuild = new SqlCommandBuilder(dAdapter); // add this

            dAdapter.Update(markingDBDataSet3, "Species");

            Without this, the update will not work.


            T Offline
            T Offline
            tjschilling
            wrote on last edited by
            #5

            I added the code as sugested - SqlDataAdapter dAdapter = new SqlDataAdapter(); SqlCommandBuilder cmdBuild = new SqlCommandBuilder(dAdapter); dAdapter.Update(markingDBDataSet3,"Species"); I am now getting a different error. Error - The DataAdapter.SelectCommand property needs to be initialized.

            T 1 Reply Last reply
            0
            • T tjschilling

              I added the code as sugested - SqlDataAdapter dAdapter = new SqlDataAdapter(); SqlCommandBuilder cmdBuild = new SqlCommandBuilder(dAdapter); dAdapter.Update(markingDBDataSet3,"Species"); I am now getting a different error. Error - The DataAdapter.SelectCommand property needs to be initialized.

              T Offline
              T Offline
              tjschilling
              wrote on last edited by
              #6

              Sean, you got me pointed in the right direction. I worked with one of the more experienced Windows developers here and we managed to cobble together the following which seems to be working. Evidently it needed a reference to the table adaptor and needed a select command added to the data adaptor. Here is the code - SqlDataAdapter dAdapter = new SqlDataAdapter(); SqlCommandBuilder cmdBuild = new SqlCommandBuilder(dAdapter); dAdapter.SelectCommand = new SqlCommand("select * from Species"); dAdapter.SelectCommand.Connection = speciesTableAdapter.Connection; dAdapter.Update(markingDBDataSet3,"Species"); Thank you very much for your help!:)

              S 1 Reply Last reply
              0
              • T tjschilling

                Sean, you got me pointed in the right direction. I worked with one of the more experienced Windows developers here and we managed to cobble together the following which seems to be working. Evidently it needed a reference to the table adaptor and needed a select command added to the data adaptor. Here is the code - SqlDataAdapter dAdapter = new SqlDataAdapter(); SqlCommandBuilder cmdBuild = new SqlCommandBuilder(dAdapter); dAdapter.SelectCommand = new SqlCommand("select * from Species"); dAdapter.SelectCommand.Connection = speciesTableAdapter.Connection; dAdapter.Update(markingDBDataSet3,"Species"); Thank you very much for your help!:)

                S Offline
                S Offline
                Sean89
                wrote on last edited by
                #7

                Ohh sorry I would have told you to put a select command in when initializing the data adapter but I thought you had just left it out so you wouldnt take up to much room in your post ;) Good to see you got it workin' ;)


                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