When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback

The 4 Guys Present: ASPFAQs.com

Jump to a FAQ
Enter FAQ #:
..or see our 10 Most Viewed FAQs.

4GuysFromRolla.com : ASP FAQS : Arrays


How do I put an array into a Session or Application "variable"? How do I then use it on the next ASP page?

[Print this FAQ]

Answer: First, my standard little lecture:

"Session Variable" and "Application Variable" are horrible misnomers. If you look at the ASP docs, you will see that what is actually in place is a collection named Session.Contents or Application.Contents. You are actually putting key/value pairs into this collection and then retrieving the values via the keys. So "Session Values" and "Application Values" might be better names. But we are stuck with the names MS gave this capability, forever no doubt.


Usually, the whole point of putting an array into a session or application value is that you want to be able to add more info to it in a later page. But even if you will not change the size of your array, the techniques described here work and work well.

The code that follows shows working with a "session array" that is intended to hold the contents of a user's shopping cart. The array is five (5) elements "wide" by any number of items "long." That is, for each item in the shopping cart, we will keep track of five pieces of information (the five "columns" of the array). And we won't set any limit on the number of items we will put in the cart.

We hope this kind of usage is fairly typical of the reason most coders would use a "session array."

On to the code. On the originating page (which could even be in Session_onStart in "global.asa", but certainly doesn't need to be):

Dim cartArray( ) ' <<<< notice no size!
ReDim cartArray( CART_COLUMNS, 10 ) ' an arbitrary initial size
cartMaxUsed = -1 ' no elements used yet...
Session("cartArray") = cartArray ' names don't have to be same,
Session("cartMaxUsed") = cartMaxUsed ' but I like it that way

Then, on any subsequent ASP page, we get the data from the Session values back into variables, manipulate the data as we want, and store the modified data back into the session values. Thus:

Dim cartArray ' no parens! no size!
Dim cartMaxUsed

cartArray = Session("cartArray")
cartMaxUsed = Session("cartMaxUsed")
... work with the array ...
... add or change info ...
Session("cartArray") = cartArray ' names don't have to be same,
Session("cartMaxUsed") = cartMaxUsed ' but I like it that way

But the key to making this useful is being able to ADD elements to the array. And you do that (in any page, including the first!) like this:

' this function will return the *index* of the next
' available slot in the cartArray. If the cartArray
' is full, it is expanded before the index is returned!
Function addCartElement( )
    If cartMaxUsed >= UBound(cartArray,2) Then
        ' add 10 elements!
        ReDim Preserve cartArray( CART_COLUMNS, cartMaxUsed + 10 )
    End If
    cartMaxUsed = cartMaxUsed + 1
    addCartElement = cartMaxUsed
End Function

And now you can use that function thus:

' let's say we want to add an item to the shopping cart:
cartItem = addCartElement( )
cartArray(0,cartItem) = "Baby Blanket"
cartArray(1,cartItem) = "Blue"
cartArray(2,cartItem) = "Large"
cartArray(3,cartItem) = 14.95
cartArray(4,cartItem) = 2

And we just added 2 large blue baby blankets at $14.95 each to the shopping cart.

Note that the "cart item number" must be the last array index. That's because ReDim Preserve can only change the last bound of an array.

Also, be careful to use the columns in the array consistently. You might even want to do something like this:

' let's say we want to add an item to the shopping cart:
cartItem = addCartElement( )
cartArray(ITEM_NAME,cartItem) = "Baby Blanket"
cartArray(ITEM_COLOR,cartItem) = "Blue"
cartArray(ITEM_SIZE,cartItem) = "Large"
cartArray(ITEM_PRICE,cartItem) = 14.95
cartArray(ITEM_QUANTITY,cartItem) = 2

Which, just incidentally, makes the code much more readable.

Hints for performance and safety!

(1) Never work with an array "directly" in a session/application value. That is, never do something like Session("cartArray")(3,17) = "whatever". You should always copy the array to a VBS variable, use it and, if you alter it, copy it back to the session/application value.

(2) When using Application-level arrays, be sure to do Application.Lock before you read the value into the local VBS variable and then do *not* do Application.Unlock until after you have copied the altered value back to the application variable. (If you will only read the application value and not alter it, the Lock/Unlock is not strictly necessary. However, if you need to read multiple application variable values that should stay "in sync", then you should still use Lock/Unlock.)

FAQ posted by Bill Wilkinson at 10/16/2000 8:36:57 PM to the Arrays category. This FAQ has been viewed 108,962 times.

Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions!

Most Viewed FAQs:

1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views)
2.) I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem? (207777 views)
3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views)
4.) How can I quickly sort a VBScript array? (196039 views)
5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views)
6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views)
7.) When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views)
8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views)
9.) Can I send emails without using CDONTS? (107083 views)
10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views)
Last computed at 9/17/2007 3:22:00 AM

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article