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. List Box Problem

List Box Problem

Scheduled Pinned Locked Moved C#
databasehelp
9 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.
  • S Offline
    S Offline
    SeeSharp 0
    wrote on last edited by
    #1

    Hi all, I m having problem with selecting multiple values in a listbox and inserting tht in the database. I wrote this code: if (lstResource.SelectedIndex != -1) { for (int i = 0; i < lstResource.Items.Count; i++) { if (lstResource.Items[i].Selected == true) { stringBuilder.Append(lstResource.Items[i].Value + ","); } } } but somehow its not giving any value as TRUE, even if it is selected. Thanks in advance :^)

    C Y V 3 Replies Last reply
    0
    • S SeeSharp 0

      Hi all, I m having problem with selecting multiple values in a listbox and inserting tht in the database. I wrote this code: if (lstResource.SelectedIndex != -1) { for (int i = 0; i < lstResource.Items.Count; i++) { if (lstResource.Items[i].Selected == true) { stringBuilder.Append(lstResource.Items[i].Value + ","); } } } but somehow its not giving any value as TRUE, even if it is selected. Thanks in advance :^)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Where does the data for the listbox come from ? Are you resetting the data on postback ? If you are, then that will cause all your selections to be lost.

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      S 2 Replies Last reply
      0
      • S SeeSharp 0

        Hi all, I m having problem with selecting multiple values in a listbox and inserting tht in the database. I wrote this code: if (lstResource.SelectedIndex != -1) { for (int i = 0; i < lstResource.Items.Count; i++) { if (lstResource.Items[i].Selected == true) { stringBuilder.Append(lstResource.Items[i].Value + ","); } } } but somehow its not giving any value as TRUE, even if it is selected. Thanks in advance :^)

        Y Offline
        Y Offline
        yogesh_kumar_agarwal
        wrote on last edited by
        #3

        The SelectedItems property will return the list of all the seleted items in a list box use this property to get the seletecd values. This will return you a string array use foreach loop to get the values. With Regards Yogesh Agarwal

        1 Reply Last reply
        0
        • S SeeSharp 0

          Hi all, I m having problem with selecting multiple values in a listbox and inserting tht in the database. I wrote this code: if (lstResource.SelectedIndex != -1) { for (int i = 0; i < lstResource.Items.Count; i++) { if (lstResource.Items[i].Selected == true) { stringBuilder.Append(lstResource.Items[i].Value + ","); } } } but somehow its not giving any value as TRUE, even if it is selected. Thanks in advance :^)

          V Offline
          V Offline
          Vikram A Punathambekar
          wrote on last edited by
          #4

          ListBox.Items[i] returns the object the listbox contains. It does not contain a property called Selected.

          Cheers, Vikram.


          If the radiance of a thousand suns Were to burst at once into the sky That would be like the splendor of the Mighty one— I am become Death, The shatterer of Worlds.

          S N 3 Replies Last reply
          0
          • C Christian Graus

            Where does the data for the listbox come from ? Are you resetting the data on postback ? If you are, then that will cause all your selections to be lost.

            Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            S Offline
            S Offline
            SeeSharp 0
            wrote on last edited by
            #5

            Hi Christian Graus, Data is comin from the database table. I did binding on page load. as in below: lstResource.DataSource = ds; //ds-Dataset lstResource.DataTextField = "Names"; On Submit button click i want the selected values to get inserted in database together.

            1 Reply Last reply
            0
            • V Vikram A Punathambekar

              ListBox.Items[i] returns the object the listbox contains. It does not contain a property called Selected.

              Cheers, Vikram.


              If the radiance of a thousand suns Were to burst at once into the sky That would be like the splendor of the Mighty one— I am become Death, The shatterer of Worlds.

              S Offline
              S Offline
              SeeSharp 0
              wrote on last edited by
              #6

              Hi Vikram.. It does have the property called "Selected"

              1 Reply Last reply
              0
              • C Christian Graus

                Where does the data for the listbox come from ? Are you resetting the data on postback ? If you are, then that will cause all your selections to be lost.

                Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                S Offline
                S Offline
                SeeSharp 0
                wrote on last edited by
                #7

                Oh..ok... Thanx Christian Graus .... Actually i was resetting data on postback.. I just put the code if (!IsPostBack) {} And it worked like a charm.... :-D Thanks a lot

                1 Reply Last reply
                0
                • V Vikram A Punathambekar

                  ListBox.Items[i] returns the object the listbox contains. It does not contain a property called Selected.

                  Cheers, Vikram.


                  If the radiance of a thousand suns Were to burst at once into the sky That would be like the splendor of the Mighty one— I am become Death, The shatterer of Worlds.

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #8

                  Vikram A Punathambekar wrote:

                  It does not contain a property called Selected.

                  It's a cross post. Looks like OP is using ASP.NET.

                  All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                  1 Reply Last reply
                  0
                  • V Vikram A Punathambekar

                    ListBox.Items[i] returns the object the listbox contains. It does not contain a property called Selected.

                    Cheers, Vikram.


                    If the radiance of a thousand suns Were to burst at once into the sky That would be like the splendor of the Mighty one— I am become Death, The shatterer of Worlds.

                    S Offline
                    S Offline
                    SeeSharp 0
                    wrote on last edited by
                    #9

                    Hi, Just put a dot after thsi and you get selected as a property. check out. ListBox.Items[i] ----Naina

                    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