Home ASP.NET Hosting Dedicated Servers Contact Us
Announcements
Virtual Tiers
.NET Applications
XML Web Services
SQL Server Database
Web Traffic Statistics
Much More...
ASP.NET Web Site Hosting
Dedicated Servers
Windows 2003 Server
2.4 GHz Pentium 4
1024 MB RAM
80 GB Hard Drive
1000 GB/month
Fully Managed
Free Setup!
$268.00/month
Windows Dedicated Servers
Specialized Plans
10 or more Domains
Windows Services
Custom Plans
Windows Services
Learning & Support
About Us
ASP.NET Tutorial Web Sites
123aspx.com
411asp.net
dotnetfreaks.com
wwwcoder.com



ASP.NET Tutorial: Adding records to a database using ADO.NET, Access Database and Visual Basic.NET



Learn More about Server Intellect



Download the Visual Studio.NET 2003 VB.NET Project Here

More ASP.NET Tutorials

Adding records to Access database using ADO.NET a fairly strait forward process and is much like adding records to SQL Server 2000.

First, you will need to import the System.Data.OleDb name space at the top of your page.

Imports System.Data.OleDb

Next, we write the actual code to connect to Access and insert the data. We apply the below commands to the Click Event of a button control. Notice that we are getting our values from the .Text property of the txtFirstName TextBox and the txtLastName TextBox.

Dim cmd As New OleDbCommand("INSERT INTO Customers (FirstName, LastName)VALUES('" & txtFirstName.Text & "','" & txtLastName.Text & "')", New OleDbConnection(strConn))

cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()


After we add the data above, we want to display it on the page along with all past records added. This is done by calling a Method that will bind the data to a repeater control.

Dim strSQL As String = "Select * From Customers"
Dim cmd As New OleDbCommand(strSQL, New OleDbConnection(strConn))

cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()

The full listing for the code behind page is as follows

Imports System.Data.OleDb

Public Class index
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents btnInsert As System.Web.UI.WebControls.Button
Protected WithEvents txtFirstName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLastName As System.Web.UI.WebControls.TextBox
Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Public strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Server.MapPath("db1.mdb")

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadData()
End Sub

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click

Dim cmd As New OleDbCommand("INSERT INTO Customers (FirstName, LastName)VALUES('" & txtFirstName.Text & "','" & txtLastName.Text & "')", New OleDbConnection(strConn))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
LoadData()
End Sub

Sub LoadData()

Dim strSQL As String = "Select * From Customers"
Dim cmd As New OleDbCommand(strSQL, New OleDbConnection(strConn))
cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
End Sub

End Class


Download the Visual Studio.NET 2003 VB.NET Project Here

Do you have an ASP.NET Tutorial you would like to see here? Contact Us!