In this article I’ll explain how to Save and retrieve ASP.Net CheckBoxList items into SQL Server Database. So let’s start with article.
Database
I have made use of the following table Hobbies with the schema as follows.
I have already inserted few records in the table
Note: You can download the database table SQL by clicking the download link below.
Connection String
Once the database is ready you can create the connection string that will be used to connect to the database.
<add name="constr" connectionString="Data Source = .\SQLExpress;
Initial Catalog = dbHobbies; Integrated Security = true"/>
HTML Markup
Below is the HTML Markup of the ASP.net web page.
Hobbies:
<asp:CheckBoxList ID="chkHobbies" runat="server">
</asp:CheckBoxList>
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Button" OnClick = "UpdateHobbies" />
As you can see I have added a CheckBoxList and a Button that will allow the user to update the selections in the database.
The screenshot below describes how the User Interface looks
Populating the CheckBoxList from Database
The following method is used to populate the Hobbies CheckBoxList from the SQL Server Database
C#
private void PopulateHobbies()
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from hobbies";
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
ListItem item = new ListItem();
item.Text = sdr["Hobby"].ToString();
item.Value = sdr["HobbyId"].ToString();
item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
chkHobbies.Items.Add(item);
}
}
conn.Close();
}
}
}
VB.Net
Private Sub PopulateHobbies()
Using conn As New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("constr").ConnectionString()
Using cmd As New SqlCommand()
cmd.CommandText = "select * from hobbies"
cmd.Connection = conn
conn.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Dim item As New ListItem()
item.Text = sdr("Hobby").ToString()
item.Value = sdr("HobbyId").ToString()
item.Selected = Convert.ToBoolean(sdr("IsSelected"))
chkHobbies.Items.Add(item)
End While
End Using
conn.Close()
End Using
End Using
End Sub
The above method is called up in the page load event in the following way
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.PopulateHobbies();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.PopulateHobbies()
End If
End Sub
Saving the Selections in Database
The following method is called up on the Submit Button Click event and is used to save the user selections to the database
C#
protected void UpdateHobbies(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "update hobbies set IsSelected = @IsSelected" +
" where HobbyId=@HobbyId";
cmd.Connection = conn;
conn.Open();
foreach (ListItem item in chkHobbies.Items)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@IsSelected", item.Selected);
cmd.Parameters.AddWithValue("@HobbyId", item.Value);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
VB.Net
Protected Sub UpdateHobbies(ByVal sender As Object, ByVal e As EventArgs)
Using conn As New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using cmd As New SqlCommand()
cmd.CommandText = "update hobbies set IsSelected = " & _
"@IsSelected where HobbyId=@HobbyId"
cmd.Connection = conn
conn.Open()
For Each item As ListItem In chkHobbies.Items
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@IsSelected", item.Selected)
cmd.Parameters.AddWithValue("@HobbyId", item.Value)
cmd.ExecuteNonQuery()
Next
conn.Close()
End Using
End Using
End Sub
Thus this way you can use CheckBoxList to populate and save the user settings or selections in database. You can download the related code in VB.Net and C# using the download link below