Solved

asp.net - selected listbox items into array

Posted on 2010-11-16
4
850 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

734 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