Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 03-02-2009
Member
 
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!
Reply With Quote
  #2  
Old 03-02-2009
Member
 
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
Reply With Quote
  #3  
Old 03-02-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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!
Reply With Quote
  #4  
Old 03-02-2009
Member
 
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!
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 03:19 AM.