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
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.
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 .
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.
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.
For our learning let us display all elements of the array once.
For i = LBound(language) to UBound(language)
Response.Write language(i) & "<br>"
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.
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>"
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.
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>"