Inserting data into access database using Asp.Net

I was trying to insert data into access database from an asp.net page.

I could not find any samples in tutorials. So I thought to put the code (that worked for me) here. If anyone is looking for a sample I hope it would be helpful.

<%@ Import Namespace=”System.Data.OleDb” %>
<script runat=”server”>
Sub submit(sender As Object, e As EventArgs)

Dim conCoaxis As OleDbConnection
Dim strInsert As String
Dim cmdInsert As OleDbCommand
Dim dtmDate As DateTime
   
dtmDate = DateTime.Now()
 
conCoaxis=New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;data source=” & server.mappath(“email.mdb”))
strInsert = “INSERT INTO email (Email, Fname, CompanyName, [Date]) Values (@Email, @Fname, @CompanyName, @Date)”
 
cmdInsert = New OleDbCommand( strInsert, conCoaxis )
 
cmdInsert.Parameters.Add( “@Email”, txtEmail.Text )
cmdInsert.Parameters.Add( “@Fname”, txtName.Text )
 cmdInsert.Parameters.Add( “@CompanyName”, txtCompany.Text )
 cmdInsert.Parameters.Add( “@Date”, OleDbType.Date).Value = dtmDate.ToString(“g”)  
  Try
   conCoaxis.Open()
   cmdInsert.ExecuteNonQuery()
   conCoaxis.Close()
   Response.Write(“Updated Successfully!<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>”)
   Catch
   conCoaxis.Close()
  End Try
 End Sub
</script>
<%@ Page Language=”VB” ContentType=”text/html” ResponseEncoding=”iso-8859-1″ Debug=”true” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
</head>
<body>
<form  runat=”server” id=”form1″ name=”form1″ method=”post” action=””>
  Name:     <asp:TextBox id=”txtName” runat=”server” /> 

Email:    <asp:TextBox id=”txtEmail” runat=”server” />
Company:    <asp:TextBox id=”txtCompany” runat=”server” />
 <asp:Button id=”id” text=”Submit” OnClick=”submit” runat=”server” />
</form> 
</body>
</html>

Leave a Reply