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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declaring & Initializing Variables 1

Status
Not open for further replies.

snaegs99

Programmer
Oct 10, 2001
12
US
Hi All,
I hope you can help me... I am very new to ASP and I am trying to declare a variable and initialize the variable to 0. The following is what I've tried in the body of my code:

<% Dim scID %>
<% scID = 0 %>
scID= <% =scID %>

when it displays on the screen scID has a null value. I've tried initializing it as a string, but it was still null. Any help would be greatly appreciated!
~Sarah
 
Sarah,

I copied your exact code into a test ASP page over here and I got.

scID= 0

Any thoughts anybody ??

TW
 
Thanks for your immediate responses.

I've been playing around with it and realized that I'm trying to do this in a .html page and not an .asp page.

My .html page links to .asp pages and needs to pass this value as a parameter. I really don't know how to do this...Any suggestions?
 
Sarah,

You can do that in the Url and get the data from the QueryString collection, or in a hidden form field. For ease, I would use the QueryString collection.

Your HTML could look like this.

<a href=&quot;asppage.asp?scID=0&quot;>Go To ASP Page</a>

Then in your ASP page, you can access that variable like this.

<%
Response.Write Request.QueryString(&quot;scID&quot;)
%>

Should put a zero on the screen.

You can put multiple key/value pairs, up to 256 characters, in the url by building them like this.

<a href=&quot;asppage.asp?scID=0&zyID=1&abcID=2&quot;>Go There</a>

Separating each key/value pair with an ampersand &.

Keep in mind, visitors can see the url and key/value pairs while they are navigating, thus they could potentially change them and send them to the ASP page also. If you want to avoid this, you can do it by creating an HTML form with hidden key/value pairs. Then ASP examines the Request.Form collection to get those. Let me know if you would like an example of this.

TW
 
I would like an example of the hidden parameters. I was trying to use something like

<INPUT TYPE=HIDDEN NAME=scID VALUE=<% =scID %>>

and then on the recieving page I put

<% Dim scID %>
<% scID = Request.Form(&quot;scID&quot;) %>

and once again I think it wasn't working because I am using a .html page as my first page. I also need to pass this parameter between .asp pages so if you could help with that syntax too that would be great!!!
 
OK. If you're using the HTML page as the first page, you won't be able to use any ASP or VBScript code there.

Also, you won't be able to just put

<input type=&quot;hidden&quot; name=&quot;scID&quot; value=&quot;something&quot;>

by itself in the HTML page. It needs to be inside a form and the form needs to be submitted to the ASP page to pass those values. Something like this.

<form method=&quot;post&quot; action=&quot;asppage.asp&quot;>
<input type=&quot;hidden&quot; name=&quot;scID&quot; value=&quot;something&quot;>
<input type=&quot;submit&quot; value=&quot;Submit&quot;>
</form>

When you click on the Submit button, it passes the form collection to the ASP page. I must say, I'm a little confused on exactly what your first HTML page looks like. Give me an example of what you're trying to accomplish between the HTML and the ASP page.

TW
 
Thank you so much for all of your help. This is for a school assignment and my teacher didn't really know why I was having a problem.

If you go to
this is my .html page. My teacher wrote this page and I need to modify it.

Click on the &quot;Product Guide&quot; link and this will take you to products.asp, which is where I need to pass a parameter. I need to initialize the variable on the .html page because my group is doing a shopping cart and that's how we know this is a new user.

I cannot initialize it on the .asp page because they will re-visit this page many times throughout their navigation.

When they order something for the first time, we will change the value accordingly and we will know they have already been assigned a shopping cart number.
~Sarah
 
I have a solution for you, but first I want to ask you one question that will help me provide you with that solution.

Is there a specific reason the home page is HTML and not ASP ??

TW
 
The only reason the first page is HTML and not ASP is because that's the way my teacher created it. I'm not sure if I can change it, but I don't think she would mind if I had to change it to an ASP page to make the coding easier. I would still like to know how to do it using an HTML page, however, if you can tell me...
~Sarah
 
OK. Well, somewhere we need to declare a Session Variable. This is easiest because it will automatically persist from page to page without you having to do it through code. In this case, we are going to do it on the second page and you may need to repeat this on other pages that are accessed from the first page depending on your need.

Now, on the 2nd page, you'll initialize the variable like this. Put this before any HTML tags.

<%
IF Session(&quot;user_status&quot;) = &quot;&quot; THEN
Session(&quot;user_status&quot;) = &quot;0&quot;
END IF
%>

The IF.. ENDIF is designed so that it won't initialize the user_status variable if one is already present for this visitor. You mentioned that people may be accessing these pages several times. This will take care of that. If the Session(&quot;user_status&quot;) has already been created, this code will be ignored.

Now, anywhere throughout the application, you can examine that Session Variable like this.

<%
IF Session(&quot;user_status&quot;) = &quot;&quot; THEN
' do something
END IF

IF Session(&quot;user_status&quot;) = &quot;0&quot; THEN
' this visitor HAS been to your second page
END IF
%>

You can change that variable at any time like this.

<%
Session(&quot;user_type&quot;) = &quot;1&quot;
%>

Remember, I would always examine the session variable before initializing it or changing it using IF .. ENDIF statements like I have provided above.

Session variables are great because they persist automatically in IIS memory for each user. No need to pass them as form elements from page to page. And you can have as many Session variables per user as you wish.

You might have Session(&quot;user_type&quot;) and Session(&quot;shop_cart_count&quot;) and so on and so forth with each variable being unique to each user. This is standard operating procedure for most applications with shopping carts, etc... because the variables are kept on the server and are not visible by the visitor through any source code or anything like that.

There's a little more to it so if you get stuck, don't hesitate to let me know.

TW



 
Thank you so much for your help. I will be trying this out tomorrow and will probably be back with some questions.
Thanks Again,
~Sarah
 
TW~
I am having a little trouble because I don't know how to change this string to an integer so that I can add a number to it and store it in an integer field of a database. Also, how would I go about displaying this value on the screen? Can I access the variable inside the HTML tags, or does it always have to be outside the tags?
~Sarah
 
Sarah,

Use the Cint() method or CLng() method to convert strings into numbers. ASP also employs a FormatNumber() method which is helpful in formatting a number specifically for display on your page.

More on the FormatNumber() method in ASP here:



<%
dim myStr
dim myInt

myStr = Session(&quot;somesessionvariable&quot;)
myInt = CInt(myStr)
%>



You can write ASP directly into HTML like this.

<font face=&quot;Arial&quot; size=&quot;2&quot;>My Name Is <%=myInt%></font>


Let me know if you need any additional tips. :)

ToddWW
 
OK,
I found that I also need to change the integer back to a string when I pick it up from the database. Seeing the above examples and knowing a little about VB, I tried

Session(&quot;scID&quot;) = CStr(intID)

and I also tried

myStr = CStr(intID)
Session(&quot;scID&quot;) = myStr

and they both gave me an error that said type mismatch on the line with Session(&quot;scID&quot;) =...

I think I may be using the wrong function to change the integer to a string. I also tried CString(), To_String(), and ToString() and got the same error.

Thanks,
~Sarah
 
Sarah,

You've got that correct. Cstr(numvar) will convert a number to a string.

Now, this is the first time I've seen you refer to initID so I'm thinking your error is not in the Cstr() method, but in the initID variable.

Where are you setting that variable and how ??

Can you provide an example ??

ToddWW
 
The variable is actually intID, not initID. I declare it like any other variable:

<% Dim intID, strQuery %>

And then I pick up the max integer in a column of the database like this:

strQuery = &quot;SELECT MAX(scID) FROM shoppingcart&quot;
intID = rsSC.Execute(strQuery)
Session(&quot;scID&quot;) = Cstr(intID)

Of Course I open the connection and declare the recordset first, but I know all of that code works correctly. I'm not sure if the MAX function works right in the query... Could that be the problem? If so, how would you suggest I get the maximum value from the database?

I was having trouble adding 1 to the maximum value and this could be because the MAX function isn't right...
~Sarah
 
OK, I think you're storing the returned recordset to that variable. I'm assuming that scID is a number type field in your table. Try this.

<%
strQuery = &quot;SELECT MAX(scID) FROM shoppingcart&quot;
rsSC.Execute strQuery

intID = rsSC(&quot;scID&quot;)

'NOW YOU HAVE A NUMBER VALUE SET TO intID
'YOU SHOULD BE ABLE TO DO MATH ON THAT LIKE THIS

intID = intID + 1

'THEN SET YOUR SESSION VARIABLE LIKE THIS

Session(&quot;scID&quot;) = Cstr(intID)


'OR YOU CAN DO MATH DIRECTLY WHEN SETTING THE SESSION VARIABLE LIKE THIS

Session(&quot;scID&quot;) = Cstr(intID + 1)


Let me know specifically what errors or problems you still incurr, if any.

Hope this helps.

ToddWW :)
 
Hi Todd,
The following code gives me an error that says &quot;Item cannot be found in the collection corresponding to the requested name or ordinal,&quot; and refers to the line with the *.

<% set rsSCID = Server.CreateObject(&quot;ADODB.Recordset&quot;) %>
<% rsSCID.Open &quot;SHOPPINGCART&quot;, connCW, 1, 3 %>
<% strQuery = &quot;SELECT MAX(SCID) FROM SHOPPINGCART&quot; %>
<% rsSCID=connCW.Execute(strQuery)%>
*<% intID = rsSCID(&quot;SCID&quot;)%>
<% Session(&quot;scID&quot;) = CStr(intID) %>

I checked the database and there is a field with the title SCID and I even checked other fields and they don't work either. Let me know what you think...
~Sarah
 
Hi Sarah,

OK, couple of things here.

#1: Make it a practice to surround your field references in quotes like this..

<% Session(&quot;scID&quot;) = CStr(&quot;intID&quot;) %>


Although excluding the quotes may work from time to time, there will definitely be other times when ASP will try to treat that as a variable instead of a fieldname and if you have Option Explicit in your ASP page, it will return an error that the variable is undefined.

Now, I don't use the MAX function in my application so I had to run a test on my server. I got the same error that you did and was able to correct it. Try this.

<% set rsSCID = Server.CreateObject(&quot;ADODB.Recordset&quot;) %>
<% rsSCID.Open &quot;SHOPPINGCART&quot;, connCW, 1, 3 %>
<% strQuery = &quot;SELECT MAX(SCID) AS MAXSCID FROM SHOPPINGCART&quot; %>
<% rsSCID=connCW.Execute(strQuery)%>
<% intID = rsSCID(&quot;MAXSCID&quot;)%>
<% Session(&quot;scID&quot;) = CStr(intID) %>


That should work fine. :)

ToddWW
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top