INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- Turn Off Ad Banners
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Member Feedback
"...Keep up the good work - excellent site - i'd been looking for something like this for ages !..."
Geography
Where in the world do Tek-Tips members come from?
|
Microsoft: Active Server Pages (ASP) FAQ
|
__General FAQ__
|
ASP basics for beginners
Posted: 20 Jan 03 (Edited 12 Mar 03)
|
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.
Reference 1 Q: Why will my ASP pages not work? 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.
Reference 2 Q: Why won't my message box display? 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.
Reference 3 Q: 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? 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).
Reference 4 Q: How do I set a javascript variable equal to the value of an ASP variable? 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>
Reference 5 Q: How do I use the value a client types into a form in my ASP script? 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.
Reference 6 Q: Could you write this code for me? A: No. This forum exists to discuss ASP and to help solve problems, not to do someones work for them.
Reference 7 Q: How do I add the value of a variable to a string? 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 %>
Reference 8 Q: How do I add the value of a variable to a string SQL statement? 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#)
%>
Reference 9 Q: How do I pass the value of a variable to the next page? 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.
Reference 10 Q: This SQL statement isn't working... ? 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.
Reference 11 Q: I am passing two|three|many fields from a form with the same name, how do I get the individual values? 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.
Reference 12 Q: My SQL statement breaks when someone puts a single quote in my form 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.
Reference 13 Q: I'm trying to _______________ in the clients browser. A: Generally that blank has something in it concerning javascript. There is a seperate forum for that.
Reference 14 Q: What's the syntax for a loop? 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 %>
Reference 15 Q: Why are my ASP variables not being submitted with my form when I insert them into input's? 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%>"> |
Back to Microsoft: Active Server Pages (ASP) FAQ Index
Back to Microsoft: Active Server Pages (ASP) Forum
My FAQ Archive
Email This FAQ To A Friend |
|
 |
|