In development, there are always multiple ways to get to a solution. A majority of tutorials will show how to display data with the use of a DataGrid and while that is alright and all, we will take a different approach. In this tutorial, you will learn how to implement a repeater control in Visual C#.

Setup

If 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.

We used over 10 web hosting companies before we found Server Intellect. Our cloud hosting was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.

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:
Code Block
Default.aspx.cs

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {


     SqlCommand cmd = new SqlCommand("SELECT TOP 5 * FROM EMPLOYEES"new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"));


    }
}

 


Essentially, apart from adding our SQL namespace, 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.cs file, directly under the SqlCommand line and directly before the closing bracket of the Page_Load method.
Code Block
Default.aspx.cs
     try {

     cmd.Connection.Open();

     rptrExample.DataSource = cmd.ExecuteReader();

     rptrExample.DataBind();

     cmd.Connection.Close();
     cmd.Connection.Dispose();

     }
     catch (Exception ex) { 
       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, the exception ex was used, 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:
Code Block
Default.aspx

<table width=”600″ border=”0″ align=”center” cellpadding=”5″ cellspacing=”1″ bgcolor=”#cccccc”> 
<tr>
<
td width=”100″ align=”right” bgcolor=”#eeeeee” class=”header1″> Repeated Employee First Name Data:</td>
<
td bgcolor=”#FFFFFF”> 
<asp:Repeater ID=”rptrExample” runat=”server”> 
<ItemTemplate> 
<br /><%# DataBinder.Eval(Container, “DataItem.firstname”%><br />
</
ItemTemplate>
</
asp:Repeater>
<
asp:Label ID=”lblStatus” runat=”server”></asp:Label>
</
td>
</
tr>
</
table> 


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.

A Few Last Words…

Learning database control and manipulation is easy with ASP.NET and we intend to be there every step of the way. Thank you for being a valued reader and join us next time for additional database tutorials!

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect’s help, we were able to avoid any headaches!

Repeater_Control_Source_2010CS.zip