Why doesn't dataadapter.update(datatable) work?

I am trying to update a single table in an Access database. Heres my code:

Dim strSql As String = "SELECT * FROM ProductDB"
Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim builder As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(da)
Dim dr as DataRow
da.SelectCommand = New OleDb.OleDbCommand(strSql)
da.SelectCommand.Connection = conn
da.UpdateCommand = builder.GetUpdateCommand
da.InsertCommand = builder.GetInsertCommand
da.DeleteCommand = builder.GetDeleteCommand



dr = dt.NewRow
' dr.BeginEdit() 'Superfluous
dr.Item(0) = txtName.Text
dr.Item(1) = txtBrand.Text
dr.Item(2) = txtWeight.Text
dr.Item(3) = txtColor.Text
dr.Item(4) = txtPrice.Text
' dr.EndEdit() ' Superfluous

dt.rows.add(dr)

If MessageBox.Show("Add", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
dt.AcceptChanges()

Else
dt.RejectChanges()
End If



MessageBox.Show(da.Update(dt).ToString)



Anyway, my guess is that I'm not using the commandbuilder properly. Any guidance or suggestions would help a lot. Also is there a select command builder




Answer this question

Why doesn't dataadapter.update(datatable) work?

  • Hippunky

    oh really Haven't read that...was wondering why most people did that. Think I need to let someone know to fix that statement a bit! What they mean by table is the DataTable in the dataset... :-)

  • seadur

    I also want to mention that if you change the datatable, then use update, the changes are automatically written to the datasource (as though acceptchanges() was automatically called).
    So the only way to reject changes was to call the rejectchanges() before using da.update()


    Edit:

    didn't realize there was a AcceptChangesDuringUpdate property which on default is set to true. So it was indeed automatically calling the acceptchanges()


  • CHOULANT Noham

    Thanks a lot. That was the problem. Once is moved the accept changes after the update, it worked :). I was confused because the MSDN site said that acceptchanges "Commits all the changes made to this table since the last time AcceptChanges was called." so I thought that i needed to commit the changes to the datatable (in the memory) then update would copy the datatable from the memory to the datasource.


  • kmoreto

    well what happens when it doesnt work I would also remove the AcceptChanges() as this tells the dataAdapter when it comes to update that there are no updates to be made to the database but there actually is but its accepted them in the dataset...but it shouldnt be as this is done after the dataAdapter does an update() to the database

  • Why doesn't dataadapter.update(datatable) work?