Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Retrieving checkbox value from db

Retrieving checkbox value from db

Scheduled Pinned Locked Moved ASP.NET
databasehelpquestion
19 Posts 5 Posters 0 Views 1 Watching
  • 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.
  • T test 09

    using that code im able to insert the check box value but if i check multiple check box im able to insert only one value to db how ineed to insert for multiple values.. and how do i retrieve that values from db i.e, the check boxes should be checked when i get the values from db.. please help me...

    foreach (DataListItem dli in DataList1.Items)
    {
    CheckBox Chk = (CheckBox)dli.FindControl("CheckBox1");
    if (Chk.Checked)
    {
    SqlConnection SqlCnn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
    // SqlDataReader dr;
    SqlCommand SqlCmd = new SqlCommand();
    SqlCmd.Connection = SqlCnn;
    SqlCnn.Open();
    SqlCmd.CommandText = "insert into test(test1) values('"+ck.Text+"')";
    SqlCmd.ExecuteNonQuery();
    SqlCnn.Close();
    break;
    }
    else
    {
    str13 = "you need to check";
    }
    }
    LblErr.Visible = true;
    LblErr.Text = str13;

    D Offline
    D Offline
    Dinesh Mani
    wrote on last edited by
    #2

    Bigger picture here would help. What exactly do you want to do? Do you wish to save the text for all the check boxes that have been checked? If yes then your code here is not doing it right. Your code is static in nature

                SqlCmd.CommandText = "insert into test(test1) values('"+ck.Text+"')";
                SqlCmd.ExecuteNonQuery();
    

    i.e. every time you get a positive, the query take the text value of the check box and saves it in the db against the same table column. So, what happens is the first positives text get over written by next which again get over written by the next and goes on until the last positive which gets saved ultimately. If you wish to save all the texts to the db, then you have got to concatenate all the text values in one string, inside the foreach loop and use it in the query and execute it outside the loop. HTH!

    T 1 Reply Last reply
    0
    • D Dinesh Mani

      Bigger picture here would help. What exactly do you want to do? Do you wish to save the text for all the check boxes that have been checked? If yes then your code here is not doing it right. Your code is static in nature

                  SqlCmd.CommandText = "insert into test(test1) values('"+ck.Text+"')";
                  SqlCmd.ExecuteNonQuery();
      

      i.e. every time you get a positive, the query take the text value of the check box and saves it in the db against the same table column. So, what happens is the first positives text get over written by next which again get over written by the next and goes on until the last positive which gets saved ultimately. If you wish to save all the texts to the db, then you have got to concatenate all the text values in one string, inside the foreach loop and use it in the query and execute it outside the loop. HTH!

      T Offline
      T Offline
      test 09
      wrote on last edited by
      #3

      i want to save text for every check box that is checked and how to i retrieve those values.. the checkboxs should be checked when i get values from db... thank you..

      Dinesh Mani wrote:

      you have got to concatenate

      and how do i concatenate those text values

      D 1 Reply Last reply
      0
      • T test 09

        i want to save text for every check box that is checked and how to i retrieve those values.. the checkboxs should be checked when i get values from db... thank you..

        Dinesh Mani wrote:

        you have got to concatenate

        and how do i concatenate those text values

        D Offline
        D Offline
        Dinesh Mani
        wrote on last edited by
        #4

        First things first, tell us how exactly are you creating your check boxes? I mean the what is the source for your checkbox's text?

        T 1 Reply Last reply
        0
        • T test 09

          using that code im able to insert the check box value but if i check multiple check box im able to insert only one value to db how ineed to insert for multiple values.. and how do i retrieve that values from db i.e, the check boxes should be checked when i get the values from db.. please help me...

          foreach (DataListItem dli in DataList1.Items)
          {
          CheckBox Chk = (CheckBox)dli.FindControl("CheckBox1");
          if (Chk.Checked)
          {
          SqlConnection SqlCnn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
          // SqlDataReader dr;
          SqlCommand SqlCmd = new SqlCommand();
          SqlCmd.Connection = SqlCnn;
          SqlCnn.Open();
          SqlCmd.CommandText = "insert into test(test1) values('"+ck.Text+"')";
          SqlCmd.ExecuteNonQuery();
          SqlCnn.Close();
          break;
          }
          else
          {
          str13 = "you need to check";
          }
          }
          LblErr.Visible = true;
          LblErr.Text = str13;

          A Offline
          A Offline
          Anurag Gandhi
          wrote on last edited by
          #5

          The break; statement will move you out of the loop once First data is inserted. Just for your knowledge, google it and you will find what for break is used. So, remove the break statement from if block then all your checked records will be inserted to the database.

          Anurag Gandhi.
          http://www.gandhisoft.com
          Life is a computer program and every one is the programmer of his own life.

          T 1 Reply Last reply
          0
          • D Dinesh Mani

            First things first, tell us how exactly are you creating your check boxes? I mean the what is the source for your checkbox's text?

            T Offline
            T Offline
            test 09
            wrote on last edited by
            #6

            source is a column from table 'test'

            D 1 Reply Last reply
            0
            • A Anurag Gandhi

              The break; statement will move you out of the loop once First data is inserted. Just for your knowledge, google it and you will find what for break is used. So, remove the break statement from if block then all your checked records will be inserted to the database.

              Anurag Gandhi.
              http://www.gandhisoft.com
              Life is a computer program and every one is the programmer of his own life.

              T Offline
              T Offline
              test 09
              wrote on last edited by
              #7

              i removed break but still im able to insert only last checked value..

              1 Reply Last reply
              0
              • T test 09

                using that code im able to insert the check box value but if i check multiple check box im able to insert only one value to db how ineed to insert for multiple values.. and how do i retrieve that values from db i.e, the check boxes should be checked when i get the values from db.. please help me...

                foreach (DataListItem dli in DataList1.Items)
                {
                CheckBox Chk = (CheckBox)dli.FindControl("CheckBox1");
                if (Chk.Checked)
                {
                SqlConnection SqlCnn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
                // SqlDataReader dr;
                SqlCommand SqlCmd = new SqlCommand();
                SqlCmd.Connection = SqlCnn;
                SqlCnn.Open();
                SqlCmd.CommandText = "insert into test(test1) values('"+ck.Text+"')";
                SqlCmd.ExecuteNonQuery();
                SqlCnn.Close();
                break;
                }
                else
                {
                str13 = "you need to check";
                }
                }
                LblErr.Visible = true;
                LblErr.Text = str13;

                B Offline
                B Offline
                Brij
                wrote on last edited by
                #8

                What is the use break in if block?

                Cheers!! Brij

                T 1 Reply Last reply
                0
                • B Brij

                  What is the use break in if block?

                  Cheers!! Brij

                  T Offline
                  T Offline
                  test 09
                  wrote on last edited by
                  #9

                  initially i used a label to check if break is not used i need to check all check boxes for label to true and viceversa

                  B 1 Reply Last reply
                  0
                  • T test 09

                    initially i used a label to check if break is not used i need to check all check boxes for label to true and viceversa

                    B Offline
                    B Offline
                    Brij
                    wrote on last edited by
                    #10

                    See your variable str13 is not associated to the every item/checkbox in datalist so it will alyas be the latest. Regarding the checkbox, try to debug the the code whether you arre able to access all the checkbox in the datalist and their associated values.If you are able to get the checkbox's value specifically then why they are not getting inserted. One reason I can think of your row might be getting updated everytime so thats you are getting the latest one.Verify it. Also

                    test-09 wrote:

                    "insert into test(test1) values('"+ck.Text+"')";

                    you checkbox is chk not ck. :)

                    Cheers!! Brij

                    1 Reply Last reply
                    0
                    • T test 09

                      source is a column from table 'test'

                      D Offline
                      D Offline
                      Dinesh Mani
                      wrote on last edited by
                      #11

                      I don't want to see your code buddy. I want the business case.

                      T 1 Reply Last reply
                      0
                      • D Dinesh Mani

                        I don't want to see your code buddy. I want the business case.

                        T Offline
                        T Offline
                        test 09
                        wrote on last edited by
                        #12

                        im having around 30 checkboxes in page i used datalist to display. when check boxs checked i need to insert their names into one column(seperated by comma or space) then when i get those values from db check boxs need to be checked(edit mode)...

                        D 1 Reply Last reply
                        0
                        • T test 09

                          im having around 30 checkboxes in page i used datalist to display. when check boxs checked i need to insert their names into one column(seperated by comma or space) then when i get those values from db check boxs need to be checked(edit mode)...

                          D Offline
                          D Offline
                          Dinesh Mani
                          wrote on last edited by
                          #13

                          What is the condition that dictates that you need 30 check boxes? Do you create the checkboxes at runtime or are they available at design time itself? I suppose you create them at runtime. If so, from where do you get their names i.e. the text for each checkbox? Are these text values unique?

                          T 1 Reply Last reply
                          0
                          • D Dinesh Mani

                            What is the condition that dictates that you need 30 check boxes? Do you create the checkboxes at runtime or are they available at design time itself? I suppose you create them at runtime. If so, from where do you get their names i.e. the text for each checkbox? Are these text values unique?

                            T Offline
                            T Offline
                            test 09
                            wrote on last edited by
                            #14

                            design time

                            Dinesh Mani wrote:

                            If so, from where do you get their names i.e.

                            text of check box is a column from db using datalist.. all values are unique..

                            D 1 Reply Last reply
                            0
                            • T test 09

                              design time

                              Dinesh Mani wrote:

                              If so, from where do you get their names i.e.

                              text of check box is a column from db using datalist.. all values are unique..

                              D Offline
                              D Offline
                              Dinesh Mani
                              wrote on last edited by
                              #15

                              If you already have the text in the table, then why do you want to save it again the db? I understand that you need to save the state but why save the text?? What is the relevance of these checkboxes to the rest of the screen? i.e. is the state common across users or each user gets his/her own set of checkboxes? When you save the state to the DB what is the reference that you would be using to retrive it??

                              T 1 Reply Last reply
                              0
                              • D Dinesh Mani

                                If you already have the text in the table, then why do you want to save it again the db? I understand that you need to save the state but why save the text?? What is the relevance of these checkboxes to the rest of the screen? i.e. is the state common across users or each user gets his/her own set of checkboxes? When you save the state to the DB what is the reference that you would be using to retrive it??

                                T Offline
                                T Offline
                                test 09
                                wrote on last edited by
                                #16

                                all users is having the same textboxes..

                                Dinesh Mani wrote:

                                why do you want to save it again the db?

                                Im saving it to a different table so i used text to insert into new table..

                                Response.Write(ck.Text + "<br/>");

                                with this i can get multiple checked text values but how do i insert into db. saving state or text whatever it may be i need to retrive them from db but my sir wants text..

                                D R 2 Replies Last reply
                                0
                                • T test 09

                                  all users is having the same textboxes..

                                  Dinesh Mani wrote:

                                  why do you want to save it again the db?

                                  Im saving it to a different table so i used text to insert into new table..

                                  Response.Write(ck.Text + "<br/>");

                                  with this i can get multiple checked text values but how do i insert into db. saving state or text whatever it may be i need to retrive them from db but my sir wants text..

                                  D Offline
                                  D Offline
                                  Dinesh Mani
                                  wrote on last edited by
                                  #17

                                  Ok I'm not getting why you need to do it this way so here you go. pseudo -

                                  Declare string CheckedBoxesText --- Use a stringbuilder
                                  For each check box in datalist
                                  if Checkbox is checked
                                  Add concatenate checkbox text to CheckedBoxesText
                                  else
                                  do nothing
                                  End of Foreach Loop
                                  Query = "insert into test(test1) values('"CheckedBoxesText"')";
                                  Execute query.

                                  Implementing this pseudo would enable you to save the "text" of your check boxes to the db.

                                  1 Reply Last reply
                                  0
                                  • T test 09

                                    all users is having the same textboxes..

                                    Dinesh Mani wrote:

                                    why do you want to save it again the db?

                                    Im saving it to a different table so i used text to insert into new table..

                                    Response.Write(ck.Text + "<br/>");

                                    with this i can get multiple checked text values but how do i insert into db. saving state or text whatever it may be i need to retrive them from db but my sir wants text..

                                    R Offline
                                    R Offline
                                    Ravindra Nidhonkar
                                    wrote on last edited by
                                    #18

                                    Remove the break; statement from the For each loop.

                                    1 Reply Last reply
                                    0
                                    • T test 09

                                      using that code im able to insert the check box value but if i check multiple check box im able to insert only one value to db how ineed to insert for multiple values.. and how do i retrieve that values from db i.e, the check boxes should be checked when i get the values from db.. please help me...

                                      foreach (DataListItem dli in DataList1.Items)
                                      {
                                      CheckBox Chk = (CheckBox)dli.FindControl("CheckBox1");
                                      if (Chk.Checked)
                                      {
                                      SqlConnection SqlCnn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
                                      // SqlDataReader dr;
                                      SqlCommand SqlCmd = new SqlCommand();
                                      SqlCmd.Connection = SqlCnn;
                                      SqlCnn.Open();
                                      SqlCmd.CommandText = "insert into test(test1) values('"+ck.Text+"')";
                                      SqlCmd.ExecuteNonQuery();
                                      SqlCnn.Close();
                                      break;
                                      }
                                      else
                                      {
                                      str13 = "you need to check";
                                      }
                                      }
                                      LblErr.Visible = true;
                                      LblErr.Text = str13;

                                      T Offline
                                      T Offline
                                      test 09
                                      wrote on last edited by
                                      #19

                                      Thank you bros.. you people guide me well I got exactly what i want...:thumbsup:

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups