Inserting data into access database using Asp.Net

I was trying to insert data into access database from an 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”)  
   Response.Write(“Updated Successfully!<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>”)
  End Try
 End Sub
<%@ Page Language=”VB” ContentType=”text/html” ResponseEncoding=”iso-8859-1″ Debug=”true” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” ““>
<html xmlns=”“>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<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” />

Leave a Reply