Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

    How to save Check Box value/Radio Button Value into SQL Server Database using C#

    Front-End Development
    sql sql-server programming c sharp
    3
    15
    3246
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • nowferrifkan
      nowferrifkan last edited by nowferrifkan

      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.

      1 Reply Last reply Reply Quote 0
      • lahirunc
        lahirunc last edited by

        You can use checkbox.checked and radiobutton.checked straight away without going through this hassle.

        nowferrifkan 1 Reply Last reply Reply Quote 1
        • nowferrifkan
          nowferrifkan @lahirunc last edited by

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

          lahirunc 1 Reply Last reply Reply Quote 0
          • lahirunc
            lahirunc @nowferrifkan last edited by lahirunc

            @nowferrifkan

            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.

            1 Reply Last reply Reply Quote 0
            • nowferrifkan
              nowferrifkan last edited by

              Did you read the headline of this article?. And real the answers well dude.

              lahirunc 1 Reply Last reply Reply Quote 0
              • lahirunc
                lahirunc @nowferrifkan last edited by lahirunc

                @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

                1 Reply Last reply Reply Quote 0
                • tnlthanzeel
                  tnlthanzeel Web Development last edited by tnlthanzeel

                  @nowferrifkan , why use string to save the value of checkbox/radio button in the database?

                  lahirunc 1 Reply Last reply Reply Quote 0
                  • lahirunc
                    lahirunc @tnlthanzeel last edited by

                    @tnlthanzeel exactly my thought mate!

                    1 Reply Last reply Reply Quote 0
                    • tnlthanzeel
                      tnlthanzeel Web Development last edited by

                      @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 ?
                      
                      lahirunc 1 Reply Last reply Reply Quote 0
                      • lahirunc
                        lahirunc @tnlthanzeel last edited by

                        @tnlthanzeel well it's just for the better understanding. @CheckBoxChecked is ParamNameHere and valuehere is chkbox.IsChecked value.

                        1 Reply Last reply Reply Quote 0
                        • tnlthanzeel
                          tnlthanzeel Web Development last edited by

                          @lahirunc ,whats the best method brother?

                          lahirunc 1 Reply Last reply Reply Quote 0
                          • lahirunc
                            lahirunc @tnlthanzeel last edited by lahirunc

                            @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();
                            
                            1 Reply Last reply Reply Quote 0
                            • tnlthanzeel
                              tnlthanzeel Web Development last edited by

                              @lahirunc , okay bro, but even in ur code there are 2 paramaters, cant we do it with one paramater?

                              lahirunc 1 Reply Last reply Reply Quote 0
                              • lahirunc
                                lahirunc @tnlthanzeel last edited by

                                @tnlthanzeel You can do it by only using this command.Parameters.AddWithValue("@CheckBoxChecked", chkbox.IsChecked);

                                Other one was for knowledge purposes.

                                1 Reply Last reply Reply Quote 0
                                • tnlthanzeel
                                  tnlthanzeel Web Development last edited by

                                  @lahirunc , okay bro, thanks alot

                                  1 Reply Last reply Reply Quote 0
                                  • 1 / 1
                                  • First post
                                    Last post

                                  2
                                  Online

                                  3.6k
                                  Users

                                  1.3k
                                  Topics

                                  5.3k
                                  Posts

                                  • Privacy
                                  • Terms & Conditions
                                  • Donate

                                  © Copyrights and All right reserved Lanka Developers Community

                                  Powered by Axis Technologies (PVT) Ltd

                                  Made with in Sri Lanka

                                  | |