Reading Records from a Microsoft Access Database with ASP.NET - Part 4

Below is the full source code for our page. It contains the HTML and VB.net code that makes up our page. It’s important to note that the VB.Net code is placed within the page_load event. This just means that every time the page is requested the page_load event is raised or run and the code we’ve written is executed.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Connection As OledbConnection
        Connection = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;" & _
                                     "Data Source=C:\Inetpub\wwwroot\code\Customers.mdb")
        Connection.Open()
        Dim Command As OleDbCommand
        Command = New OleDbCommand("SELECT * FROM tblCustomers", Connection)
        Dim DataReader As OleDbDataReader
        DataReader = Command.ExecuteReader()
        GridView1.DataSource = DataReader
        GridView1.DataBind()
       
        Connection.Close()
              
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Read from MS Access Database</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID=GridView1 runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Here’s how our records display in a browser. The Gridview control is specific to ASP.NET 2.0 so make sure that’s what you’re running either on your local server or with your hosting company. As you can see below the Gridview renders records in a tabular format.

Reading Records

Just in case there are no records to display we could set the Gridview’s emptydatatext property to ‘There are no records’ as below.

<asp:GridView ID=GridView1 runat="server" EmptyDataText=”There are no records”>             
</asp:GridView>

 

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.