Sample Dynamic Checkboxlist connect with SQL Server 2008 in asp.net using c#
Let me show you how to create dynamic Checkboxlist in ASP.Net using C#. To do this , I need to binding data into checkboxlist control on page load.My database design in MS SQL Server 2008 , and I want to load data from column "country" in a table , tblcountry, into my checkboxlist when page is loading.
In SQL Server 2008:
In SQL Server 2008:
Sample table:
Configure 1 : sample table tblcountry
In ASP.Net:
Code Design :
<asp:CheckBoxList ID="CheckBoxList1" runat="server"></asp:CheckBoxList>
Code behind :
protectedvoidPage_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
DataTable dt = new DataTable();
dt = Loadlist();
CheckBoxList1.DataSource = dt;
CheckBoxList1.DataValueField = "country";
//country is table field name in tblcountry
//country is table field name in tblcountry
CheckBoxList1.DataBind();
}
}
public DataTable Loadlist()
{
SqlConnection con = new SqlConnection();
string str;
string sql;
str="Server=UBEE_THAROTH;Database=Drink;uid=sa;pwd=123;";
sql = "Select * from tblcountry order by country asc";
con.ConnectionString = str;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(sql,con);
DataTabledt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count>0){
return dt;
}
else {
return null;
}
}
No comments:
Post a Comment