Introduction
Repeater is a data-bound list control that allows custom layout by repeating a specified template for each item displayed in the list. In this article, I will demonstrate how to dynamically bind repeater control in ASP.NET from a database.
- Dynamically Bind DataList Control in Asp.Net from Database - Part One
- Dynamically Bind DetailsViewControl in Asp.Net from Database - Part Two
- Dynamically Bind FormViewControl in Asp.Net from Database - Part Three
- Dynamically Bind GridViewControl in Asp.Net from Database - Part Four
- Dynamically Bind ListViewControl in Asp.Net from Database - Part Five
Step 1
Create a database table in SQL Server 2014.
- CREATE TABLE [dbo].[Employees](
- [ID] [int] IDENTITY(1,1) NOT NULL,
- [Name] [nvarchar](50) NULL,
- [Position] [nvarchar](50) NULL,
- [Office] [nvarchar](50) NULL,
- [Salary] [nvarchar](50) NULL,
- [Profile_Picture] [nvarchar](50) NULL,
- CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED
- (
- [ID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- CREATE PROCEDURE [dbo].[spGetEmployees]
- AS
- BEGIN
- SELECT Name,Position,Office,Salary,Profile_Picture from [dbo].[Employees]
- END
Step 2
Open Visual Studio 2015, click on New Project, and create an empty web application project.
Screenshot for creating new project 1
Screenshot for creating new project 2
After clicking on OK, one more window will appear. Choose Empty, check on Web Forms checkbox, and click on OK.
Screenshot for creating new project 3

Double click on webconfig file and connect the database by writing the following line of code.
- <connectionStrings>
- <add name="DBCS" connectionString="data source=DESKTOP-M021QJH\SQLEXPRESS; database=DemoDB; integrated security=true;"/>
- </connectionStrings>
Step 4 - Add web form in your project
Right click on project, select Add >>Web Form.
Screenshot for adding web form 1
After clicking on web form, one window will appear. Give name to the web form, such as - Repeater or any meaningful name. Click on OK and web form will be added to the project.
Screenshot for adding web form 2
Add script and styles of bootstrap in the head section of the web form.
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
Step 6
Drag and drop a Repeater control on the web form. Design Repeater control.
- <body>
- <form id="form1" runat="server">
- <div class="container py-4">
- <h5 class="text-center text-uppercase">Repeater control in asp.net</h5>
- <asp:Repeater ID="Repeater1" runat="server">
- <ItemTemplate>
- <table class="table table-bordered">
- <tr>
- <td style="width: 20%;">
- <img alt="" width="150" class="img-thumbnail" src='<%#Eval("Profile_Picture")%>' />
- </td>
- <td>
- <table class="table">
- <tr>
- <td style="width:50%;">Employee Name:</td>
- <td><%#Eval("Name")%></td>
- </tr>
- <tr>
- <td>Position:</td>
- <td><%#Eval("Position")%></td>
- </tr>
- <tr>
- <td>Office:</td>
- <td><%#Eval("Office")%></td>
- </tr>
- <tr>
- <td>Salary:</td>
- <td><%#Eval("Salary")%></td>
- </tr>
- </table>
- </td>
- </tr>
- </table>
- </ItemTemplate>
- </asp:Repeater>
- </div>
- </form>
- </body>
Complete web form code
- <!DOCTYPE html>
- <html>
- <head runat="server">
- <title>Repeator</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div class="container py-4">
- <h5 class="text-center text-uppercase">Repeater control in asp.net</h5>
- <asp:Repeater ID="Repeater1" runat="server">
- <ItemTemplate>
- <table class="table table-bordered">
- <tr>
- <td style="width: 20%;">
- <img alt="" width="150" class="img-thumbnail" src='<%#Eval("Profile_Picture")%>' />
- </td>
- <td>
- <table class="table">
- <tr>
- <td style="width:50%;">Employee Name:</td>
- <td><%#Eval("Name")%></td>
- </tr>
- <tr>
- <td>Position:</td>
- <td><%#Eval("Position")%></td>
- </tr>
- <tr>
- <td>Office:</td>
- <td><%#Eval("Office")%></td>
- </tr>
- <tr>
- <td>Salary:</td>
- <td><%#Eval("Salary")%></td>
- </tr>
- </table>
- </td>
- </tr>
- </table>
- </ItemTemplate>
- </asp:Repeater>
- </div>
- </form>
- </body>
- </html>
Step 7
Right click and view web form code. Write the following code.
- using System;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- namespace BindDataControl_Demo
- {
- public partial class Repeator : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindRepeator();
- }
- }
- private void BindRepeator()
- {
- string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
- using (SqlConnection con = new SqlConnection(CS))
- {
- SqlCommand cmd = new SqlCommand("spGetEmployees", con);
- cmd.CommandType = CommandType.StoredProcedure;
- con.Open();
- Repeater1.DataSource = cmd.ExecuteReader();
- Repeater1.DataBind();
- }
- }
- }
- }
Step 8
Create a folder with name images in the project and add some images to the folder.
Run the project by pressing Ctrl+F5.
Screenshot
Conclusion
In this article, I have explained how to dynamically bind Repeater control from database step by step. I hope it will help you.

Firoz AhmadPosted Oct 5, 2019, 1:08 AM
Gud effort