The ASP.Net CheckBoxList control also supports data binding feature that allows you to bind the data items retrieved from the SQL DataBase. In this sample we have used SQL Northwind DataBase to bind the categories table with CheckBoxList control. There are 2 ways to bind the SQL data with it, one is using in-built ASP.Net datasource server control and second is using C# code to connect and fetch the data programmatically. The SqlDataSource control provides the functionality to bind the data with CheckBoxList control by following the wizard steps. You can easily connect to the SQL Database and specify the SQL query to fetch the desired fields. The CheckBoxList control has a DataSourceID property that accepts the ID of the DataSource control used to bind the SQL Data for example ID of SQLDataSource control.
In this sample we have used the C# code to connect to SQL Database and fetch the categories. For checkboxlist SQL databinding using C# code we have used the DataSource property to pass the data object containing SQL records. Whether you use SqlDataSource control or C# code for SQL DataBinding you have to use the following 2 properties of CheckBoxList control to specify the text and value property for its checkbox type list items:
Here we have specified the "CategoryName" field as DataTextField to display the text content for checkbox list item and "CategoryID" field as DataValueField to set the back end value for each list item.
See the sample below to understand the output result of ASP.Net CheckBoxList control SQL DataBinding: