Insert CheckBox Value into Database in Windows Form Using C#

Here Laxmikant has explained How to Insert CheckBox Value into Database in Windows Form Using C#.
In this blog, we will learn step by step Insert CheckBox Value into Database in Windows Form Using C#.
For this blog, I have created a Database and Simple Table with the two column “HobbiesId “, “HobbiesName “.

create database CheckBox_Demo
use CheckBox_Demo

create table tblHobbies
hobbiesId int primary key identity(1,1),
hobbiesName varchar(40)

Create Stored procedure for Insert Record into Database.

create proc sp_tblHobbies_insert
@hobbiesName varchar(40)
insert into tblHobbies(hobbiesName) values(@hobbiesName)

Now Open Visual Studion and create the project as:
“Start” –> “All Programs”- “Microsoft Visual Studio 2012”.
“File” – “New Project” – ” Select Visual C# Windows from left template” – “Windows Form Application” (to avoid adding a master page).

Give the Project name such as “CheckBox_Demo” or another as you wish and specify the location.

In “Form1.cs” page drag and drop one CheckBox control and one Button.

After that add item in CheckBox Control.Single Click on CheckBox Control–>”Edit Items”–>After that Add item then click “Ok” Button.Looks like screenshot.

Open App.config file and Establish a connection with database

Key Point:

  • In initial catalog write Database Name
  • In data source you can simply write (.) /your server name/(local)
  • Integrated security for Authentication
  <add name="DBCS" connectionString="data source=.;initial catalog=CheckBox_Demo;integrated security=true"/>

Open “Form1.cs” page to write code for Insert CheckBox Value into Database in Windows Form Using C#.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//Need to import the follwing Namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace CheckBox_Demo
    public partial class Form1 : Form
       //Establish connection with Database
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
        public Form1()
        //insert code write here on button click
        //Here take one string variable name "HOB".
        //Store select value of checkbox in "HOB" variable
       //"chkhobbies" is the Name of CheckBox
        private void btnSave_Click(object sender, EventArgs e)
            string hobbies = "";
            foreach (var checkedItem in this.chkhobbies.CheckedItems)
                hobbies += checkedItem.ToString()+",";
            hobbies = hobbies .TrimEnd(',');
            SqlCommand cmd = new SqlCommand("sp_tblHobbies_insert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@hobbiesName", hobbies );
            MessageBox.Show("Data Insert successfully");

screenshot describe the output.

Any suggestion and question-related to this article please comment me. Have a nice Day.Thank You

Leave a Reply

Your email address will not be published. Required fields are marked *