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. General Programming
  3. C#
  4. ListBox Multiple Selection

ListBox Multiple Selection

Scheduled Pinned Locked Moved C#
5 Posts 3 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
    Lost User
    wrote on last edited by
    #1

    The ListBox gets populated by SQL server database (databinding), I need to support multiple selection so that I can retrieve all the selected values in the ListBox and send them back to database as sql statement and then get the result back to show in the web page. My question is: How can I construct the sql statement with parameter (array) that reflect those selections in ListBox? e.g. In sql statement " Where id=@id" works for single parameter, if id is type of int, @id is also type of int. Now I have an id array holding the selected id from ListBox. What will be the array type. How can I construct sql ? Chris#

    A J L 3 Replies Last reply
    0
    • L Lost User

      The ListBox gets populated by SQL server database (databinding), I need to support multiple selection so that I can retrieve all the selected values in the ListBox and send them back to database as sql statement and then get the result back to show in the web page. My question is: How can I construct the sql statement with parameter (array) that reflect those selections in ListBox? e.g. In sql statement " Where id=@id" works for single parameter, if id is type of int, @id is also type of int. Now I have an id array holding the selected id from ListBox. What will be the array type. How can I construct sql ? Chris#

      A Offline
      A Offline
      Andres Manggini
      wrote on last edited by
      #2

      You'll have to create an "or" separated parameter list for the Where Taking your example: "... Where id=1 or id=2 or id=3 " So, I would say a for statement iterating through the array, getting each value and adding "or id=" + value Something along theselines.. I'm not sure if this is clear enough, if you have any questions about it, i'll try to clarify. Andres Manggini. Buenos Aires - Argentina.

      L 1 Reply Last reply
      0
      • A Andres Manggini

        You'll have to create an "or" separated parameter list for the Where Taking your example: "... Where id=1 or id=2 or id=3 " So, I would say a for statement iterating through the array, getting each value and adding "or id=" + value Something along theselines.. I'm not sure if this is clear enough, if you have any questions about it, i'll try to clarify. Andres Manggini. Buenos Aires - Argentina.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Thanks! I will try your way. I just posted another 2 questions. You may have idea too! Thanks again!

        1 Reply Last reply
        0
        • L Lost User

          The ListBox gets populated by SQL server database (databinding), I need to support multiple selection so that I can retrieve all the selected values in the ListBox and send them back to database as sql statement and then get the result back to show in the web page. My question is: How can I construct the sql statement with parameter (array) that reflect those selections in ListBox? e.g. In sql statement " Where id=@id" works for single parameter, if id is type of int, @id is also type of int. Now I have an id array holding the selected id from ListBox. What will be the array type. How can I construct sql ? Chris#

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          Andres should work but you can shorten it down by using the "IN" keyword. WHERE id IN ( @id1, @id2, @id3, ... ) I'm pretty sure you'll have to use dynamic sql generation if you try this in a stored proc though. James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978

          1 Reply Last reply
          0
          • L Lost User

            The ListBox gets populated by SQL server database (databinding), I need to support multiple selection so that I can retrieve all the selected values in the ListBox and send them back to database as sql statement and then get the result back to show in the web page. My question is: How can I construct the sql statement with parameter (array) that reflect those selections in ListBox? e.g. In sql statement " Where id=@id" works for single parameter, if id is type of int, @id is also type of int. Now I have an id array holding the selected id from ListBox. What will be the array type. How can I construct sql ? Chris#

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I created a method to generate sql string "where clause", and then append the string to the first part of sql. It works fine. Thanks for both of your help. chris#

            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