Hi you can bind your check box with multiple ways :-
1). Using Datareader:-
Sqlconnection con = new SqlConnection("Your connection string");
con.open();
SqlCommand cmd = new SqlCommand(con);
cmd.commandtext = "select emp_id,emp_name from employee_master where status = 'AA'";
SqlDataReader dr = cmd.ExecuteReader();
chk_lst_emp.Items.Clear();
While(dr.read())
{
try
{
if(!IsDBNull(dr("emp_name"))
chk_lst_emp.Iteams.Add(New ListItem(convert.tostring(dr("emp_id")),convert.tostring(dr("emp_name")));
}
catch(exception ex)
{
throw ex;
}
finally
{
if(dr!=null) dr.close();
if(con!=null) con.close();
}
}
2). Using DataSet:-
Sqlconnection con = new SqlConnection("Your connection string");
//no need to open a connection because dataset is a disconnected architecture
SqlCommand cmd = new SqlCommand(con);
cmd.commandtext = "select emp_id,emp_name from employee_master where status = 'AA'";
SqlDataAdapter da = new SqlDataAdapter(cmd);
Dataset ds = new Dataset();
da.Fill(ds,"Employee_Master");
if(ds.tables(0).rows.count>0)
{
chk_lst_emp.DataSource = ds;
chk_lst_emp.DataTextField = "emp_name";
chk_lst_emp.DataValueField = "emp_id";
chk_lst_emp.DataBind();
}
else
{
chk_lst_emp.DataSource = null;
chk_lst_emp.DataBind();
}
3). Using List(Generics) for this you need to create separate class and make properties like
Public Class Employee_Master
{
private string emp_id = string.empty;
private string emp_name = string.empty;
public string Employee_Id
{
get{return emp_id;}
set{emp_id = value;}
}
//make same for emp_name
}
Public void fill_checkbox_list()
{
//take list as
List lst_employee = new List(Employee_Master);
Employee_Master emp_obj = null;
Sqlconnection con = new SqlConnection("Your connection string");
con.open();
SqlCommand cmd = new SqlCommand(con);
cmd.commandtext = "select emp_id,emp_name from employee_master where status = 'AA'";
SqlDataReader dr = cmd.ExecuteReader();
chk_lst_emp.Items.Clear();
While(dr.read())
{
try
{
if(!IsDBNull(dr("emp_name"))
{
emp_obj = new Employee_Master();
emp_obj.Employee_Id = convert.tostring(dr("emp_id"));
emp_obj.Employee_Name = convert.tostring(dr("emp_name"))
lst_employee.Add(emp_obj);
}
}
catch(exception ex)
{
throw ex;
}
finally
{
if(dr!=null) dr.close();
if(con!=null) con.close();
}
}
}
//after while loop close
if(lst_employee.Count>0)
{
chk_lst_emp.DataSource = lst_employee;
chk_lst_emp.DataTextField = "emp_name";
chk_lst_emp.DataValueField = "emp_id";
chk_lst_emp.DataBind();
}
else
{
chk_lst_emp.DataSource = null;
chk_lst_emp.DataBind();
}
}
//You can also fill you checkbox list with distionary in that case you need to give
DataTextField = "key" and DataValueField = "value"
I think it helps you lot.
Shail12345, if this helps please login to Mark As Answer. | Alert Moderator