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.
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
After clicking on New Project, one window will appear. Select Web from the left panel, choose ASP.NET Web Application, give a meaningful name to your project, then click on OK.
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
Step 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
Step 5
Add script and styles of bootstrap in the head section of the web form.