Help with using DataView to insert Record..Please!

A

acs974

I'm new to ado.net/asp.net and have been on a crash course these last
few days. I've been stuck on this current problem for over a day and it
seems pretty simple, but I can't get it to work. I'd like to create a
data view from a data table, insert a record into that data view and
have it inserted back into the actual database (Access 2000). Following
tutorials online, I've been able to use a dataadapter to connect to the
database, then using a datatable, insert new rows and have them
inserted in the actual database. But when it comes to doing that same
functionality via a dataview, I've had no luck. Here is the code I've
been using. It generates no errors, and a new row is added to the
underlying datatable, but nothing is ever sent back to the database. I
think it might have something to do with the myAdpater.InsertCommand
code (are these used the same way in DataViews as in DataTables?).
Thanks in advance for any advice.

Public Sub editLevelProgressFromView()

'Objects used to connect/read from database
Dim cs As ConnectionStringSettings
Dim myConnection As OleDbConnection
Dim myAdapter As OleDbDataAdapter
Dim myTable As DataTable
Dim myView As DataView
Dim myRow As DataRowView

Dim sqlStr As String

cs = ConfigurationManager.ConnectionStrings("strConn")
myConnection = New OleDbConnection(cs.ConnectionString)


sqlStr = "Select * from tblTest"
myAdapter = New OleDbDataAdapter(sqlStr, myConnection)

'Set up insert commands for use on myAdapter.Update method
myAdapter.InsertCommand = New OleDbCommand("INSERT INTO tblTest
(c_name, c_number) VALUES(?, ?)", myConnection)
myAdapter.InsertCommand.Parameters.Add("@c_name",
OleDbType.VarChar, 15, "c_name")
myAdapter.InsertCommand.Parameters.Add("@c_number",
OleDbType.VarChar, 30, "c_number")

'Fill Table and create View
myTable = New DataTable()
myAdapter.Fill(myTable)
myView = myTable.DefaultView

'Add Row to myView, insert data via BeginEdit and EndEdit methods
myRow = myView.AddNew()
myRow.BeginEdit()
myRow("c_name") = "testName"
myRow("c_number") = "testNumber"
myRow.EndEdit()

'Accept changes in table from view
myTable.AcceptChanges()

'Commint changes to database (does not work, but no error thrown)
myAdapter.Update(myTable)

End Sub
 
C

Cor Ligthert [MVP]

Hi,
'Accept changes in table from view
myTable.AcceptChanges()
Where did you read about this command, it does not mean accept in table. It
means accept the changes as if they are done. Therefore it will never been
updated.

You can just delete this one.

I hope this helps,

Cor
 

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