Dynamically Populate a CheckBoxList in C#
7
Dynamically Populate a CheckBoxList in C#
//The function to bind the database data to your CheckBoxList Control
protected void bindYourList()
{
SqlConnection objConn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlServer"]);
SqlCommand objCmd = new SqlCommand("select description, id from notificationLists", objConn);
objConn.Open();
cblList.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection);
cblList.DataBind();
}
//In the Page_Load function call your behind method.
bindYourList();
//in your aspx page make sure to declare your CheckBoxList and define the DataTextField and DataValueField parameters.
<asp:checkboxlist id="cblList" runat="server" DataTextField="description" DataValueField="id">
protected void bindYourList()
{
SqlConnection objConn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlServer"]);
SqlCommand objCmd = new SqlCommand("select description, id from notificationLists", objConn);
objConn.Open();
cblList.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection);
cblList.DataBind();
}
//In the Page_Load function call your behind method.
bindYourList();
//in your aspx page make sure to declare your CheckBoxList and define the DataTextField and DataValueField parameters.
<asp:checkboxlist id="cblList" runat="server" DataTextField="description" DataValueField="id">






Not usually a good idea.
Additionally as the guy said above, you're not disposing.
Use a using(){} block for the objects that implement IDisposable.