Results 1 to 4 of 4

Thread: DataAdapter.Update command is not updating the database.

  1. #1
    Join Date
    Jan 2009
    Posts
    44

    DataAdapter.Update command is not updating the database.

    DataAdapter.Update command is not updating the database.

    My code failed to update the Access database with the data.

    Code:
    'Open connection to update data
    Dim myConn As New OleDbConnection(ConnectString)
    myConn.Open()
    
    'Create the SQL queries
    Dim SQLSecurity As String = "SELECT * FROM [Security]"
    Dim SQLHistory As String = "SELECT * FROM [History]"
    
    'Create new DataAdapter objects
    Dim SecDataAdapter As New OleDbDataAdapter(SQLSecurity, myConn)
    Dim HistDataAdapter As New OleDbDataAdapter(SQLHistory, myConn)
    
    'Create new datasets
    Dim SecDataSet As New DataSet()
    Dim HistDataSet As New DataSet()
    
    'Fill datasets to store existing data
    SecDataAdapter.Fill(SecDataSet, "Security")
    HistDataAdapter.Fill(HistDataSet, "History")
    
    'Create command builders which is necessary to update the database
    Dim SecCommandBuilder As OleDbCommandBuilder = New
    OleDbCommandBuilder _(SecDataAdapter)
    Dim HistCommandBuilder As OleDbCommandBuilder = New
    OleDbCommandBuilder _(HistDataAdapter)
    
    For i = 0 To SecDataSet.Tables("Security").Rows.Count - 1
    'Get the current datarow from DataTable
    Dim row As DataRow = SecDataSet.Tables("Security").Rows(i)
    
    'Construct a valid security ticker
    Dim strSecurity As String = row("Ticker").ToString
    
    'Calling a function that will return historical data for a
    security
    Dim oData As Object = GetHistoricalData(strSecurity)
    
    'Add historical data to DataSet
    For j = 0 To oData.GetUpperBound(0)
    Dim NewRow As DataRow = HistDataSet.Tables("History").NewRow
    NewRow("Ticker") = row("Ticker").ToString
    NewRow("Date") = Convert.ToDateTime(oData(j, 0))
    NewRow("Price") = Convert.ToSingle(oData(j, 1))
    HistDataSet.Tables("History").Rows.Add(NewRow)
    Next
    
    HistDataSet.Tables("History").AcceptChanges()
    Dim count As Integer = oData.GetUpperBound(0)
    row("LastUpdate") = Convert.ToDateTime(oData(count, 0))
    row("DataCount") = Convert.ToInt32(count)
    SecDataSet.Tables("Security").AcceptChanges()
    
    'Update database with the updated information
    SecDataAdapter.Update(SecDataSet, "Security")
    HistDataAdapter.Update(HistDataSet, "History")
    Next
    
    'Display results
    Dim StatusForm As frmStatus
    StatusForm = New frmStatus()
    StatusForm.MdiParent = Me
    StatusForm.DataGrid1.DataSource = SecDataSet.DefaultViewManager
    StatusForm.Show()
    
    'Close connection
    myConn.Close()
    Please help me correct this code!

  2. #2
    Join Date
    May 2008
    Posts
    115

    Re: DataAdapter.Update command is not updating the database.

    Huh? You don't have to call the CB to manually create any of the action
    commands. Once you link the CB to the DA, it's done automatically behind the
    scenes. See my article on the command builder FMI.
    http://www.betav.com/msdn_magazine.htm

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: DataAdapter.Update command is not updating the database.

    You have created the command builder object but not generated for the Update commands

    Code:
    'Create command builders which is necessary to update the database
    Dim SecCommandBuilder As OleDbCommandBuilder = New
    OleDbCommandBuilder _(SecDataAdapter)
    
    SeccommandBuilder.GetUpdateCommand()
    
    Dim HistCommandBuilder As OleDbCommandBuilder = New
    OleDbCommandBuilder _(HistDataAdapter)
    
    HistCommandBuilder.GetUpdateCommand
    I hope this helps you!

  4. #4
    Join Date
    May 2008
    Posts
    40

    Re: DataAdapter.Update command is not updating the database.

    HI,
    According to me you should watch where you put those acceptchanges commands. Right before calling update, add a debug.Assert(dataSet.HasChanges) for each update and verify that there are changes to update. That looks like the culprit.

    Hope this helps you!

Similar Threads

  1. updating a database using a dataset in visual studio 2010
    By BHlophe in forum Software Development
    Replies: 2
    Last Post: 29-09-2010, 05:33 PM
  2. DataBase update failure
    By Santiaago in forum Networking & Security
    Replies: 5
    Last Post: 10-04-2010, 03:16 PM
  3. Unable to update my database using insert command
    By Govardhann in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 11:10 AM
  4. Update database with Hibernate
    By CheckMeNot in forum Software Development
    Replies: 3
    Last Post: 10-12-2009, 02:42 PM
  5. DataAdapter.Fill Exception in .NET
    By Pollock in forum Software Development
    Replies: 4
    Last Post: 06-03-2009, 04:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,102,977.08536 seconds with 16 queries