4 Replies - 3332 Views - Last Post: 23 October 2011 - 09:16 PM Rate Topic: -----

#1 fsavage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 11

Trouble using Session State for an Array of items

Posted 16 October 2011 - 06:32 PM

Hello all, I am working on a shopping site for a school project and I am trying to use Session State to hold the selections made and then retrieve them in another page. Most of the examples I find online show using sessions state for a single value at a time. I found an example on msdn showing the creation of an ArrayList as a session variable, but when I try to use it I receive the error:

"System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Collections.ArrayList'."


my code is as follows in my shopping page:

    Private Sub SelectCD()
        Dim cart As ArrayList = CType(Session("cart"), ArrayList) 'per msdn - ASP.NET Session State Overview
        Session("cart") = cart  'per msdn - ASP.NET Session State Overview

        If n = 0 Then
            txtSelect1.Text = rblAvailCD.SelectedItem.Text
        ElseIf n = 1 Then
            txtSelect2.Text = rblAvailCD.SelectedItem.Text
        ElseIf n = 2 Then
            txtSelect3.Text = rblAvailCD.SelectedItem.Text
        ElseIf n = 3 Then
            txtSelect4.Text = rblAvailCD.SelectedItem.Text
        ElseIf n = 4 Then
            txtSelect5.Text = rblAvailCD.SelectedItem.Text
        End If

        Session.Add("cart", rblAvailCD.SelectedItem.Text)   'per msdn - ASP.NET Session State Overview
    End Sub


The error is thrown on the line declaring the arraylist"

Dim cart As ArrayList = CType(Session("cart"), ArrayList)


I also found this C# code that appears to refer to a session object that would hold a set, but I have not been able to figure out how to translate it into VB.NET, to try it out.
STORE:
DataSet ds = GetDataSet(whatever parameters);
Session["mydataset")=ds;

RETRIEVE:
DataSet ds = (DataSet)Session["mydataset"];


I do realize that using text boxes is not ideal, but unfortunately it is required by our instructor in this project.

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble using Session State for an Array of items

#2 fsavage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 11

Re: Trouble using Session State for an Array of items

Posted 23 October 2011 - 01:26 PM

I finally figured out populating an array in Session State, but I found another issue. Session State is rounding my cost amounts up, but I am not sure why. I placed break points in to check the values being passed and they are correct until the line that actually adds the value to my session object.

  Private cartItems As New List(Of CartItem)


    'locates item cost from CD class
    Private Function ItemCost(ByVal title As String) As Integer
        Dim cost As Double
        Dim i As Integer
        For i = 0 To 19
            If title = stock(i).Title Then
                cost = stock(i).Price
            End If
        Next
        Return cost
    End Function

    'adds selected items to cart
    Private Sub AddToCart(ByVal title As String, ByVal cost As Double)
        Dim c As New CartItem(title, cost)
        'Dim cart As ArrayList = DirectCast(Session("Cart"), ArrayList)
        cartItems.Add(c)
        'cart.Add(c)
    End Sub


I initially thought the issue was due to using the arraylist, but the same issue occurs when I changed the line to:
cartItems.Add(c)


Any guidance is appreciated
Was This Post Helpful? 0
  • +
  • -

#3 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: Trouble using Session State for an Array of items

Posted 23 October 2011 - 01:55 PM

it looks like you're trying to return a Double value type but you have your method defined as an Integer return type
Was This Post Helpful? 1
  • +
  • -

#4 fsavage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 11

Re: Trouble using Session State for an Array of items

Posted 23 October 2011 - 04:39 PM

Thank you Nakor, i guess I have just been staring at this too long and just needed a fresh set of eyes.
Was This Post Helpful? 0
  • +
  • -

#5 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: Trouble using Session State for an Array of items

Posted 23 October 2011 - 09:16 PM

no problem, it happens to all of us from time to time :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1