Solved

Multiple select items in ListBox to SQL Database

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

708 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

16 Experts available now in Live!

Get 1:1 Help Now