Connecting to a Microsoft Access database with ASP.NET - Manipulating a Microsoft Access database through ODBC using ASP.NET
(Page 5 of 6 )
Once you complete everything as suggested in the previous section, you need to work with one more button, “Add,” within the same application. Add the following code to the “Add” button available.
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnAdd.Click
Dim cn As New OdbcConnection("Driver={Microsoft Access
Driver (*.mdb)};DBQ=c:\AccessDB\MyDB.mdb")
Dim cmd As New OdbcCommand
With cmd
.CommandText = "insert into emp
(empno,ename,sal,deptno) values (" & Me.txtEmpno.Text & ",'" &
Me.txtEname.Text & "'," & Me.txtSal.Text & "," &
Me.txtDeptno.Text & ")"
.Connection = cn
.Connection.Open()
.ExecuteNonQuery()
.Connection.Close()
.Dispose()
End With
cn.Dispose()
'refresh the list
btnList_Click(Nothing, Nothing)
End Sub
Once you complete the above, you execute your application (by pressing F5), provide some reasonable values in the textboxes and finally click on the button “Add.” If you are lucky, you should be able to see the newly added row in the grid.
If you run into the same error, “Operation must use an updateable query,” you need to modify your file “web.config” by inserting the following line just below “<system.web>” tag:
<identity impersonate="true"
userName="computername\administrator" password="password" />
Replace the “computername” with your system name and “password” with the password of the “administrator” account. You can also replace “administrator” with any other customized user account with respective privileges. I simply demonstrated using the “administrator” user account.
Now, you should be able to run without any errors (hopefully).
Next: Connecting to a password protected Microsoft Access database directly using ASP.NET >>
More Microsoft Access Articles
More By Jagadish Chaterjee