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
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