Display Data Using the Repeater Control in VB
SetupIf you have not already done so, open Visual Studio and attach the database provided to you in the source files, located at the end of the tutorial. In the case where you make your own database, you can skip this attachment step.
The Concept…A repeater control is quite powerful. It is used to display a repeated list of items that are bound to the control. The steps in this tutorial will be a little backwards, as we have to build from the code behind and then the default page.
Step one.First you will need to import the System.Data.SqlClient namespace. It contains the SqlCommand and SqlConnection classes that you will need in order to connect to the database and send an SQL command to it using the target attribute.
Apart from this, we will also need to add a bit of code to the Page_Load method. Copy and paste the following code if you are starting the project from scratch:
Partial Public Class _Default : Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim cmd As SqlCommand = New SqlCommand("SELECT TOP 5 * FROM EMPLOYEES", New SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"))
Essentially, what happened here, apart from adding our SQL namespace, is that we declared the variable cmd and set it to type SqlCommand, which was given two parameters: a string that selects the top five rows of the Employees table and a new SqlConnection that receives a parameter of its own (yes a parameter receiving a parameter), which is another string with three things: server type; database name and trusted connection value (which is set to TRUE).
When the code runs, we will have the results of our SQL query assigned to the rptrExample’s DataSource property. The next step after the cmd variable is the Try-Catch block, which within it we call the DataBind() method of the rptrExample to bind the data to the control. Add the try-catch block presented below to your Default.aspx.vb file, directly under the SqlCommand line and directly before the closing bracket of the Page_Load method.
rptrExample.DataSource = cmd.ExecuteReader()
Catch ex As Exception
lblStatus.Text = ex.Message
In here the variable cmd calls the Open() method, afterwards rptrExample.DataSource is set to have the cmd variable with an ExecuteReader() method call. “rptrExample” is then set to DataBind() in order to bind the data to the control. Variable cmd then calls Close() and Dispose() methods on the Connection property before the block is closed. In the catch part of the block, it is to catch the exception ex, which is when the label text gets ex.Message.
Step two.The next step is to add the appropriate code to the default.aspx file. We will need a table with the repeater control in a cell, rptrExample will be the ID of the repeater, if you recall the code above referencing and setting values to it.
Right under the repeater, we will have and ItemTemplate with the DataBinder code and below the ItemTemplate, a label. The code for the table is presented below:
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> Repeated Employee First Name Data:</td>
<asp:Repeater ID="rptrExample" runat="server">
<br /><%# DataBinder.Eval(Container, "DataItem.firstname") %><br />
<asp:Label ID="lblStatus" runat="server"></asp:Label>
Save and run your code. You will now have five employees listed in the table we’ve created, well, if you used the database that was provided in the source files. Otherwise, you should now see the respective fields within your table that you have selected.