In this article we will learn to bind the CheckBoxList Control with Database Table. I have used the SQL Database in this project. Also I have used the IDataReader and DataTable to fetch the records from the database table. This article will beneficial when you need to show a checkboxlist with various options so that user can choose more than one checkbox. Also later my next article are coming about inserting these multiple selected values into the database.
Free Download: Bind CheckBoxList : Demo Project
Source Code:
<span style="color: #800000;"><asp:Button</span> <span style="color: #ff6600;">ID</span><span style="color: #0000ff;">="btnGetList"</span> <span style="color: #ff6600;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff6600;">onclick</span><span style="color: #0000ff;">="btnGetList_Click"</span> <span style="color: #ff6600;">Text</span><span style="color: #0000ff;">="Get List"</span> /> <span style="color: #800000;"><asp:Label</span> <span style="color: #ff6600;">ID</span><span style="color: #0000ff;">="lblerror"</span> <span style="color: #ff6600;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff6600;">Text</span><span style="color: #0000ff;">="lblerror"</span> <span style="color: #ff6600;">Visible</span><span style="color: #0000ff;">="False"</span>><span style="color: #800000;"></asp:Label></span> <span style="color: #800000;"><asp:CheckBoxList</span> <span style="color: #ff6600;">ID</span><span style="color: #0000ff;">="CheckBoxList1"</span> <span style="color: #ff6600;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff6600;">Height</span><span style="color: #0000ff;">="400px"</span> <span style="color: #ff6600;">RepeatColumns</span><span style="color: #0000ff;">="15"</span> <span style="color: #ff6600;">RepeatDirection</span><span style="color: #0000ff;">="Horizontal"</span> <span style="color: #ff6600;">Width</span><span style="color: #0000ff;">="700px"</span> <span style="color: #ff6600;">BorderStyle</span><span style="color: #0000ff;">="Solid"</span> <span style="color: #ff6600;">CellPadding</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff6600;">CellSpacing</span><span style="color: #0000ff;">="2"</span>><span style="color: #800000;"></asp:CheckBoxList></span>
.ASPX.CS Code:
<span style="color: #0000ff;">protected void</span> btnGetList_Click(<span style="color: #0000ff;">object</span> sender, <span style="color: #008080;">EventArgs</span> e)
{
<span style="color: #0000ff;">try</span>
{
<span style="color: #008080;">SqlConnection</span> con = <span style="color: #0000ff;">new</span> <span style="color: #008080;">SqlConnection()</span>;
con.ConnectionString = <span style="color: #800000;">"Data Source=.\\SQLEXPRESS;AttachDbFilename=[DataDirectory]</span>
<span style="color: #800000;"> /</span><span style="color: #800000;">CountryMasterDatabase.mdf;Integrated Security=True;User Instance=True"</span>;
<span style="color: #008080;">SqlCommand</span> com = <span style="color: #0000ff;">new</span> <span style="color: #008080;">SqlCommand()</span>;
com.Connection = con;
com.CommandText = <span style="color: #800000;">"GetList"</span>;
com.CommandType = <span style="color: #008080;">CommandType</span>.StoredProcedure;
con.Open();
<span style="color: #008080;">IDataReader</span> dr = com.ExecuteReader();
<span style="color: #008080;">DataTable</span> dt = <span style="color: #0000ff;">new</span> <span style="color: #008080;">DataTable</span>();
dt.Load(dr);
CheckBoxList1.DataSource = dt;
CheckBoxList1.DataTextField = <span style="color: #800000;">"countryname"</span>;
CheckBoxList1.DataValueField = <span style="color: #800000;">"id"</span>;
CheckBoxList1.DataBind();
con.Close();
}
<span style="color: #0000ff;">catch</span> (<span style="color: #008080;">Exception</span> ex)
{
lblerror.Text = <span style="color: #800000;">"Unable to fetch records..."</span>;
}
}