Click here to Skip to main content
15,851,242 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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

else

it should be save as 0

Please help.


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

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

Please help. my friends.
Abhishek Pant 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){
value=1;

}
else{
value=0;
}
//pass value to db here

ChauhanAjay 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.

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

Example:
C#
var sqlConnection = new SqlConnection(Settings.SqlConnectionString);
sqlConnection.Open();

var sqlCommand = sqlConnecteion.CreateCommand();

if (checkBox1.Checked)
{
  sqlCommand.CommandText = "INSERT INTO tbl (_checkBoxValue) VALUES(1)";
}
else
{
  sqlCommand.CommandText = "INSERT INTO tbl (_checkBoxValue) VALUES(0)";
}


sqlCommand.ExecuteNonQuery();


The code can be shorted to about 5 lines of code.
 
Share this answer
 
v2
Comments
Abhishek Pant 24-Aug-14 7:44am    
I think its checkBox1.Checked in place of checkBox1.IsChecked
see check box property here[^]
Kim Togo 24-Aug-14 7:49am    
Thanks :-)
hi,
try like this:

C#
String str = "";
for (int i = 0; i <=CheckBoxList1.Items.Count-1; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
 if (str == "")
{
str = CheckBoxList1.Items[i].Text;
}
 else
{
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900