updating the databse from dataset is not working

  • Thread starter Thread starter JH
  • Start date Start date
J

JH

I have the following simple code. I can see the changes in the dataset by
displaying it to a data grid. All the new rows added
can be seen on the data grid. But when I try to update the database, it does
not commit. What am I doing wrong?? I tried both
codes at the end of this message.
Any help appreciated


dim ConnectionString2 As String =
"Provider=Microsoft.Jet.OLEDB.4.0;Password='';" & _

"User ID=Admin;Data Source= C:\temp\account.mdb;" & _

"Jet OLEDB:Database Password='password';"

Dim Connection2 As OleDbConnection = New OleDbConnection(ConnectionString2)

Dim sqlCommand2 As String = "SELECT * from acc_checking"

Dim command2 As OleDbCommand = New OleDbCommand(sqlCommand2)

command2.CommandType = CommandType.Text

Connection2.Open()

command2.Connection = Connection2



Dim oleDBDataadapter2 As OleDbDataAdapter = New OleDbDataAdapter

oleDBDataadapter2.SelectCommand = command2

Dim LogDs2 As New DataSet

oleDBDataadapter2.Fill(LogDs2, "acc_checking")


'add row to the dataset

Dim row As DataRow

row = LogDs2.Tables("Acc_Checking").NewRow()

row("PinNumber") = CDec(TPinNumber)

row("TransactDate") = Now.Date

row("Credit") = 0.0

row("Debit") = AmtMoney

row("Cbalance") = TCbalance

'update data table from the current dataset

oleDBDataadapter2.Update(LogDs2)

' oleDBDataadapter2.Update(LogDs2, "Acc_checking")
 
JH:

What does your INsert command look like? The update you commented out
should be used, but make sure that table names match. The main problem
though is that the row you create isn't added to the datatable... Right
after this line [ row("Cbalance") = TCbalance] use
LogDs2.Tables("Acc_checking").Rows.Add(row) so the row gets added. If you
do this , your dataset shoudl HaveChanges. Hence, call
Debug.Assert(LogsDs2.HasChanges) . The assertion should fail (assuming you
didn't just add the line I mention) and you'll get a big ugly box telling
you it failed asking what you want to do. If your dataset doesn't have
changes, you can call update all year and nothing will happen. If it does,
and the update command /insert command/delete command's are correct, you
should be good to go. So make sure the row is added, that the dataset does
in fact have changes, and that your insert/update/delete logic is set (in
this instance, all you'll need is InsertCommand b/c you aren't deleting or
updating rows, however, you'll need those other commands in all likelihood
for the rest of your app).

HTH,

Bill
 
Back
Top