Solved

asp.net - selected listbox items into array

Posted on 2010-11-16
4
843 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
ID: 34147171
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
ID: 34147193
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
ID: 34147212
Thank you.
0
 
LVL 13

Expert Comment

by:devlab2012
ID: 34147215
Change the following line:

Dim arrBrand As ArrayList

to

Dim arrBrand As New ArrayList

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

22 Experts available now in Live!

Get 1:1 Help Now