Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session Variable Arrays?

Status
Not open for further replies.

stsuing

Programmer
Joined
Aug 22, 2001
Messages
596
Location
US
Is is possible to make a session Variable an Array?

I am putting together a survey and want to store all the answers in a session variable instead of making multiple updates to s database. There are 77 questions on 8 asp pages.

Thanks in advance
 
Hello stsuing!

Sure it's possible. Here is how to use Session and arrays taken from IIS 5 Documentation (write again, if you don't find it clear enough):


If you store an array in a Session object, you should not attempt to alter the elements of the stored array directly. For example, the following script will not work:

<% Session(&quot;StoredArray&quot;)(3) = &quot;new value&quot; %>

This is because the Session object is implemented as a collection. The array element StoredArray(3) does not receive the new value. Instead, the value is indexed into the collection, overwriting any information stored at that location.

It is strongly recommended that if you store an array in the Session object, you retrieve a copy of the array before retrieving or changing any of the elements of the array. When you are done with the array, you should store the array in the Session object again so that any changes you made are saved. This is demonstrated in the following example:

---file1.asp---
<%
'Creating and initializing the array
Dim MyArray()
Redim MyArray(5)
MyArray(0) = &quot;hello&quot;
MyArray(1) = &quot;some other string&quot;

'Storing the array in the Session object.
Session(&quot;StoredArray&quot;) = MyArray

Response.Redirect(&quot;file2.asp&quot;)
%>

---file2.asp---
<%
'Retrieving the array from the Session Object
'and modifying its second element.
LocalArray = Session(&quot;StoredArray&quot;)
LocalArray(1) = &quot; there&quot;

'Printing out the string &quot;hello there.&quot;
Response.Write(LocalArray(0)&LocalArray(1))

'Re-storing the array in the Session object.
'This overwrites the values in StoredArray with the new values.
Session(&quot;StoredArray&quot;) = LocalArray
%>


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top