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. Visual Basic
  4. Writing data back to a table

Writing data back to a table

Scheduled Pinned Locked Moved Visual Basic
databasehelpannouncement
3 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.
  • D Offline
    D Offline
    DA_Loring
    wrote on last edited by
    #1

    This is probably very simple to answer, and I must be doing something very silly, but I just don't seem to be able to get changes in a dataset to write back to a table in the database. I have used stored procs up 'til now but this is a quick fix needed to join two columns, date and time, into one datetime column, and I just cannot get it to work. Here is the code: Dim dsetData As New DataSet Dim dteTemp1 As Date Dim dteTemp2 As Date Dim intRowCount As Integer = 1 Using sqlcnnData As New SqlConnection(m_strConnect) Dim sqlcmdData As New SqlCommand("SELECT * FROM DDI_tbl_SystemDemandData", sqlcnnData) sqlcmdData.CommandType = CommandType.Text Dim sqldaData As New SqlDataAdapter(sqlcmdData) Dim builder As SqlCommandBuilder = New SqlCommandBuilder(sqldaData) builder.QuotePrefix = "[" builder.QuoteSuffix = "]" sqldaData.Fill(dsetData, "Dates") If dsetData.Tables(0).Rows.Count > 0 Then Dim drowData As DataRow For Each drowData In dsetData.Tables(0).Rows dteTemp1 = DateValue(drowData("DataDateTime")) dteTemp2 = drowData("DataTime") dteTemp1 = dteTemp1.AddHours(Hour(dteTemp2)) If Minute(dteTemp2) > 0 Then dteTemp1 = dteTemp1.AddMinutes(Minute(dteTemp2)) End If drowData("DataDateTime") = dteTemp1 drowData.AcceptChanges() Label1.Text = "Processed row " & intRowCount.ToString Application.DoEvents() intRowCount = intRowCount + 1 Next dsetData.AcceptChanges() intRowCount = intRowCount - 1 Label1.Text = "Completed. Processed " & intRowCount.ToString & " rows" sqldaData.Update(dsetData, "Dates") Else Label1.Text = "FAILED" End If sqldaData.Dispose() sqlcmdData.Dispose() End Using dsetData.Dispose() I don't get any errors, it runs OK, but just does not do it! I any one can point me in the right direction I would be most grateful. David Loring !! Keep Music Live !!

    M 1 Reply Last reply
    0
    • D DA_Loring

      This is probably very simple to answer, and I must be doing something very silly, but I just don't seem to be able to get changes in a dataset to write back to a table in the database. I have used stored procs up 'til now but this is a quick fix needed to join two columns, date and time, into one datetime column, and I just cannot get it to work. Here is the code: Dim dsetData As New DataSet Dim dteTemp1 As Date Dim dteTemp2 As Date Dim intRowCount As Integer = 1 Using sqlcnnData As New SqlConnection(m_strConnect) Dim sqlcmdData As New SqlCommand("SELECT * FROM DDI_tbl_SystemDemandData", sqlcnnData) sqlcmdData.CommandType = CommandType.Text Dim sqldaData As New SqlDataAdapter(sqlcmdData) Dim builder As SqlCommandBuilder = New SqlCommandBuilder(sqldaData) builder.QuotePrefix = "[" builder.QuoteSuffix = "]" sqldaData.Fill(dsetData, "Dates") If dsetData.Tables(0).Rows.Count > 0 Then Dim drowData As DataRow For Each drowData In dsetData.Tables(0).Rows dteTemp1 = DateValue(drowData("DataDateTime")) dteTemp2 = drowData("DataTime") dteTemp1 = dteTemp1.AddHours(Hour(dteTemp2)) If Minute(dteTemp2) > 0 Then dteTemp1 = dteTemp1.AddMinutes(Minute(dteTemp2)) End If drowData("DataDateTime") = dteTemp1 drowData.AcceptChanges() Label1.Text = "Processed row " & intRowCount.ToString Application.DoEvents() intRowCount = intRowCount + 1 Next dsetData.AcceptChanges() intRowCount = intRowCount - 1 Label1.Text = "Completed. Processed " & intRowCount.ToString & " rows" sqldaData.Update(dsetData, "Dates") Else Label1.Text = "FAILED" End If sqldaData.Dispose() sqlcmdData.Dispose() End Using dsetData.Dispose() I don't get any errors, it runs OK, but just does not do it! I any one can point me in the right direction I would be most grateful. David Loring !! Keep Music Live !!

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      I always use stored procs so this may be incorrect but! drowData.AcceptChanges() This sets the changed rows to current rows and removes the changed flag on the row sqldaData.Update(dsetData, "Dates") This presumably updates the change rows (which you have set to current) into the databse - it cannot find any changed rows! I suggest you remove drowData.AcceptChanges()

      D 1 Reply Last reply
      0
      • M Mycroft Holmes

        I always use stored procs so this may be incorrect but! drowData.AcceptChanges() This sets the changed rows to current rows and removes the changed flag on the row sqldaData.Update(dsetData, "Dates") This presumably updates the change rows (which you have set to current) into the databse - it cannot find any changed rows! I suggest you remove drowData.AcceptChanges()

        D Offline
        D Offline
        DA_Loring
        wrote on last edited by
        #3

        Thanks for the reply. I had just reached that conclusion myself, so your reply confirmed my suspicions. I also normally used stored procs so this one off was a new venture!!

        David Loring !! Keep Music Live !!

        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