
Navigator : Home > Tutorials > Database Tutorials > ...
Displaying Data using ASP.NET 2.0 Repeater and C#
This tutorial will show you how to display data using the .NET Repeater Control, ASP.NET 2.0 and C#.NET
The Repeater control is a powerful tool and is easy to use.
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
First, you will need to import the System.Data.SqlClient namespace.
The System.Data.SqlClient namespace contains the SqlCommand and SqlConnection classes that we need in order to
connect to our database and to send an SQL command to it.
| using System.Data.SqlClient; |
We'll put our code in the Page_Load() event. This makes the most sense, as most of the time we will want our data to be displayed when the page loads.
When the Page_Load() event fires, a new SqlCommand object is instantiated with our connection string and our command.
Afterwards, we will attempt to connect using the Open() method of our cmd.Connection object. Once it is connected we will attempt to execute the command we specified earlier (in this example "SELECT TOP 5 * FROM EMPLOYEES" in the Northwind db).
If all goes well, we will have the results of our SQL query assigned to the rptrExample's DataSource property. Now all we have to do is call the DataBind() method of our rptrExample to bind the data to the control. The data is now ready to be displayed.
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;")); try {
cmd.Connection.Open(); rptrExample.DataSource = cmd.ExecuteReader(); rptrExample.DataBind();
cmd.Connection.Close(); cmd.Connection.Dispose(); } catch (exception ex) {
lblStatus.Text = ex.Message; } } |
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
We have to add a few tags on the front end of the .aspx page to place where we want the Repeater control to display its bound data. We also specify what part of the data from the data set we would like to display (in this case, "DataItem.firstname"). The front end .aspx page looks something like this:
<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> |
The flow for the code behind page is as follows.
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; 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;")); try {
cmd.Connection.Open(); rptrExample.DataSource = cmd.ExecuteReader(); rptrExample.DataBind();
cmd.Connection.Close(); cmd.Connection.Dispose(); } catch (Exception ex) {
lblStatus.Text = ex.Message; } } } |
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!