Click here to Skip to main content
12,075,542 members (67,139 online)
Rate this:
 
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

else

it should be save as 0

Please help.


Thanks.
Posted 24-Aug-14 0:47am
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.
Rate this: bad
 
good
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.

Example:
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.
  Permalink  
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 :-)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

hi,
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;
}
 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.
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160208.1 | Last Updated 3 Jan 2015
Copyright © CodeProject, 1999-2016
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