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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Update() database from DataSet

Update() database from DataSet

Scheduled Pinned Locked Moved C#
databasehelpquestionannouncement
21 Posts 6 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.
  • P PIEBALDconsult

    You need to set the Commands.

    A Offline
    A Offline
    aecordoba
    wrote on last edited by
    #11

    The commands were automaticaly generated by Visual Studio 2005 in the moment of insert the DataSet.

    -- Adrián Córdoba

    P 1 Reply Last reply
    0
    • A aecordoba

      The commands were automaticaly generated by Visual Studio 2005 in the moment of insert the DataSet.

      -- Adrián Córdoba

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #12

      But they're in the original TableAdapter. When you do DataSetTableAdapters.UsersTableAdapter usersTableAdapter = new DataSetTableAdapters.UsersTableAdapter(); you instantiate a new/uninitialized TableAdapter.

      1 Reply Last reply
      0
      • P PIEBALDconsult

        No it isn't.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #13

        It's never a good idea to trust ANY data comming into your app, even if it IS from a trusted source. The data can be corrupted by the time it reaches your validation code.

        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        P 1 Reply Last reply
        0
        • A aecordoba

          Hi, there! I can't Update a table in database after a row is deleted in DataSet. I delete a row, as following: DataRow[] usersRows; usersRows = ContactsDataSet.Users.Select("UserName = '" + userName + "'"); foreach (DataRow userRow in usersRows) userRow.Delete(); and I tried to update the database: DataSetTableAdapters.UsersTableAdapter usersTableAdapter = new DataSetTableAdapters.UsersTableAdapter(); usersTableAdapter.Update(contactsDataSet.Users); But the data aren't updated. Can someone help me? Thank you in advance.

          -- Adrián Córdoba

          D Offline
          D Offline
          DarthDub
          wrote on last edited by
          #14

          First off do you have your update command made yet? I'll I know how to do with DataAdapters is the sql part of them, but if thats what your doing here is an example. It should be kinda the same type of thing. SqlConnection CNN = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=true"); SqlDataAdapter DA = new SqlDataAdapter(); DataSet DS = new DataSet(); SqlCommand cmdSelect = CNN.CreateCommand(); cmdSelect.CommandText = "Select CustomerID, ContactName, CompanyName From Customers"; // create an Update command SqlCommand cmdUpdate = CNN.CreateCommand(); cmdUpdate.CommandText = "Update Customers SET CompanyName=@CompanyName, ContactName=@ContactName WHERE CustomerID=@CustomerID"; cmdUpdate.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"); cmdUpdate.Parameters.Add("@ContactName", SqlDbType.NVarChar, 30, "ContactName"); cmdUpdate.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID"); cmdUpdate.Parameters["@CustomerID"].SourceVersion = DataRowVersion.Original; // create an Insert command SqlCommand cmdInsert = CNN.CreateCommand(); cmdInsert.CommandText = "Insert into customers (customerid, companyname, contactname) Values (@customerid, @companyname, @contactname)"; cmdInsert.Parameters.Add("@customerid", SqlDbType.NChar, 5, "customerid"); cmdInsert.Parameters.Add("@companyname", SqlDbType.NVarChar, 40, "companyname"); cmdInsert.Parameters.Add("@contactname", SqlDbType.NVarChar, 30, "contactname"); cmdInsert.Parameters["@customerid"].SourceVersion = DataRowVersion.Original; // create a Delete command SqlCommand cmdDelete = CNN.CreateCommand(); cmdDelete.CommandText = "Delete from customers Where customerid = @customerid"; cmdDelete.Parameters.Add("@customerid", SqlDbType.NChar, 5, "customerid"); cmdDelete.Parameters["@customerid"].SourceVersion = DataRowVersion.Original; DA.SelectCommand = cmdSelect; DA.UpdateCommand = cmdUpdate; DA.InsertCommand = cmdInsert; DA.DeleteCommand = cmdDelete; DA.Fill(DS, "Customers"); //then do whatever you want to with modifying it. //And then just do update like this

          1 Reply Last reply
          0
          • D Dave Kreskowiak

            It's never a good idea to trust ANY data comming into your app, even if it IS from a trusted source. The data can be corrupted by the time it reaches your validation code.

            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #15

            But why assume in this case that the data is "coming into" the application? He could very well be using string userName=System.Security.Principal.WindowsIdentity.GetCurrent().Name or otherwise validating the value.

            D 1 Reply Last reply
            0
            • P PIEBALDconsult

              You need to set the Commands.

              A Offline
              A Offline
              aecordoba
              wrote on last edited by
              #16

              PIEBALDconsult wrote:

              You need to set the Commands.

              You are right. Visual Studio 2005 didn't generate the DeleteCommand, seemingly because the Users table in database hasn't a Primary Key. I added a Primary Key to the table, and when I added this table to the DataSet the DeleteCommand was automaticaly generated. Thank you very much.

              -- Adrián Córdoba

              1 Reply Last reply
              0
              • P PIEBALDconsult

                But why assume in this case that the data is "coming into" the application? He could very well be using string userName=System.Security.Principal.WindowsIdentity.GetCurrent().Name or otherwise validating the value.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #17

                True. It doesn't make it 100% valid though. What if the app was run using RunAs, but without the new users profile? Suddenly, the profile path doesn't point to where the userId says it should. Making an assumption like that can also cause problems.

                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007

                P 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  True. It doesn't make it 100% valid though. What if the app was run using RunAs, but without the new users profile? Suddenly, the profile path doesn't point to where the userId says it should. Making an assumption like that can also cause problems.

                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #18

                  Well, my point is that using a parameter doesn't guarantee safety, only validation of the string will do that. Even if his snippet said ... "SELECT * FROM Y WHERE X=" + textbox1.Text ..." ... I'd still not assume that proper validation wasn't performed outside the snippet.

                  D 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Well, my point is that using a parameter doesn't guarantee safety, only validation of the string will do that. Even if his snippet said ... "SELECT * FROM Y WHERE X=" + textbox1.Text ..." ... I'd still not assume that proper validation wasn't performed outside the snippet.

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #19

                    It would appear that you missed this part of my previous post: ... The data can be corrupted by the time it reaches your validation code.

                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007

                    P 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      It would appear that you missed this part of my previous post: ... The data can be corrupted by the time it reaches your validation code.

                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #20

                      That's why we have validation code. Now, if the data gets corrupted after the validation code...

                      D 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        That's why we have validation code. Now, if the data gets corrupted after the validation code...

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #21

                        You can only do so much without resorting to specialized hardware.

                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                             2006, 2007

                        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