Select Records from MS Access database and bind to Gridview

The following code selects the records from a Microsoft Access database and binds them to a Gridview control. The Access database is called 'Customers' and has a table called 'tblCustomers'. The database is located in the folder 'H:\Inetpub\wwwroot\code\Customers.mdb'.

If the datareader 'DataReader' contains records returned from the database then the records are bound to the Gridview 'Gridview1" otherwise we have set the EmptyDataText property of the gridview to display the text "There are no records".

<%@ 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 New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;" & _
    "Data Source=H:\Inetpub\wwwroot\code\Customers.mdb")
    Connection.Open()
    Dim Command As New OleDbCommand("Select * FROM tblCustomers", Connection)
    Dim DataReader As OleDbDataReader
    DataReader = Command.ExecuteReader
    GridView1.DataSource = DataReader
    GridView1.DataBind()
    DataReader.Close
    Connection.Close()

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id=Head1 runat="server">
<title>Customers</title>
</head>

<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" EmptyDataText="There are no records." />
</div>
</form>

</body>
</html>

Get the best asp hosting provider from web-hosting-top.com and save up to 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.

Global ASP.NET Hosting Leader - Click Here