Click here to Skip to main content
11,806,590 members (64,930 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET SQL C#3.5
Hi frnds,

on my webpage i have a checkbox, when it is checked in database it should insert 1 else it should be 0

In database i have field name Status.

If checkbox checked it must save as 1


it should be save as 0

Please help.

Posted 23-Aug-14 23:47pm
Abhinav S at 24-Aug-14 5:56am
What are you having a problem with? Do you need help converting checkbox value to 0 or 1?
Member239258 at 24-Aug-14 5:58am
ya, I need help.
already i took datatype as int.

If checkbox selected Insert value as 1 in database
insert value as 0

Please help. my friends.
Abhishek Pant at 24-Aug-14 7:29am
no problem just pass value as 1/0 to db when checkbox is checked. first of all find checkbox is checked or not ans then pass its value..

int value;
if (DraftCheckBox.Checked){

//pass value to db here

ChauhanAjay at 24-Aug-14 6:51am
Kindly follow the steps below to achieve your requirement

1. Take a variable to store the value of 0 or 1 and this would be used for insertion
2. If checkbox is selected then set the value of the variable to 1 else 0
3. Now in the insert statement use this variable to store the value of status.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If the table column type is INT and not BIT. It is easy to save the value to your database.

var sqlConnection = new SqlConnection(Settings.SqlConnectionString);
var sqlCommand = sqlConnecteion.CreateCommand();
if (checkBox1.Checked)
  sqlCommand.CommandText = "INSERT INTO tbl (_checkBoxValue) VALUES(1)";
  sqlCommand.CommandText = "INSERT INTO tbl (_checkBoxValue) VALUES(0)";


The code can be shorted to about 5 lines of code.
Abhishek Pant at 24-Aug-14 7:44am
I think its checkBox1.Checked in place of checkBox1.IsChecked
see check box property here[^]
Kim Togo at 24-Aug-14 7:49am
Thanks :-)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

try like this:

String str = "";
for (int i = 0; i <=CheckBoxList1.Items.Count-1; i++)
if (CheckBoxList1.Items[i].Selected)
 if (str == "")
str = CheckBoxList1.Items[i].Text;
str += "," + CheckBoxList1.Items[i].Text;

it will create a string str with comma separated checkbox values.
then insert the string str to your database column.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 ppolymorphe 205
1 OriginalGriff 175
2 Richard Deeming 175
3 Sergey Alexandrovich Kryukov 170
4 Abhinav S 120
0 OriginalGriff 3,380
1 Maciej Los 1,990
2 CPallini 1,915
3 KrunalRohit 1,907
4 ppolymorphe 1,317

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 3 Jan 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100