In this article I will explain with an example, how to insert multiple values from CheckBoxList to
SQL Server database in ASP.Net using C# and VB.Net.
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.
HTML Markup
The following HTML Markup consists of:
CheckBoxList – For displaying data.
Button – For updating records in the
SQL Server database.
The Button has been assigned with an OnClick event handler.
Hobbies:
<asp:CheckBoxList ID="chkHobbies" runat="server"></asp:CheckBoxList>
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="UpdateHobbies" />
Populating the CheckBoxList from Database
Inside the Page Load event handler, the PopulateHobbies method is called.
PopulateHobbies
Inside the
PopulateHobbies method, the records are fetched from the
SQL Server database table using
ExecuteReader function.
The Checked and Unchecked state of CheckBoxList will be decided based on
IsSelected column value fetched from the
SQL Server database.
If fetched value of IsSelected column is 1 then the CheckBox will be checked and if it is 0 then the CheckBox will be set as unchecked.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateHobbies();
}
}
private void PopulateHobbies()
{
string query = "SELECT HobbyId, Hobby, IsSelected FROM Hobbies";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.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);
}
}
con.Close();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.PopulateHobbies()
End If
End Sub
Private Sub PopulateHobbies()
Dim query As String = "SELECT HobbyId, Hobby, IsSelected FROM Hobbies"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = NewSqlCommand(query, con)
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Dim item As ListItem = 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
con.Close()
End Using
End Using
End Sub
Updating records based on the Selections into the SQL Server Database
When the
Update button is clicked, a FOR EACH loop will be executed over the CheckBoxList items and the records are updated into the
SQL Server database table based on the state of CheckBoxes in CheckBoxList using
ExecuteNonQuery function.
C#
protected void UpdateHobbies(object sender, EventArgs e)
{
string query = "UPDATE Hobbies SET IsSelected = @IsSelected WHERE HobbyId = @HobbyId";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
foreach (ListItem item in chkHobbies.Items)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@IsSelected", item.Selected);
cmd.Parameters.AddWithValue("@HobbyId", item.Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
VB.Net
Protected Sub UpdateHobbies(ByVal sender As Object, ByVal e As EventArgs )
Dim query As String = "UPDATE Hobbies SET IsSelected = @IsSelected WHERE HobbyId = @HobbyId"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
For Each item As ListItem In chkHobbies.Items
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@IsSelected", item.Selected)
cmd.Parameters.AddWithValue("@HobbyId", item.Value)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Next
End Using
End Using
End Sub
Screenshots
The Form
Records after Updating
Demo
Downloads