Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: code


Message #1 by jiwilliams@c... on Wed, 29 May 2002 21:12:12
Hi all,
I'm trying to fill an array to test weather this session variable works 
works. Does anyone see a problem this? I can't get it to work.
<%
dim x
Dim arrTemp(x) 
x = 0
?first step is to fill in an array arrTemp with a changing variable
do
Session("arrTemp(x)") = "changing variable"

X = x + 1

Loop until x = 10      ? 

' Second step is to print the array of varaibles to see if it took it.
x = 0
Do 

Response.Write(session("arrTemp(x)") & "<br>")
x = x + 1

Loop until x = 10
%>

Message #2 by "Imar Spaanjaars" <Imar@S...> on Thu, 30 May 2002 10:54:37
Hi there,

You cannot directly use items from a session array. You'll need to 
retrieve a local copy of the session array, change any items you want and 
then store the array in the session again.

Dim arrLocal
arrLocal = Session("MyArray")
If IsArray(arrLocal) Then
    arrLocal(x) = "SomeValue"
    Session("MyArray") = arrLocal
End If


HtH

Imar

> Hi all,
I> 'm trying to fill an array to test weather this session variable works 
w> orks. Does anyone see a problem this? I can't get it to work.
<> %
d> im x
D> im arrTemp(x) 
x>  = 0
?> first step is to fill in an array arrTemp with a changing variable
d> o
S> ession("arrTemp(x)") = "changing variable"

> X = x + 1

> Loop until x = 10      ? 

> ' Second step is to print the array of varaibles to see if it took it.
x>  = 0
D> o 

> Response.Write(session("arrTemp(x)") & "<br>")
x>  = x + 1

> Loop until x = 10
%> >

Message #3 by "the Office of Brent Allen VanderMeide" <ccbbttmm@a...> on Thu, 30 May 2002 17:37:55 -0600
Hello,

Once a month I find time to answer a question from this site, and I
thought I would choose yours as it probably applies to several people as
well.  The following information will help you understand and
confidently program using session array variables.
________________________________________________________________________
____

PROBLEMS WITH CURRENT CODE
	1.	You attempting to resize a fixed array.
	2.	You are accessing the array through the session
improperly

OBJECTIVES
	1.  	I will teach you what, why, and how to
	2.	sample files for instruction. 
	3.	Best training possible is by example attatched with
instruction

File 1 - Form.asp			' WE WILL CREATE AN HTML ARRAY
THROUGH A FORM
File 2 - Form_hndlr.asp		' CONVERTS ARRAY TO ASP ARRAY AND INTO A
					' SESSION VARIABLE ARRAY.
File 3 - Form_End.asp		' WE WILL THEN ACCESS OUR SESSION
VARIABLE
					' ARRAY FOR OUTPUT TO HTML.


<!-- FILE 1 - FORM.asp -->
<HTML><HEAD>FILE 1 - The Form</HEAD>
<BODY>
	<FORM ACTION="Form_hndlr.asp" METHOD="POST">
		<!--
			by assigning multiple fields the same NAME
attribute
			we create an array that is passed to the server
		-->
		<INPUT TYPE="TEXT" NAME="myArray" VALUE="1"><BR>
		<INPUT TYPE="TEXT" NAME="myArray" VALUE="2"><BR>
		<INPUT TYPE="TEXT" NAME="myArray" VALUE="3"><BR>
		<INPUT TYPE="SUBMIT" NAME="mySubmitButton" VALUE="SEND
ARRAY TO HANDLER">
	</FORM>
</BODY>
</HTML>


<%
' FILE 2 - FORM_hndlr.asp
'What is virtually passed from the form to the server as a request
'------------------------------------------------------------------
'IF IT WAS SENT USING EITHER "POST" OR "GET" METHOD
'	to implicity access variables sent by either method
'	you can use request(variablename)
'
'
'IF IT WAS SENT USING THE "POST" METHOD
'	to explicity access variables sent by POST
'	you can use the request.queryForm(variablename)
'
'	Requestpath=http://domain.com/....curentpath/form_hndlr.asp
'	HeaderSentVariables
'		myArray="1"
'		myArray="2"
'		myArray="3"
'		mySubmitButton="SEND ARRAY TO HANDLER"
'	
'
'IF IT WAS SENT USING THE "GET" METHOD
'	to explicity access variables sent by POST
'	you can use the request.queryString(variablename)
'
Requestpath=http://domain.com/....curentpath/form_hndlr.asp?myArray=1&my
Array=2&myArray=3&mySubmitButton=SEND+ARRAY+TO+HANDLER
'
'------------------------------------------------------------------
'

'DIM myArray(2)	< this would create a static, nonresizable, array.

'DIM x		< this still creates a static,
'DIM myArray(x)   < nonresizable, array.

' The ReDim statement is used to resize dynamic arrays
DIM myArray()	' < this creates a dynamic array
ReDim myArray(0)	' < After creating we then set an initial size

DIM curArrayCnt	' This will be for later

' To find out how large of an array was sent to this page from page 1
' We do the following:
'------------------------------------------------------------------

' Request(variablename).count 		< Implicit access for POST or
GET
' Request.queryString(variablename).count < Explicit access for GET
' Request.queryForm(variablename).count 	< Explicit access for
POST

' Page 1 uses the POST method so we are going to get the count
' and resize our ASP variable through the Explicit "queryForm" method.

ReDim myArray(Request.queryForm("myArray").count)


curArrayCnt		' < We will use this variable to keep location
of 

' The UBound statement will get the highest count
' of any dimension in an array.  In this example
' it will get the highest # of the first dimension
' of the "myArray" array.

curArrayCnt = 0
While curArrayCnt <= UBound(myArray, 1)	

	' IF YOU DO NOT SPECIFY THE ARRAY# TO ACCESS THE FIRST VALUE IS
	' RECEIVED.

	' myArray(curArrayCnt) = request.queryForm(myArray)
	
	' myArray(curArrayCnt) would now equal "1" because it was the
first
	' value on page 1 with the myArray NAME attribute

	'IMPROPER WAY OF ACCESSING ARRAYS THROUGH THE REQUEST OBJECT
	'myArray(curArrayCnt) = request.queryForm(myArray(curArrayCnt))


	'THE PROPER WAY OF ACCESSING ARRAYS THROUGH THE REQUEST OBJECT
	myArray(curArrayCnt) = request.queryForm(myArray)(curArrayCnt)
	curArrayCnt = curArrayCnt + 1
Wend

' !!!!!!!!!!!!!!! FINISHED !!!!!!!!!!!!!!!
' NOW, YOU WANTED TO PASS ARRAYS FROM ONE PAGE TO ANOTHER THROUGH
SESSION
' VARIABLES.  WELL LET'S GET STARTED.. READY.. TOO BAD.. JUST KIDDING.

' IMPROPER WAYS OF ASSIGNING ARRAYS TO SESSION VARIABLES
' Session("myArray"()) = myArray()
' Session("myArray")() = myArray()
' Session("myArray") = myArray()
' Session("myArray") = myArray(UBound(myArray, 1))
'
' AS HARD AS IT MAY SEEM TO FIGURE THIS OUT, IT WAS
' ACTUALLY THE SIMPLEST WAY THAT DOES IT

Session("myArray") = myArray



' Now we are redirecting the user to File3(FORM_End.asp)
' to pick up this session variable to print back to the end-user
' But before we do that, we should kill our variables that are
' still taking up memory space

SET myArray = Nothing
SET curArrayCnt = Nothing

' Now the redirection.
Response.ReDirect "FORM_End.asp"
%>

<!-- FILE3 - FORM_End.asp -->
<%
	DIM curArrayCnt	' Will be used for the same purpose as in FILE2
%>
<HTML><HEAD><TITLE>FILE 3 - THE FORM RESPONSE</TITLE></HEAD>
<BODY>
	<%
	' IMPROPER
	' The #1 issue that people have with getting session
	' array variables is that they declare the array
	' variable first.  Normally would be correct in VB
	'
	' DIM myArray()
	' myArray() = Session("myArray"())
	' myArray() = Session("myArray")()
	' myArray() = Session("myArray") 
	'
	' PROPER
	' However, because variables held in the Session collection
	' are unknown when we try to assign the myArray = the 
	' session variable ASP will through an error up.
	'
	' Also, once again, as it was simple to set the session
	' array variable it's just as simple to get it, and that's 
	' because both the myArray and the session("myArray")
	' variables are both VARIANTS as far as VB can tell
	'
	myArray = Session("myArray")	' Simpler than you thought eh?
	curArrayCnt = 0
	WHILE curArrayCnt <= UBound(myArray, 1)
		%>
		MyArray(<%= curArrayCnt %>) = 
		<%= Server.HTMLEncode(myArray(curArrayCnt)) %>
		<BR>
		<%
		curArrayCnt = curArrayCnt + 1
	WEND

	' Remember to Kill variables
	SET curArrayCnt = Nothing
	SET myArray = Nothing
	Session.Contents.Remove "myArray"
	%>
</BODY>
</HTML>

________________________________________________________________________
____
Congratulations, We are done.

Thank you for the opportunity to help.  I hope this has not only helped
you but others who have dealt with trying to access arrays through HTML,
ASP, and ASP Session Variables.


Thank you,

Brent Allen VanderMeide
Intermountain Software Solutions
Owner / Senior Web & Application Developer
support@s...


Message #4 by "the Office of Brent Allen VanderMeide" <ccbbttmm@a...> on Thu, 30 May 2002 18:06:00 -0600
Hello,
_______________________________________________________
I JUST REALIZED A MISTAKE OF MY OWN

In FILE2, it compares Request.QueryString to Request.QueryForm.  It
should be Request.Form not Request.QueryForm


On both File2 and File3 the proper, mine shows the improper, way to
deallocate or kill an array variable is to use

ERASE myArray
	Instead of
SET myArray = Nothing
_______________________________________________________
Brent Allen VanderMeide
Intermountain Software Solutions
Owner / Senior Web & Application Developer
support@s...



  Return to Index