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: Count Records from Access Database using ADO.NET, SQL, and Visual Basic.NET



Learn More about Server Intellect



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

More ASP.NET Tutorials

Counting records from Access database using ADO.NET and the .NET Framework can be accomplished easily with a few lines of code.

Also see: Count Records from SQL Database using ADO.NET, SQL, and Visual Basic.NET.

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 Database and count the records. We do this with a Count SQL command against the Customers table. We specify the UID (Unique ID) column as the column to count. To execute the Count command we simply call ExecuteScalar to produce the count. As seen below we can simply apply this to the .Text property of the lbCount Label control.

Dim cmd As New OleDbCommand("Select Count(UID) From Customers", New OleDbConnection(strConn))

cmd.Connection.Open()
lbCount.Text = cmd.ExecuteScalar()
cmd.Connection.Close()

Once we have our Count code built we put it in a Method called DisplayCustomerCount and call it from the Page_Load Event. This will alutomatically write the record count to the page.

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

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 lbCount As System.Web.UI.WebControls.Label

'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
DisplayCustomerCount()
End Sub

Sub DisplayCustomerCount()
Dim cmd As New OleDbCommand("Select Count(UID) From Customers", New OleDbConnection(strConn))
cmd.Connection.Open()
lbCount.Text = cmd.ExecuteScalar()
cmd.Connection.Close()
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!