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. Database & SysAdmin
  3. Database
  4. Update a datatable to database

Update a datatable to database

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintestingbeta-testing
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.
  • B Offline
    B Offline
    Bluebamboo
    wrote on last edited by
    #1

    Hi guys, I got a datatable from a excel file, and try to update this table onto sql server, in other words, the users can change the table in sql server based on excel file. I use SqlDataAdapter to update the table, but the problem is no change happen on my database. '... Dim cmdSql As SqlCommand = New SqlCommand cmdSql.Connection = connSql cmdSql.CommandText = "UPDATE pfSchool SET notes = @notes WHERE ID = @id" cmdSql.Parameters.Add(New SqlParameter("@notes", SqlDbType.NText)) cmdSql.Parameters("@notes").SourceColumn = "notes" cmdSql.Parameters.Add(New SqlParameter("@id", SqlDbType.Int)) cmdSql.Parameters("@id").SourceColumn = "ID" ' modify the data for testing dtData.Rows(0)("notes") = "test2" Dim daSql As SqlDataAdapter = New SqlDataAdapter daSql.UpdateCommand = cmdSql daSql.UpdateCommand.Connection.Open() daSql.Update(dtData) '... Above is a piece of codes which i made for testing. Any suggestion would be appreciated! Ming

    J 1 Reply Last reply
    0
    • B Bluebamboo

      Hi guys, I got a datatable from a excel file, and try to update this table onto sql server, in other words, the users can change the table in sql server based on excel file. I use SqlDataAdapter to update the table, but the problem is no change happen on my database. '... Dim cmdSql As SqlCommand = New SqlCommand cmdSql.Connection = connSql cmdSql.CommandText = "UPDATE pfSchool SET notes = @notes WHERE ID = @id" cmdSql.Parameters.Add(New SqlParameter("@notes", SqlDbType.NText)) cmdSql.Parameters("@notes").SourceColumn = "notes" cmdSql.Parameters.Add(New SqlParameter("@id", SqlDbType.Int)) cmdSql.Parameters("@id").SourceColumn = "ID" ' modify the data for testing dtData.Rows(0)("notes") = "test2" Dim daSql As SqlDataAdapter = New SqlDataAdapter daSql.UpdateCommand = cmdSql daSql.UpdateCommand.Connection.Open() daSql.Update(dtData) '... Above is a piece of codes which i made for testing. Any suggestion would be appreciated! Ming

      J Offline
      J Offline
      Joe 2
      wrote on last edited by
      #2

      Bluebamboo wrote:

      cmdSql.CommandText = "UPDATE pfSchool SET notes = @notes WHERE ID = @id"

      I believe that the problem lies in here. You'll have to pass values with the variable enclosed between Quotes and Ampersand '" & notes & "' Having said that,you might want to restructure your code as it is vulnerable to Sql Injections[^].

      Give a man a fish, he'll eat for a day. Teach a man how to fish, he'll eat for lifetime. Pradeep Joe

      B 1 Reply Last reply
      0
      • J Joe 2

        Bluebamboo wrote:

        cmdSql.CommandText = "UPDATE pfSchool SET notes = @notes WHERE ID = @id"

        I believe that the problem lies in here. You'll have to pass values with the variable enclosed between Quotes and Ampersand '" & notes & "' Having said that,you might want to restructure your code as it is vulnerable to Sql Injections[^].

        Give a man a fish, he'll eat for a day. Teach a man how to fish, he'll eat for lifetime. Pradeep Joe

        B Offline
        B Offline
        Bluebamboo
        wrote on last edited by
        #3

        Thanks for your reply I don't think that causes the problem, you can check the solution provided by Micsoft support http://support.microsoft.com/kb/308055[^] and the solution declare a SqlCOmmand with such a query DAUpdateCmd = New SqlCommand("Update CustTest set CustName = @pCustName where CustId = @pCustId", da.SelectCommand.Connection) which is actually perform same function as what I did

        J 1 Reply Last reply
        0
        • B Bluebamboo

          Thanks for your reply I don't think that causes the problem, you can check the solution provided by Micsoft support http://support.microsoft.com/kb/308055[^] and the solution declare a SqlCOmmand with such a query DAUpdateCmd = New SqlCommand("Update CustTest set CustName = @pCustName where CustId = @pCustId", da.SelectCommand.Connection) which is actually perform same function as what I did

          J Offline
          J Offline
          Joe 2
          wrote on last edited by
          #4

          Bluebamboo wrote:

          http://support.microsoft.com/kb/308055\[^\]

          Apologize for my ignorance. I didn't know that before. I tried out that sample that you've guided me with, it works like charm. Can you try providing a mapping table name when you fill and update the adapter (in case you are using a dataset) ? da = New SqlDataAdapter("select * from CustTest order by CustId", cn) da.Fill(CustomersDataSet, "Customers") da.Update(CustomersDataSet, "Customers") If you already have that in place, kindly update me on how you got it working once you achieve it. Good Luck friend :-)

          Give a man a fish, he'll eat for a day. Teach a man how to fish, he'll eat for lifetime. Pradeep Joe

          B 1 Reply Last reply
          0
          • J Joe 2

            Bluebamboo wrote:

            http://support.microsoft.com/kb/308055\[^\]

            Apologize for my ignorance. I didn't know that before. I tried out that sample that you've guided me with, it works like charm. Can you try providing a mapping table name when you fill and update the adapter (in case you are using a dataset) ? da = New SqlDataAdapter("select * from CustTest order by CustId", cn) da.Fill(CustomersDataSet, "Customers") da.Update(CustomersDataSet, "Customers") If you already have that in place, kindly update me on how you got it working once you achieve it. Good Luck friend :-)

            Give a man a fish, he'll eat for a day. Teach a man how to fish, he'll eat for lifetime. Pradeep Joe

            B Offline
            B Offline
            Bluebamboo
            wrote on last edited by
            #5

            Hi Joe, Thanks for you suggestion, Problem has been *solved*, I haven't found where exactly cause the problem, because it worked once suddenly, and now it always works. Amazing! I think it is not a problem with the code I pasted, probably it was something wrong with other potential reasons, which I don't know exactly, but one of them what I guess could be this[^] Thanks. Ming

            J 1 Reply Last reply
            0
            • B Bluebamboo

              Hi Joe, Thanks for you suggestion, Problem has been *solved*, I haven't found where exactly cause the problem, because it worked once suddenly, and now it always works. Amazing! I think it is not a problem with the code I pasted, probably it was something wrong with other potential reasons, which I don't know exactly, but one of them what I guess could be this[^] Thanks. Ming

              J Offline
              J Offline
              Joe 2
              wrote on last edited by
              #6

              Glad that it works now. Thanks for sharing that piece of information :-)

              Give a man a fish, he'll eat for a day. Teach a man how to fish, he'll eat for lifetime. Pradeep Joe

              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