Dynamically Bind DataList Control In ASP.NET From Database - Part One

Introduction

Data-bound web server controls are controls that can be bound to a data source control to make it easy to display and modify data in your web application. DataList a data-bound list control that displays items using templates. In this article, I will demonstrate how to dynamically bind DataList control in asp.net from the database.

There are several data-bound server controls.

  1. DataList control
  2. DetailsView control
  3. FormView control
  4. GridView control
  5. ListView control
  6. Repeater control
  7. Chart Control

Step 1

Create database table in SQL Server 2014.

  1. CREATE TABLE [dbo].[ProductsList](  
  2.     [ProductId] [int] IDENTITY(1000,1) NOT NULL,  
  3.     [ProductName] [nvarchar](50) NULL,  
  4.     [ProductPrice] [nvarchar](50) NULL,  
  5.     [ProductDescription] [nvarchar](max) NULL,  
  6.     [ProductImage] [nvarchar](100) NULL,  
  7.  CONSTRAINT [PK_ProductsList] PRIMARY KEY CLUSTERED   
  8. (  
  9.     [ProductId] ASC  
  10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]  
  11. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  
  12.   
  13. GO  
  1. CREATE PROCEDURE spGetProductList  
  2. AS  
  3. BEGIN  
  4. SELECT*FROM [dbo].[ProductsList]  
  5. END  
Bind DataList Control in Asp.Net

Step 2

Open Visual Studio 2015, click on New Project and create an empty web application project.

Screenshot for creating new project 1

Bind DataList Control in Asp.Net 

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

Bind DataList Control in Asp.Net 

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

Bind DataList Control in Asp.Net 

Step 3

Double click on webconfig file and database connections. Write the following line of code.

  1. <connectionStrings>  
  2.   <add name="DBCS" connectionString="data source=DESKTOP-M021QJH\SQLEXPRESS; database=DemoDB; integrated security=true;"/>  
  3. </connectionStrings>  

Step 4

Add web form in your project.

Right click on project, select Add, choose Web Form, and click.

Screenshot adding web form 1

 Bind DataList Control in Asp.Net

After clicking on web form, one window will appear. Give the name DataList or any meaningful name. Click on OK. The web form will be added to the project.

Screenshot adding web form 2

 Bind DataList Control in Asp.Net

Step 5

Add script and styles of bootstrap in head section of the web form.

  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">  
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  3. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>  
  4. <style>  
  5.         .productList {  
  6.             border: 1px solid #6c757d;  
  7.             margin-bottom: 10px;  
  8.             padding: 15px;  
  9.             border-radius: 3px;  
  10.         }  
  11.     </style>  

Step 6

Drag and drop a DataList control on the web form. Design DataList control.

  1. <body>  
  2.     <form id="form1" runat="server">  
  3.         <div class="container py-4">  
  4.             <h5 class="text-center text-uppercase">DataList control in asp.net</h5>  
  5.             <asp:DataList ID="DataList1" runat="server" CssClass="row">  
  6.                 <ItemTemplate>  
  7.                     <div class="row productList">  
  8.                         <div class="col-4">  
  9.                             <img alt="" width="250" src='<%#Eval("ProductImage")%>' />  
  10.                         </div>  
  11.                         <div class="col-8">  
  12.                             <h4><%#Eval("ProductName")%></h4>  
  13.                             <h6><%#Eval("ProductPrice")%></h6>  
  14.                             <div>  
  15.                                 <%#Eval("ProductDescription")%>  
  16.                             </div>  
  17.                         </div>  
  18.                     </div>  
  19.                 </ItemTemplate>  
  20.             </asp:DataList>  
  21.         </div>  
  22.     </form>  
  23. </body>  

Complete web form code

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4. <head runat="server">  
  5.     <title>DataList</title>  
  6.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">  
  7.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  8.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>  
  9.     <style>  
  10.         .productList {  
  11.             border: 1px solid #6c757d;  
  12.             margin-bottom: 10px;  
  13.             padding: 15px;  
  14.             border-radius: 3px;  
  15.         }  
  16.     </style>  
  17. </head>  
  18. <body>  
  19.     <form id="form1" runat="server">  
  20.         <div class="container py-4">  
  21.             <h5 class="text-center text-uppercase">DataList control in asp.net</h5>  
  22.             <asp:DataList ID="DataList1" runat="server" CssClass="row">  
  23.                 <ItemTemplate>  
  24.                     <div class="row productList">  
  25.                         <div class="col-4">  
  26.                             <img alt="" width="250" src='<%#Eval("ProductImage")%>' />  
  27.                         </div>  
  28.                         <div class="col-8">  
  29.                             <h4><%#Eval("ProductName")%></h4>  
  30.                             <h6><%#Eval("ProductPrice")%></h6>  
  31.                             <div>  
  32.                                 <%#Eval("ProductDescription")%>  
  33.                             </div>  
  34.                         </div>  
  35.                     </div>  
  36.                 </ItemTemplate>  
  37.             </asp:DataList>  
  38.         </div>  
  39.     </form>  
  40. </body>  
  41. </html>  

Step 7

Right click and view web form code. Write the following code.

  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5.   
  6. namespace BindDataControl_Demo  
  7. {  
  8.     public partial class DataList : System.Web.UI.Page  
  9.     {  
  10.         protected void Page_Load(object sender, EventArgs e)  
  11.         {  
  12.             if (!IsPostBack)  
  13.             {  
  14.                 BindDataList();  
  15.             }  
  16.         }  
  17.   
  18.         private void BindDataList()  
  19.         {  
  20.             string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
  21.             using (SqlConnection con = new SqlConnection(CS))  
  22.             {  
  23.                 SqlCommand cmd = new SqlCommand("spGetproductList", con);  
  24.                 cmd.CommandType = CommandType.StoredProcedure;  
  25.                 con.Open();  
  26.                 DataList1.DataSource = cmd.ExecuteReader();  
  27.                 DataList1.DataBind();  
  28.             }  
  29.         }  
  30.     }  
  31. }  

Step 8: Create a folder under project with name Products add some product images 

Step 9: Run the project by pressing ctrl+F5.

Screenshot

 Bind DataList Control in Asp.Net

Conclusion

In this article, I have explained how to dynamically bind DataList control from database step by step. Hope it will help you.


Similar Articles