Solved

Multiple select items in ListBox to SQL Database

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 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