Solved

asp.net - selected listbox items into array

Posted on 2010-11-16
4
851 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

636 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