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. Need Help with Database Update using DataAdaptor

Need Help with Database Update using DataAdaptor

Scheduled Pinned Locked Moved C#
helpcssdatabaseannouncementlearning
9 Posts 3 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.
  • J Offline
    J Offline
    Jethro63
    wrote on last edited by
    #1

    Greetings: I am working my way through the MCSD "Developing Windows Based Apps" book. I'm in the section on databases and ADO. This chapter is woefully lacking in working examples so I am trying to contrive my own in order to see some of this stuff in action. To do this, I am connecting to the Northwind database supplied with Access in order to have a database to play with. OK, so I have successfully connected to Northwind, I have used data readers and data adaptors to load tables from Northwind into data sets. I have successfully displayed the data using data grids. I have also successfully executed non querys, scalars and readers. Eveything is going tickety-boo. However, the last thing on my agenda before moving on is to ADD a row to one of the tables and then actually write it back to the Northwind database file. To do this, I have fashioned a simple input form and when I get all the required input fields, I return from the form and execute the following: private void btnAddSupplier_Click(object sender, System.EventArgs e) { AddSupplier frmAdd = new AddSupplier(); frmAdd.ShowDialog( this ); if (frmAdd.AddSelected == false) return; oleDbDataAdapter_Suppliers.Fill( dataSet_Suppliers ); DataRow dr = dataSet_Suppliers.Tables[0].NewRow(); dr["SupplierID"] = System.DBNull.Value; dr["CompanyName"] = frmAdd.CompanyName; dr["ContactName"] = frmAdd.ContactName; dr["ContactTitle"] = frmAdd.ContactTitle; dr["Address"] = frmAdd.Address; dr["Region"] = frmAdd.City; dr["City"] = frmAdd.RegionProv; dr["PostalCode"] = frmAdd.Postal; dr["Country"] = frmAdd.Country; dr["Phone"] = ""; dr["FAX"] = ""; dr["HomePage"] = ""; oleDbDataAdapter_Suppliers.Update( dataSet_Suppliers ); dataSet_Suppliers.Tables[0].Rows.Add( dr ); dr.AcceptChanges(); } "dataSet_Suppliers" and "oleDbDataAdaptor_Suppliers" are both members of the main form that I am using for my experimentation scaffold. I am certain that the adaptor and data set are successfully loaded with the Supplier table. I am able to Fill the supplier data set through the data adaptor and bind the data set to a data grid and display it. The problem is that the above code does not write the new row to the database file. It appears unchanged. The code executes OK, but the changes are not applied to the file. I am confused about the order in which I should call "AcceptChanges" for the DataRow and "Update" for the data adaptor. The MCSD book says that you must call "Update" on the data adaptor FIRST. I

    E B 2 Replies Last reply
    0
    • J Jethro63

      Greetings: I am working my way through the MCSD "Developing Windows Based Apps" book. I'm in the section on databases and ADO. This chapter is woefully lacking in working examples so I am trying to contrive my own in order to see some of this stuff in action. To do this, I am connecting to the Northwind database supplied with Access in order to have a database to play with. OK, so I have successfully connected to Northwind, I have used data readers and data adaptors to load tables from Northwind into data sets. I have successfully displayed the data using data grids. I have also successfully executed non querys, scalars and readers. Eveything is going tickety-boo. However, the last thing on my agenda before moving on is to ADD a row to one of the tables and then actually write it back to the Northwind database file. To do this, I have fashioned a simple input form and when I get all the required input fields, I return from the form and execute the following: private void btnAddSupplier_Click(object sender, System.EventArgs e) { AddSupplier frmAdd = new AddSupplier(); frmAdd.ShowDialog( this ); if (frmAdd.AddSelected == false) return; oleDbDataAdapter_Suppliers.Fill( dataSet_Suppliers ); DataRow dr = dataSet_Suppliers.Tables[0].NewRow(); dr["SupplierID"] = System.DBNull.Value; dr["CompanyName"] = frmAdd.CompanyName; dr["ContactName"] = frmAdd.ContactName; dr["ContactTitle"] = frmAdd.ContactTitle; dr["Address"] = frmAdd.Address; dr["Region"] = frmAdd.City; dr["City"] = frmAdd.RegionProv; dr["PostalCode"] = frmAdd.Postal; dr["Country"] = frmAdd.Country; dr["Phone"] = ""; dr["FAX"] = ""; dr["HomePage"] = ""; oleDbDataAdapter_Suppliers.Update( dataSet_Suppliers ); dataSet_Suppliers.Tables[0].Rows.Add( dr ); dr.AcceptChanges(); } "dataSet_Suppliers" and "oleDbDataAdaptor_Suppliers" are both members of the main form that I am using for my experimentation scaffold. I am certain that the adaptor and data set are successfully loaded with the Supplier table. I am able to Fill the supplier data set through the data adaptor and bind the data set to a data grid and display it. The problem is that the above code does not write the new row to the database file. It appears unchanged. The code executes OK, but the changes are not applied to the file. I am confused about the order in which I should call "AcceptChanges" for the DataRow and "Update" for the data adaptor. The MCSD book says that you must call "Update" on the data adaptor FIRST. I

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      Add before update?

      A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

      J 1 Reply Last reply
      0
      • E Ennis Ray Lynch Jr

        Add before update?

        A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

        J Offline
        J Offline
        Jethro63
        wrote on last edited by
        #3

        Is that a question or a suggestion? M.

        E 1 Reply Last reply
        0
        • J Jethro63

          Greetings: I am working my way through the MCSD "Developing Windows Based Apps" book. I'm in the section on databases and ADO. This chapter is woefully lacking in working examples so I am trying to contrive my own in order to see some of this stuff in action. To do this, I am connecting to the Northwind database supplied with Access in order to have a database to play with. OK, so I have successfully connected to Northwind, I have used data readers and data adaptors to load tables from Northwind into data sets. I have successfully displayed the data using data grids. I have also successfully executed non querys, scalars and readers. Eveything is going tickety-boo. However, the last thing on my agenda before moving on is to ADD a row to one of the tables and then actually write it back to the Northwind database file. To do this, I have fashioned a simple input form and when I get all the required input fields, I return from the form and execute the following: private void btnAddSupplier_Click(object sender, System.EventArgs e) { AddSupplier frmAdd = new AddSupplier(); frmAdd.ShowDialog( this ); if (frmAdd.AddSelected == false) return; oleDbDataAdapter_Suppliers.Fill( dataSet_Suppliers ); DataRow dr = dataSet_Suppliers.Tables[0].NewRow(); dr["SupplierID"] = System.DBNull.Value; dr["CompanyName"] = frmAdd.CompanyName; dr["ContactName"] = frmAdd.ContactName; dr["ContactTitle"] = frmAdd.ContactTitle; dr["Address"] = frmAdd.Address; dr["Region"] = frmAdd.City; dr["City"] = frmAdd.RegionProv; dr["PostalCode"] = frmAdd.Postal; dr["Country"] = frmAdd.Country; dr["Phone"] = ""; dr["FAX"] = ""; dr["HomePage"] = ""; oleDbDataAdapter_Suppliers.Update( dataSet_Suppliers ); dataSet_Suppliers.Tables[0].Rows.Add( dr ); dr.AcceptChanges(); } "dataSet_Suppliers" and "oleDbDataAdaptor_Suppliers" are both members of the main form that I am using for my experimentation scaffold. I am certain that the adaptor and data set are successfully loaded with the Supplier table. I am able to Fill the supplier data set through the data adaptor and bind the data set to a data grid and display it. The problem is that the above code does not write the new row to the database file. It appears unchanged. The code executes OK, but the changes are not applied to the file. I am confused about the order in which I should call "AcceptChanges" for the DataRow and "Update" for the data adaptor. The MCSD book says that you must call "Update" on the data adaptor FIRST. I

          B Offline
          B Offline
          bahaa_sa5
          wrote on last edited by
          #4

          try the following code... DataRowView NewRow = DataSet.Tables[TableName].DefaultView.AddNew() NewRow[ColumnName] = ""; NewRow.EndEdit(); DataAdapter.Update(DataSet, TableName); bahaa

          J 1 Reply Last reply
          0
          • J Jethro63

            Is that a question or a suggestion? M.

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #5

            Suggestion but untested one. The ? absolves me of responsibility for it not working :p

            A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

            J 1 Reply Last reply
            0
            • B bahaa_sa5

              try the following code... DataRowView NewRow = DataSet.Tables[TableName].DefaultView.AddNew() NewRow[ColumnName] = ""; NewRow.EndEdit(); DataAdapter.Update(DataSet, TableName); bahaa

              J Offline
              J Offline
              Jethro63
              wrote on last edited by
              #6

              OK, so my code now looks like this: DataRowView drv = dataSet_Suppliers.Tables[0].DefaultView.AddNew(); drv["SupplierID"] = System.DBNull.Value; ... more field assignments here ... drv["HomePage"] = ""; drv.EndEdit(); oleDbDataAdapter_Suppliers.Update( dataSet_Suppliers, "Suppliers" ); When I call the "Update" command, an OleDbException is thrown. Any other thoughts? Mark

              B 1 Reply Last reply
              0
              • E Ennis Ray Lynch Jr

                Suggestion but untested one. The ? absolves me of responsibility for it not working :p

                A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

                J Offline
                J Offline
                Jethro63
                wrote on last edited by
                #7

                Hi Ennis: I tried your suggestion. Now the lower part of my code block looks like this: dr["HomePage"] = ""; dataSet_Suppliers.Tables[0].Rows.Add( dr ); oleDbDataAdapter_Suppliers.Update( dataSet_Suppliers ); dr.AcceptChanges(); The call to "Update" now throws an OleDbException. Thanks for your help anyway. If you have any other thoughts, I'm all ears... Mark

                E 1 Reply Last reply
                0
                • J Jethro63

                  Hi Ennis: I tried your suggestion. Now the lower part of my code block looks like this: dr["HomePage"] = ""; dataSet_Suppliers.Tables[0].Rows.Add( dr ); oleDbDataAdapter_Suppliers.Update( dataSet_Suppliers ); dr.AcceptChanges(); The call to "Update" now throws an OleDbException. Thanks for your help anyway. If you have any other thoughts, I'm all ears... Mark

                  E Offline
                  E Offline
                  Ennis Ray Lynch Jr
                  wrote on last edited by
                  #8

                  move accept up one line. BTW, what are the specifics of the exception.

                  A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

                  1 Reply Last reply
                  0
                  • J Jethro63

                    OK, so my code now looks like this: DataRowView drv = dataSet_Suppliers.Tables[0].DefaultView.AddNew(); drv["SupplierID"] = System.DBNull.Value; ... more field assignments here ... drv["HomePage"] = ""; drv.EndEdit(); oleDbDataAdapter_Suppliers.Update( dataSet_Suppliers, "Suppliers" ); When I call the "Update" command, an OleDbException is thrown. Any other thoughts? Mark

                    B Offline
                    B Offline
                    bahaa_sa5
                    wrote on last edited by
                    #9

                    What the OleDbException message say...?

                    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