1

I am trying to take an array of string (Filled from a listbox on a previous page and passed via Session) and display it in a label,this is how i got the array:

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles CheckOut.Click
    Dim x = ListBox1.GetSelectedIndices.Count
    Dim ListPNames(x) As String
    Dim i As Integer

    i = 0
    For Each item As String In ListBox1.GetSelectedIndices
        ListPNames(i) = (ListBox1.SelectedItem).ToString
        i = i + 1

    Next


    Session("SlctdPhones") = ListPNames(x)

    Response.Redirect("CheckOut.aspx")


End Sub

And this is how i am trying to display it :

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load


    Dim SlctdPhones() As String = CType(Session.Item("SlctdPhones"), Array)
    Dim i As Integer

    Label3.Text = ""

    For i = 0 To SlctdPhones.Length - 1

    Label3.Text += SlctdPhones(i).ToString() + Environment.NewLine
    Next


End Sub

It is giving me an error :Object reference not set to an instance of an object. when it reaches the SlctdPhones.Length - 1 Line!! i don't know how i can fix it ,also is my array code correct(Is everything being stored correctly in it?)

xTMx
  • 67
  • 9
  • Probably related: [What does “Object reference not set to an instance of an object” mean?](http://stackoverflow.com/questions/779091/what-does-object-reference-not-set-to-an-instance-of-an-object-mean) – Krease May 24 '16 at 22:31
  • but the array is already declared and took the values from the previous page,why am i still getting the error?! that's what i don't understand – xTMx May 24 '16 at 22:41
  • If you can't tell for yourself via the debugger if the array has the correct data, you need to go back to the fundamentals here. – Joel Coehoorn May 25 '16 at 02:12

2 Answers2

2

You declare the For loop like this:

For Each item In ...

But then never use the item variable in the body of the loop. Instead, you keep using the same SelectedItem property. You want to change that whole method to look like this:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles CheckOut.Click
    Dim PNames As New List(Of String)()
    For Each index As Integer In ListBox1.GetSelectedIndices
        PNames.Add(ListBox1.Items(index).Value)
    Next
    Session("SlctdPhones") = PNames

    Response.Redirect("CheckOut.aspx")
End Sub

With that fixed, the Page_Load can do this:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim SlctdPhones As List(Of String) = TryCast(Session.Item("SlctdPhones"), List(Of String))

    If SlctdPhones Is Nothing OrElse SlctdPhones.Length = 0 Then
        'Something went wrong here!
        Return
    End If

    Label3.Text = String.Join("<br/>", SlctdPhones.ToArray())
End Sub

But I'd really love to see you use a data control rather than stuffing <br/>s into a label. Here's markup for a ListView:

<asp:ListView ID="ListView1" runat="server">
    <LayoutTemplate>
    <ul>
        <asp:PlaceHolder ID="itemPlaceholder" runat="server" />    
    </ul>                
    </LayoutTemplate>
    <ItemTemplate>
    <li><%# Container.DataItem.ToString() %></li>
    </ItemTemplate>
    <EmptyDataTemplate>
    <p>Nothing here.</p>
    </EmptyDataTemplate>
</asp:ListView>

And then Page_Load is even simpler:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    ListView1.DataSource =  Session.Item("SlctdPhones")
    ListView1.DataBind()
End Sub
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Tried both ,and ended up using the list view because it is much better and cleaner to use.Thanks! – xTMx May 25 '16 at 12:13
1

On display page, use Literal instead of Label

Dim SlctdPhones() As String = CType(Session.Item("SlctdPhones"), Array)
Dim result as String = string.Join("<br>", SlctdPhones) 'Instead of <br> try Environment.NewLine as well 
YourLitetal = result 

Hope this helps!

Sami
  • 3,686
  • 4
  • 17
  • 28
  • Another option could be,, you just create a delimited (by line-break) string at first page (rather array), pass it to next page, and display with using Literal. YourLiteral = Session("DelimitedSelectedItem").ToString() Thanks – Sami May 24 '16 at 23:38