In this article we discus how to bind CheckBox with Database.In Asp.Net we have two controls one is CheckBox and other is CheckBoxList. Here we discuss about CheckBoxList Control.Using this control we are able to bind our data dynamically.I am using a table name as "SkillsDetails".Table Name: SkillsCreate table Skills(SkillId int primary key,SkillName nvarchar(50))[Note: copy the above statement and paste in MS-SQL server and run it; the table is created]
private void LoadSkills(){ try { DataTable dt = new Pigo.DBUtility.SqlHelper().GetDataTable("SELECT SkillID,' '+ SkillName as SkillName from SkillDetails", null); if (dt != null) { chkSkills.DataValueField = "SkillID"; chkSkills.DataTextField = "SkillName"; chkSkills.DataSource = dt; chkSkills.DataBind(); } dt = null; } catch (Exception ex) { }}
Public String MakeString(CheckBoxList chkSkills){ int[] Skills = new int[chkSkills.Items.Count]; StringBuilder Skill = new StringBuilder(); String delimeter = String.Empty; for (int i = 0; i < chkSkills.Items.Count; i++) { if (chkSkills.Items[i].Selected) { Skills[i] = Convert.ToInt32((chkSkills.Items[i].Value).ToString()); Skill.Append(delimeter + chkSkills.Items[i].Text.ToString()); delimeter = ", "; } } String str = Skill.ToString(); Return str;}[NOTE : In this Example ,(comma) is a separator, you can replace it according to your convince
private int CheckboxEmpty(CheckBoxList chkSkills){ int selectCount = 0, unselectCount = 0; for (int i = 0; i < chkSkills.Items.Count; i++) { if (chkSkills.Items[i].Selected) selectCount++; else unselectCount++; } return selectCount;}
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: