Solved

asp.net - selected listbox items into array

Posted on 2010-11-16
4
842 Views
Last Modified: 2012-05-10
Hello,

I have a ListBox control that is being populated in code (from a SQL query).

How do I set the selected items to an ArrayList?


Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click

        Dim arrBrand As ArrayList

        For Each item As ListItem In lstBxBrand.Items
            If item.Selected = True Then
                arrBrand.Add(item.Text)
            End If
        Next

End Sub


This is giving me a "Object reference not set to an instance of an object", on:

                arrBrand.Add(item.Text)



Thank you.

0
Comment
Question by:Rick
4 Comments
 
LVL 5

Accepted Solution

by:
Vipul Patel earned 500 total points
Comment Utility
You have missed to initialize the array object.

The correct code would be like;
Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click

        Dim arrBrand As ArrayList = New ArrayList()

        For Each item As ListItem In lstBxBrand.Items
            If item.Selected = True Then
                arrBrand.Add(item.Text)
            End If
        Next

End Sub
0
 
LVL 6

Expert Comment

by:ajb2222
Comment Utility
You have declared array brand but havenot set it to an object

add this line after the dim statement

arrBrand = new ArrayList
0
 
LVL 13

Author Closing Comment

by:Rick
Comment Utility
Thank you.
0
 
LVL 13

Expert Comment

by:devlab2012
Comment Utility
Change the following line:

Dim arrBrand As ArrayList

to

Dim arrBrand As New ArrayList

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

13 Experts available now in Live!

Get 1:1 Help Now