Asp.net CheckBoxList DataSource
step1: create table category in your database, the query of the table link below
step2: insert some values into tblcategory table as shown in below figure
tblCategory table data
step3: Drag CheckBoxList control into Default.aspx Page
<asp:CheckBoxList ID=”CheckBoxList1″ runat=”server” RepeatColumns=”2″>
</asp:CheckBoxList>
Step4: write below code in Page_Load event
//Sample code
protected void Page_Load(object sender, EventArgs e)
{
//Data Source = Your server name or localĀ sqlserver name
//Data Source=./SQLEXPRESS (for local Sqlserver)
//Data Source = santhu or 192.168.1.34 (for remote servers)
string ConnectionString = “Data Source=santhu;Initial Catalog=DataBaseName;User Id=sa;Password=abc123″;
SqlConnection objConn = new SqlConnection(ConnectionString);
string query = “select CategoryID,CategoryName from tblCategory”;
SqlCommand objCmd = new SqlCommand(query, objConn);
objCmd.CommandType = CommandType.Text;
DataSet objDs = new DataSet();
SqlDataAdapter objDa = new SqlDataAdapter(objCmd);
objConn.Open();
objDa.Fill(objDs);
objCmd.ExecuteNonQuery();
CheckBoxList1.DataTextField = “CategoryName”;
CheckBoxList1.DataValueField = “CategoryID”;
CheckBoxList1.DataSource = objDs;
CheckBoxList1.DataBind();
objConn.Close();
}
Happy coding…
