Solved

Multiple select items in ListBox to SQL Database

Posted on 2011-03-14
2
771 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
A short film showing how OnPage and Connectwise integration works.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

943 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now