Introduction

In this article, I will demonstrate how to dynamically bind asp.net server control from the database. I will bind DropdowList, RadioButtonList, CheckBoxList, ListBox, and BulletedList.

Step 1

Create database table in sql server 2014.

  1. CREATE TABLE [dbo].[Languages](
  2. [ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Programming_Language] [nvarchar](50) NULL,
  4. CONSTRAINT [PK_Languages] PRIMARY KEY CLUSTERED
  5. (
  6. [ID] ASC
  7. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  8. ) ON [PRIMARY]
  9. GO
  10. CREATE procedure spGetAllProgrammingLanguage
  11. as
  12. begin
  13. select ID,Programming_Language from [dbo].[Languages]
  14. end
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

ASP.NET

After clicking on New Project one window will appear; select Web from left panel, choose ASP.NET Web Application and give a meaningful name to your project then click on OK as shown in below screenshot.

Screenshot for creating new project 2

ASP.NET

After clicking on OK one more window will appear. Choose empty, check on the Web Forms checkbox and click on OK.

Screenshot for creating new project 3

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

Right click on project and web form and give it a meaningful name.

Step 5

Add script and styles of bootstrap in the head section of 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>

Step 6

Design web form, and drag and drop DropdowList control

  1. <!DOCTYPE html>
  2. <html>
  3. <head runat="server">
  4. <title></title>
  5. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div class="container py-4">
  12. <h4 class="text-uppercase text-center">HOW TO DYNAMICALLY BIND ASP.NET DROPDOWNLIST CONTROL FROM DATABASE</h4>
  13. <div class="row">
  14. <div class="col-md-6">
  15. <div class="form-group">
  16. <label>Choose Programming Language</label>
  17. <asp:DropDownList ID="DropDownList1" runat="server" CssClass="custom-select"></asp:DropDownList>
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. </form>
  23. </body>
  24. </html>

Step 7

Right click on web form choose view code. Write following code to bind DropdowList control.

  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. namespace ServerControlBinding_Demo
  6. {
  7. public partial class DropDownList : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. if (!IsPostBack)
  12. {
  13. BindDropdowList();
  14. }
  15. }
  16. private void BindDropdowList()
  17. {
  18. string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
  19. using (SqlConnection con = new SqlConnection(CS))
  20. {
  21. SqlCommand cmd = new SqlCommand("spGetAllProgrammingLanguage", con);
  22. cmd.CommandType = CommandType.StoredProcedure;
  23. con.Open();
  24. DropDownList1.DataSource = cmd.ExecuteReader();
  25. DropDownList1.DataTextField = "Programming_Language";
  26. DropDownList1.DataValueField = "ID";
  27. DropDownList1.DataBind();
  28. DropDownList1.Items.Insert(0, "Choose Programming Language");
  29. }
  30. }
  31. }
  32. }

Output for DropdowList

ASP.NET

Step 8

Design web form drag and drop RadioButtonList control

  1. <!DOCTYPE html>
  2. <html>
  3. <head runat="server">
  4. <title>RadioButtonLis</title>
  5. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div class="container py-4">
  12. <h5 class="text-uppercase text-center">HOW TO DYNAMICALLY BIND ASP.NET RADIOBUTTONLIST CONTROL FROM DATABASE</h5>
  13. <div class="row">
  14. <div class="col-md-6">
  15. <div class="form-group">
  16. <label>Choose Programming Language</label>
  17. <asp:RadioButtonList ID="RadioButtonList1" runat="server" CssClass="custom-radio"></asp:RadioButtonList>
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. </form>
  23. </body>
  24. </html>

Step 9

Write the following code to bind DropdowList control.

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

Output for RadioButtonList

ASP.NET

Step 10

Design web form drag and drop ListBox control

  1. <!DOCTYPE html>
  2. <html>
  3. <head runat="server">
  4. <title>ListBox</title>
  5. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div class="container py-5">
  12. <h4 class="text-uppercase text-center">HOW TO DYNAMICALLY BIND ASP.NET LISTBOX CONTROL FROM DATABASE</h4>
  13. <div class="row">
  14. <div class="col-md-6">
  15. <div class="form-group">
  16. <label>Choose Programming Language</label>
  17. <asp:ListBox ID="ListBox1" runat="server" CssClass="form-control"></asp:ListBox>
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. </form>
  23. </body>
  24. </html>

Step 11

Write the following code to bind ListBox control.

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

Output for RadioButtonList

ASP.NET

Step 12

Design web form drag and drop CheckBoxList control

  1. <!DOCTYPE html>
  2. <html>
  3. <head runat="server">
  4. <title>CheckBoxList</title>
  5. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div class="container py-5">
  12. <h5 class="text-uppercase text-center">HOW TO DYNAMICALLY BIND ASP.NET CHECKBOXLIST CONTROL FROM DATABASE</h5>
  13. <div class="row">
  14. <div class="col-md-6">
  15. <div class="form-group">
  16. <label>Choose Programming Language</label>
  17. <asp:CheckBoxList ID="CheckBoxList1" runat="server" CssClass="custom-checkbox"></asp:CheckBoxList>
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. </form>
  23. </body>
  24. </html>

Step 13

Write the following code to bind CheckBoxList control.

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

Output for CheckBoxList

ASP.NET

Step 14

Design web form drag and drop BulletedList control,

  1. <!DOCTYPE html>
  2. <html>
  3. <head runat="server">
  4. <title>BulletedList</title>
  5. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div class="container py-5">
  12. <h5 class="text-uppercase text-center">HOW TO DYNAMICALLY BIND ASP.NET BULLETEDLIST CONTROL FROM DATABASE</h5>
  13. <div class="row">
  14. <div class="col-md-6">
  15. <div class="form-group">
  16. <label>Choose Programming Language</label>
  17. <asp:BulletedList ID="BulletedList1" runat="server"></asp:BulletedList>
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. </form>
  23. </body>
  24. </html>

Step 15

Write the following code to bind BulletedList control.

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

Output for BulletedList

ASP.NET

Conclusion

In this article, I have explained data binding with ASP.NET server control like DropdowList, RadioButtonList, CheckBoxList, ListBox, and BulletedList. I hope you have understood all the controls.