Adding a record to an Access db using DataAdapter.Update

W

Winshent

I need help with this. v simple.. but ive read a few postings regarding
this subject, but mine still doesnt work..

my problem lies with the insert command.. here is the output
###################################################
INSERT INTO tblCustomer( CustomerID , FirstName , LastName , Email )
VALUES ( ? , ? , ? , ? )
###################################################

Do i have to manually create this command?

Hers is my code below.

Public Function AddCustomer( _
ByVal CustomerID As Int64, _
ByVal Firstname As String, _
ByVal LastName As String, _
ByVal Email As String) as string

Dim ds As DataSet
Dim myTable As DataTable
Dim myNewRow As DataRow

ds = GetData("SELECT * FROM tblCustomer", "Customers")
myTable = ds.Tables("Customers")

myNewRow = myTable.NewRow
myNewRow.BeginEdit()
myNewRow.Item("CustomerID") = CustomerID
myNewRow.Item("FirstName") = Firstname
myNewRow.Item("LastName") = LastName
myNewRow.Item("Email") = Email
myNewRow.EndEdit()
ds.Tables("Customers").Rows.Add(myNewRow)

Dim s As String

s = "SELECT CustomerID, Firstname, LastName, Email FROM tblCustomer"

Dim da As OleDbDataAdapter = GetDataAdapter(s)
Dim cb As OleDbCommandBuilder
cb = New OleDbCommandBuilder(da)
da.UpdateCommand = cb.GetUpdateCommand
da.InsertCommand = cb.GetInsertCommand
da.DeleteCommand = cb.GetDeleteCommand

Try

da.Update(ds, myTable.TableName)
ds.AcceptChanges()

Catch ex As Exception
Return (ex.ToString) & da.InsertCommand.CommandText()
Finally
ds.Dispose()
End Try



Thanks in advance
 
K

Ken Tucker [MVP]

Hi,

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/commandbuilder.asp

Ken
--------------------
I need help with this. v simple.. but ive read a few postings regarding
this subject, but mine still doesnt work..

my problem lies with the insert command.. here is the output
###################################################
INSERT INTO tblCustomer( CustomerID , FirstName , LastName , Email )
VALUES ( ? , ? , ? , ? )
###################################################

Do i have to manually create this command?

Hers is my code below.

Public Function AddCustomer( _
ByVal CustomerID As Int64, _
ByVal Firstname As String, _
ByVal LastName As String, _
ByVal Email As String) as string

Dim ds As DataSet
Dim myTable As DataTable
Dim myNewRow As DataRow

ds = GetData("SELECT * FROM tblCustomer", "Customers")
myTable = ds.Tables("Customers")

myNewRow = myTable.NewRow
myNewRow.BeginEdit()
myNewRow.Item("CustomerID") = CustomerID
myNewRow.Item("FirstName") = Firstname
myNewRow.Item("LastName") = LastName
myNewRow.Item("Email") = Email
myNewRow.EndEdit()
ds.Tables("Customers").Rows.Add(myNewRow)

Dim s As String

s = "SELECT CustomerID, Firstname, LastName, Email FROM tblCustomer"

Dim da As OleDbDataAdapter = GetDataAdapter(s)
Dim cb As OleDbCommandBuilder
cb = New OleDbCommandBuilder(da)
da.UpdateCommand = cb.GetUpdateCommand
da.InsertCommand = cb.GetInsertCommand
da.DeleteCommand = cb.GetDeleteCommand

Try

da.Update(ds, myTable.TableName)
ds.AcceptChanges()

Catch ex As Exception
Return (ex.ToString) & da.InsertCommand.CommandText()
Finally
ds.Dispose()
End Try



Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top