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. How to add all value from checkbox list

How to add all value from checkbox list

Scheduled Pinned Locked Moved ASP.NET
databasetutorial
4 Posts 2 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.
  • L Offline
    L Offline
    LovelyHelp
    wrote on last edited by
    #1

    How am i to insert all my selected items from checkbox list to my database. Right now i only able to insert 1 item to my database even I have select 2 items from my checkboxlist. my code is as below. mycommand2 = New SqlCommand("INSERT INTO t_userpackage (userID, packageID) VALUES (@userid, @packageid)", myconnection) mycommand2.Parameters.Add("@userid", SqlDbType.Int, 4).Value = ddluser.SelectedValue mycommand2.Parameters.Add("@packageid", SqlDbType.Int, 4).Value = chkpackage.SelectedItem.Value mycommand2.ExecuteNonQuery()

    K 1 Reply Last reply
    0
    • L LovelyHelp

      How am i to insert all my selected items from checkbox list to my database. Right now i only able to insert 1 item to my database even I have select 2 items from my checkboxlist. my code is as below. mycommand2 = New SqlCommand("INSERT INTO t_userpackage (userID, packageID) VALUES (@userid, @packageid)", myconnection) mycommand2.Parameters.Add("@userid", SqlDbType.Int, 4).Value = ddluser.SelectedValue mycommand2.Parameters.Add("@packageid", SqlDbType.Int, 4).Value = chkpackage.SelectedItem.Value mycommand2.ExecuteNonQuery()

      K Offline
      K Offline
      KrIstOfK
      wrote on last edited by
      #2

      You only have one item because you only ask one item, loop through your checkboxes with some kind of loop. (syntax will not be correct but you'll get the meaning i hope)

      foreach(CheckBox chxChecked in chkpackage.SelectedItems){
      // save here to the database this one item
      }

      L 1 Reply Last reply
      0
      • K KrIstOfK

        You only have one item because you only ask one item, loop through your checkboxes with some kind of loop. (syntax will not be correct but you'll get the meaning i hope)

        foreach(CheckBox chxChecked in chkpackage.SelectedItems){
        // save here to the database this one item
        }

        L Offline
        L Offline
        LovelyHelp
        wrote on last edited by
        #3

        I trying to loop too but i failed. What is the syntax for looping. BElow is what i done but it is wrong. Dim i As Integer For i = 0 To chkpackage.SelectedItem.Count - 1 mycommand2.Parameters.Add("@packageid", SqlDbType.Int, 4).Value = chkpackage.SelectedItem.Value End If Next

        K 1 Reply Last reply
        0
        • L LovelyHelp

          I trying to loop too but i failed. What is the syntax for looping. BElow is what i done but it is wrong. Dim i As Integer For i = 0 To chkpackage.SelectedItem.Count - 1 mycommand2.Parameters.Add("@packageid", SqlDbType.Int, 4).Value = chkpackage.SelectedItem.Value End If Next

          K Offline
          K Offline
          KrIstOfK
          wrote on last edited by
          #4

          I guess the loop is allright, but i'm not familiar with VB.NET, but i can see that there is a logical mistake in your code. You're trying to add each time inside your loop the same parameter. But you need each time you loop a new command which is going to insert the data into your database. Once that is done, then you can step to the next item. For your information, maybe you can get some information out of it to transform it to VB.Net i've sketched it in C#:

          		CheckBoxList chkList = new CheckBoxList();
          		foreach(CheckBox chkBox in chkList.Items) {
          			if(chkBox.Checked) {
          				// make your command
          				// add your parameters
          				// save to the database
          			}
          		}
          
          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