How to save Check Box value/Radio Button Value into SQL Server Database using C#
-
string Isactive = chkBoxActive.Checked ? "TRUE" : "FALSE";
// Isactive is a variable which store value of the checkbox. If the check box is checked the variable value will be TRUE if not the variable Value will be FALSE - Call the variable name to save it into the database.
string radioButton1Value = radioButton1.Checked ? "TRUE" : "FALSE";
// radioButton1Value is a variable which store value of the radioButton. If the radioButton1 is ticked , the variable value will be TRUE if not the variable Value will be FALSE - Call the variable name to save it into the database.
-
You can use checkbox.checked and radiobutton.checked straight away without going through this hassle.
-
@lahirunc Post the sample code. I targeted the Beginners. So they can understand about the variable and these controls at once. Please Post your code brother. so readers get something from it.
-
if (checkBox.IsChecked) { MessageBox.Show("CheckBox ticked"); } else { MessageBox.Show("CheckBox unticked"); }
So here if the checkbox has been checked then it will display Checkbox ticked else checkbox unticked. You don't have to go round and round using tertiary ifs and another variable to get its bool value.
-
Did you read the headline of this article?. And real the answers well dude.
-
@nowferrifkan Dude by using
chkbox.IsChecked
property you get the same thing as you do by your code. try and see. I just posted a code segment just for understanding purpose.using (SqlConnection connection = new SqlConnection("ConnectionStringHere")) { using (SqlCommand command = new SqlCommand()) { command.Connection = connection; // <== lacking command.CommandType = CommandType.Text; command.CommandText = "SQL Statment here (insert or update)"; command.Parameters.AddWithValue("@ParamNameHere", valuehere); command.Parameters.AddWithValue("@CheckBoxChecked", chkbox.IsChecked); try { connection.Open(); command.ExecuteNonQuery(); } catch(SqlException) { //error here } finally { connection.Close(); } } }
above is the correct code to save the data to db
-
@nowferrifkan , why use string to save the value of checkbox/radio button in the database?
-
@tnlthanzeel exactly my thought mate!
-
@lahirunc , bro, why have u used two parameters to save the value of the checkbos?
command.Parameters.AddWithValue("@ParamNameHere", valuehere); command.Parameters.AddWithValue("@CheckBoxChecked", chkbox.IsChecked); @ParamNameHere and @CheckBoxChecked ?
-
@tnlthanzeel well it's just for the better understanding.
@CheckBoxChecked
isParamNameHere
andvaluehere
ischkbox.IsChecked
value. -
@lahirunc ,whats the best method brother?
-
@tnlthanzeel well there's nothing wrong with @nowferrifkan nor mine. But if you ask the most efficient way?
Then I would suggest mine as there's no point in assigning a value which is already boolean to string using tertiary if. That just produce messy code. You can convert using
.toString()
eg:-
chkbox.IsChecked.toString();
-
@lahirunc , okay bro, but even in ur code there are 2 paramaters, cant we do it with one paramater?
-
@tnlthanzeel You can do it by only using this
command.Parameters.AddWithValue("@CheckBoxChecked", chkbox.IsChecked);
Other one was for knowledge purposes.
-
@lahirunc , okay bro, thanks alot