Solved

Multiple select items in ListBox to SQL Database

Posted on 2011-03-14
2
788 Views
Last Modified: 2012-05-11
I have two list boxes and I have written a statement to insert the selected item into the database, that work for single items.  But I cannot get it to insert ALL selected items into the SQL database.  Below is my VB.NET code.  I am not getting an error and it is only inserting the first selected item.

 For i = 0 To ListBox1.Items.Count - 1
            If ListBox1.Items(i).Selected = True Then

                'Data to be inserted
                cmd.Parameters("@amenityid").Value = Me.ListBox1.SelectedValue
                cmd.Parameters("@locationid").Value = Me.Request.QueryString("LocationID")

                Dim ok As Integer = cmd.ExecuteNonQuery
                MsgBox("Conference Room successfully updated.")

            End If
            Next i

 Current Views
0
Comment
Question by:vnewman29
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35129294
Instead of:
cmd.Parameters("@amenityid").Value = Me.ListBox1.SelectedValue

Open in new window

You need:
cmd.Parameters("@amenityid").Value = Me.ListBox1.Items(i).Value

Open in new window

Otherwise you are always retrieving the value of the first selected item.
0
 

Author Closing Comment

by:vnewman29
ID: 35129417
I've spent two days on this.  Your solution work.  THANK YOU!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question