Iterate through the DataReader Records

The following code connects to a Microsoft Access Database and iterates through the DataReader records row by row. The records returned are assigned to a label 'lblRecords' controls. If there are no records the text property of the label will display the message "There are no customers."

<%@ 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()

    If DataReader.Read() Then
      Do While DataReader.Read()
       lblRecords.Text &= DataReader("FirstName") & " "
       lblRecords.Text &= DataReader("LastName") & "<br />"
      Loop
     Else
       lblRecords.Text = "There are no customers."
    End If

    Connection.Close()

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Iterate through Datareader records</title>
</head>
<body>

<form id="form1" runat="server">
<div>
<asp:label ID="lblRecords" runat="server" />
</div>
</form>

</body>
</html>

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.