web form to insert record in access database-error

S

sudhinma

Hello all,

I created a web form to enter a new record to a access database called
student.mdb.
But when i click the save button(button1) the record does not get
added and i get a exception. the code is in vb and i have used visual
web developer 2005 express edition.the code is as under. IS this the
right way to do it.If not pls suggest a better method.

regards
sudhin

Imports System.Data.OleDb

Partial Class _Default
Inherits System.Web.UI.Page
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
cn = New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Visual
Studio 2005\WebSites\WebSite3\App_Data\student.mdb;")

cmd = New OleDbCommand("INSERT INTO student1 (Name,Reg
no,College)VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "','" &
TextBox3.Text & "')", cn)

cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
Catch ex As Exception
TextBox1.Text = ex.StackTrace
End Try


End Sub
End Class
 
Joined
May 16, 2006
Messages
27
Reaction score
0
Your code is correct,

1. check the path you are using for the database is correct.
2. check whether the mdb file is readonly or not. if it is readonly , remove the readonly.
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top