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

ASP basics for beginners

__General FAQ__

ASP basics for beginners

by  onpnt  Posted    (Edited  )
This is a list of commonly asked questions in the ASP Forum at Tek-Tips.
Below you will find topics such as why the msgbox function does not work
in ASP and the answers to type mismatches in SQL statements. Although
many may seem to be simple questions, they are asked with great
frequency and we felt that supplying a central set of answers would not
only help people with questions they have now, but perhaps with
questions they would have in the future.

We are also hoping for this FAQ to become a direction to point a beginner in ASP
to. Many of these in the beginning may seem like high hurdles to overcome
but with help from a more advanced developer can be errors overcome quickly.

Please feel free to respond via the link below if you have questions you
feel should be addressed. I expect as the weeks go by we will be adding
many more to it in an effort to keep the repetition in the forum to a
minimum.

[color red]Reference 1[/color]
Q: [color blue]Why will my ASP pages not work?[/color]
A: First, you need to run all ASP pages under server support. eg IIS, PWS or on your hosting services server. Second, you need to save all pages containing ASP scripting
with a .asp extension. Third, is all ASP code enclosed within <% %> tags.
Lastly, if you are running IIS and you still receive nothing when running your pages, there is a IIS forum that can better assist in this question.

[color red]Reference 2[/color]
Q: [color blue]Why won't my message box display?[/color]
A: ASP is server-side scripting which means it executes on the server. Thus it does not have access to client-side controls like the message box.

[color red]Reference 3[/color]
Q: [color blue]I have a javascript function (or client-side VBScript function) and I am trying to call an ASP function and it isn't working, why not?[/color]
A: ASP is server-side scripting. When the script executes it starts building the html file it will be sending to the browser. When the script finishes it finishes sending the file and then exits. At this point the client browser finishes receiving the file and the body onLoad event fires, so the ASP function no longer exists by the time javascript has started running (on the client).

[color red]Reference 4[/color]
Q: [color blue]How do I set a javascript variable equal to the value of an ASP variable?[/color]
A: Using Response.Write you can write the value of the variable into your javascript section like so:

<%
'asp section
Dim a, b
a = 1
b = "One"
%>
<script language="JavaScript">
<!--
//assign javascript variables from asp variables
var myAspNum = <%=a%>;
var myAspStr = "<%=b%>";
//-->
</script>

[color red]Reference 5[/color]
Q: [color blue]How do I use the value a client types into a form in my ASP script?[/color]
A: In order for the ASP script to use the value you must submit the form to an ASP page. At that point the variables are sent as part of the Request Collection and can then be accessed by the ASP code. You can't access them from ASP code on the same page because that code generated the page and exited long before your client had time to type anything in the form.

[color red]Reference 6[/color]
Q: [color blue]Could you write this code for me?[/color]
A: No. This forum exists to discuss ASP and to help solve problems, not to do someones work for them.

[color red]Reference 7[/color]
Q: [color blue]How do I add the value of a variable to a string?[/color]
A: Not to difficult, all you have to do is use the & like so:
<%
Dim myString, myVar
myVar = "whatever"
myString = "the value if my var is: " & myVar
%>

[color red]Reference 8[/color]
Q: [color blue]How do I add the value of a variable to a string SQL statement?[/color]
A: This is very easy to do. All you need to remember is the syntax for the data type of the variable.
<%
Dim myString, myNumber, myDate, sql
myNumber = 2
myString = "This is a string"
myDate = now()

' to use them you need to enclose as follows
'Numeric values do not get enclosed in anything in the SQL string

sql = "INSERT INTO tbl(value) "
sql = sql & "values(" & myNumber & ")"

output = INSERT INTO tbl(value) values(2)

'String values need to be surrounded by single quotes

sql = "INSERT INTO tbl(value) "
sql = sql1 & "values('" & myString & "')"

output = INSERT INTO tbl(value) values('This is a string')

'In MS Access date fields are surrounded by #'s, in most other databases they are surrounded by single quotes

sql = "INSERT INTO tbl(value) "
sql = sql & "values(#" & myDate & "#)"

output = INSERT INTO tbl(value) values(#2/17/03 3:07:23 PM#)

%>

[color red]Reference 9[/color]
Q: [color blue]How do I pass the value of a variable to the next page?[/color]
A: Many options here. You could put it into a hidden input field, add it to the querystring of an address, put it in a session variable, put it in a cookie.

[color red]Reference 10[/color]
Q: [color blue]This SQL statement isn't working... ?[/color]
A:
1) Try Response.Write'ing it to the screen like so:
<%
Response.Write MySqlString
Response.End
%>

2) If any of the variables your adding are blank, then that means they aren't being passed correctly from the previous page.
3) If that doesn't solve it copy it into your database and try to execute it.
4) If you don't understand the error from your database execute then go to your database forum and post questions
eg: access is the database. Got to Microsoft: Access Queries and JET SQL Forum forum701 and submit a question
5) If that works without giving you an error, than try renaming fields like "username" and "password" because most likely you have used a reserved word. Another option is to surround the word in square brackets to force the database to treat it as a field name.

[color red]Reference 11[/color]
Q: [color blue]I am passing two|three|many fields from a form with the same name, how do I get the individual values?[/color]
A: You can either use a for each statement or the split command to split those values into an array. When the form sends the information it is sent as one variable with a lot of comma delimited values. Here is an example:
<%
'pretend we have a few text inputs on previous page called txtSample
Dim myArray
myArray = Split(Request.Form("txtSample"),", ")
%>
Notice we split on comma-space. The form uses comma-space for who knows what reason. We want to get rid of that extra space as well so we split on comma-space.

[color red]Reference 12[/color]
Q: [color blue]My SQL statement breaks when someone puts a single quote in my form[/color]
A: You should always replace single quotes before putting values from a form into an SQL statement, otherwise your user John O'Malley is going to have problems or you could fall prey to an SQL injection attack.
<%
Dim myString
myString = Replace(Request.Form("txtSample"),"'","'")
%>
The double single quotes makes it so the server doesn't think your string is finished, but will still input a single single quote into the entry your adding/comparing to.

[color red]Reference 13[/color]
Q: [color blue]I'm trying to _______________ in the clients browser.[/color]
A: Generally that blank has something in it concerning javascript. There is a seperate forum for that.

[color red]Reference 14[/color]
Q: [color blue]What's the syntax for a loop?[/color]
A: Search for examples of "for next" "do loop" loops on the web. here are two basic loop structures.
syntax
<%
dim myArray, x, contents myArray = Array("one","two","three")

'for next loop
for x = 0 to UBound(myArray)
contents = myArray(x)
response.write myArray(x)
Next

'do while loop
x = 0
Do While x <= UBound(myArray)
contents = myArray(x)
response.write myArray(x)
x = x + 1
Loop
%>

[color red]Reference 15[/color]
Q: [color blue]Why are my ASP variables not being submitted with my form when I insert them into input's?[/color]
A: More then likely you need to surround your ASP variables or Request objects with quotes when inserting them into form
fields
<input type="hidden" name="txt" value="<%=var%>">
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top