Creating Session arrasy and variables in ASP
At any time we can create a session variable and store data in it. Let us try to store user name in our session variable. We have given the name of our session variable as name only. Here is the example
session("name")=name
Note that the variable name has the name of the user and now it is available as a session variable. Let us learn how to display this session variable.
Response.Write "Welcome " & session("name")
Above line will display a welcome message with the user name. Now let us try to display how many session variables are present for the user or the total number of session variables present for the user.
Response.Write Session.Contents.Count
The above line will display a number as total number of session variables present in our user session.
Removing all values from Session Variables
We can destroy or remove all session variables like this .
Session.Abandon
OR
Session.Contents.RemoveAll()
Selectively removing session values
There may be a requirement of selectively removing some session variables. Let us learn that . Here is an example of removing one particular session variable.
Session.Contents.Remove("name")
ASP Session array
We can store array in our session and carry that to different pages in ASP. We will learn how to assign a session array from a local array. Display all elements from a session array and how to assign a local array from session array.
Let us take the example of a simple array and assign some elements to it.
Dim language(3)
language(0)="ASP"
language(1)="PHP"
language(2)="JavaScript"
language(3)="Perl"
For our learning let us display all elements of the array once.
Dim i
For i = LBound(language) to UBound(language)
Response.Write language(i) & "<br>"
Next
Same way we will be displaying our session arrays also so a better understanding from the beginning will help. Now Let us assign our session variable to this array.
Session("my_languages")=language
Now we have our array in our session variable. Let us try to display elements of the session array.
For i = LBound(session("my_languages")) to UBound(session("my_languages"))
Response.Write session("my_languages")(i) & "<br>"
Next
The above code will display our session elements. Now let us move to another page and check our session array. You can write the same ( above ) code and display the elements. Now let us learn how to transfer the session array to a local array.
Dim i,my_languages2
my_languages2=session("my_languages")
Now we have our my_languages2 as local array storing the value from session array. Now we will loop through and display all the elements of this local array. Before that we will use one if condition checking to know our variable is an array or not .
If IsArray(my_languages2) then
for i=LBound(my_languages2) to UBound(my_languages2)
Response.Write my_languages2(i) & "<br>"
Next
Else
Response.Write " not an array"
End If
This article is written by plus2net.com team.
Be the first to post comment on this article :
plus2net.com
|