Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 853
  • Last Modified:

asp.net - selected listbox items into array

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
Rick
Asked:
Rick
1 Solution
 
Vipul Patel.NET ExpertCommented:
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
 
ajb2222Commented:
You have declared array brand but havenot set it to an object

add this line after the dim statement

arrBrand = new ArrayList
0
 
RickAuthor Commented:
Thank you.
0
 
devlab2012Commented:
Change the following line:

Dim arrBrand As ArrayList

to

Dim arrBrand As New ArrayList

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now