Solved

Multiple select items in ListBox to SQL Database

Posted on 2011-03-14
2
794 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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