Now in this article I am going to explain how to dynamically Bind/Load/Fill CheckBoxList with the data from the Sql Server table. It is one of very common requirement while working on asp.net application.
- In the design page(.aspx) place a DropDownList and a CheckBoxList control as:
- Create a connectionstring in the web.config file under configuration tag as:
<connectionStrings>
<add name="MyDbCon" connectionString="Data Source=LocalServer;Initial Catalog=MyDataBase;Integrated
Security=True"/>
</connectionStrings>
</configuration>
Note: Replace the Data Source and Initial Catalog(i.e. Database name) as per your application.
Note: Replace the Data Source and Initial Catalog(i.e. Database name) as per your application.
- Create a Database e.g. "MyDataBase" in sql server and also create a table as shown below and name it "Qualification_Tb"
- Now in the code behind file(.aspx.cs) write the code as
Asp.Net C# Code to Bind/Load/Fill CheckBoxList from Sql server Database in asp.net
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
protected void Page_Load(object
sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillQualCheckBoxList();
}
}
private void
FillQualCheckBoxList()
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["MyDbCon"].ConnectionString);
SqlCommand cmd = new
SqlCommand("Select
* from Qualification_Tb", con);
SqlDataAdapter adp = new
SqlDataAdapter(cmd);
DataTable dt = new
DataTable();
adp.Fill(dt);
cblCourses.DataSource = dt;
cblCourses.DataTextField = "Qualification";
cblCourses.DataValueField = "Qualification_Id_Pk";
cblCourses.DataBind();
}
Asp.Net VB Code to Bind/Load/Fill CheckBoxList from Sql server Database in asp.net
- Now in the code behind file(.aspx.vb) write the code as:
Imports System.Data.SqlClient
Imports System.Configuration
Protected Sub
Page_Load(ByVal sender As
Object, ByVal e
As System.EventArgs) Handles
Me.Load
If Not
Page.IsPostBack Then
FillQualCheckBoxList()
End If
End Sub
Private Sub
FillQualCheckBoxList()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("MyDbCon").ConnectionString)
Dim cmd As New SqlCommand("Select
* from Qualification_Tb", con)
Dim adp As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
adp.Fill(dt)
cblCourses.DataSource = dt
cblCourses.DataTextField = "Qualification"
cblCourses.DataValueField = "Qualification_Id_Pk"
cblCourses.DataBind()
End Sub
Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."
2 comments
Click here for commentsThere is no need to write any code-behind for this as this would do exactly the same:
Reply<asp:CheckBoxList ID="cblCourses" runat="server" RepeatColumns="2" DataSourceID="sdsCourses" DataValueField="Qualification_Id_Pk" DataTextField="Qualification" />
<asp:SqlDataSource ID="sdsCourses" runat="server" ConnectionString="<%$ ConnectionStrings:MyDbCon %>" SelectCommand="SELECT * FROM Qualification_Tb;" />
Hello andrec..yes you are right..that's also a way to fill the checkbox using SqldataSource..thanks for sharing your knowledge too..keep reading. :)
ReplyIf you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..