Refer below sample code for your reference.
SQL
CREATE PROCEDURE [TotalCheckBoxes]
AS
BEGIN
SELECT 5 as Totalcount
END
CREATE PROCEDURE [GetListForCheckboxes]
AS
BEGIN
SELECT 1 AS ID,'First' AS Value
UNION ALL
SELECT 2 AS ID,'Second' AS Value
UNION ALL
SELECT 3 AS ID,'Third' AS Value
END
GO
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
By Count
<asp:CheckBoxList ID="chkByCount" runat="server">
</asp:CheckBoxList>
<br />
<br />
<br />
By ListValue
<asp:CheckBoxList ID="ChkByList" runat="server">
</asp:CheckBoxList>
</div>
</form>
</body>
</html>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
PopulateCountCheckBox();
PopulateListofCheckBox();
}
}
private void PopulateCountCheckBox()
{
DataTable dt = GetData("TotalCheckBoxes");
List<ListItem> list = new List<ListItem>();
int totalCount = Convert.ToInt32(dt.Rows[0]["Totalcount"]);
for (int i = 1; i <= totalCount; i++)
{
list.Add(new ListItem("Option " + i, i.ToString()));
}
chkByCount.DataSource = list;
chkByCount.DataTextField = "Text";
chkByCount.DataValueField = "Value";
chkByCount.DataBind();
}
private void PopulateListofCheckBox()
{
DataTable dt = GetData("GetListForCheckboxes");
ChkByList.DataSource = dt;
ChkByList.DataTextField = "Value";
ChkByList.DataValueField = "ID";
ChkByList.DataBind();
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
PopulateCountCheckBox()
PopulateListofCheckBox()
End If
End Sub
Private Sub PopulateCountCheckBox()
Dim dt As DataTable = GetData("TotalCheckBoxes")
Dim list As List(Of ListItem) = New List(Of ListItem)()
Dim totalCount As Integer = Convert.ToInt32(dt.Rows(0)("Totalcount"))
For i As Integer = 1 To totalCount
list.Add(New ListItem("Option " & i, i.ToString()))
Next
chkByCount.DataSource = list
chkByCount.DataTextField = "Text"
chkByCount.DataValueField = "Value"
chkByCount.DataBind()
End Sub
Private Sub PopulateListofCheckBox()
Dim dt As DataTable = GetData("GetListForCheckboxes")
ChkByList.DataSource = dt
ChkByList.DataTextField = "Value"
ChkByList.DataValueField = "ID"
ChkByList.DataBind()
End Sub
Private Function GetData(ByVal query As String) As DataTable
Dim dt As DataTable = New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("ConStr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
End Using
End Using
Return dt
End Using
End Function
Screenshot
