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 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
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 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%>">