Inserting Records into a Microsoft Access Database with ASP.NET - Part 1

This tutorial follows on from Reading records from an Access Database with ASP.NET and ADO.NET and shows you how to insert a record into a Microsoft Access Database.

As we decided to connect to a Microsoft Access database ‘Customers.mdb’ in part one we’ll be using the classes that the System.Data.Oledb namespace provides.

First of all we need to import the namespace at the top of our page.

<%@ Import Namespace="System.Data.Oledb" %>

If we didn’t import the namespace we’d obviously get an error when we tried to use the classes and create objects.

Next we need to think about creating a connection object that will allow us to connect to our Microsoft Access database. We’ll declare an OledbConnection variable that’s fit for the purpose of holding a connection object.

Dim Connection As OledbConnection

‘Dim Connection As String’ obviously wouldn’t suffice, as we need a variable that can specifically store a connection object.

After declaring the connection variable we need to create a connection object. We do this using the New Keyword and at the same time we pass in a parameter i.e. the connection string to our newly created connection object. The connection string will hold the provider name that in our case is Microsoft.jet.Oledb4.0, the file name and path to the database (‘C:\Inetpub\wwwroot\code\Customers.mdb’).

Once we’ve created our connection object we can assign it to our connection variable ‘connection’.

Connection = New OledbConnection("Provider=Microsoft.Jet.Oledb.4.0;" & _
"Data Source=C:\Inetpub\wwwroot\code\Customers.mdb" )

If we didn’t pass in a connection string then the connection object wouldn’t know where to look.

We can now open the connection object.

Connection.Open()

Get the best asp web hosting provider now and save 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.