ACTIVE SERVER PAGES TIPS

Tips >>
 
What are Active Server Pages?
Displaying Date, Time and Text
Using Variables and Forms
If...Then and For...Next instructions
Do...Loop and Select...Case instructions
Subroutines and Include/virtual
Session and Application methods
Dictionary Object
Cookies
Open Read and Create files
Introduction to Global.ASA
Using ASP and javascript together
Using Arrays
Displaying pictures from an ASP file
Active Server Pages Server-Side Scripting Programmer's Reference
Define ASP?
ASP Functions
ASP with Database
Errors
Tables
Include File
Recordset
Glossary
Forms
SOME COMMON TIPS
To write the Euro character to a browser, use this code :-

Example :-

Response.Write "€"

Note :- This will work for any Unicode character as well.


Using ASP it is possible to find out the age of a given file by
employing the following short snippet using FileSystemObject.

Example :-

Code :-
<%
set fso = server.createobject("scripting.FileSystemObject")
set thisfile = fso.GetFile("c:\myfile.txt")
'ensure that myfile.txt is available in c:\ of the machine
dtcreated = thisfile.DateCreated
Response.write "The given file is " & Int(now() - dtCreated) & " days old"
' note that the above mentioned snippet gives the age of the file in days
%>


In ASP we can create random file and folder names by employing the GetTempName method of FileSystemObject.

The Syntax for using GetTempName Method :-

tmpname = fs.GetTempName

Where, fs stands for FileSystemObject

The following piece of code Demonstrates the method of using the above method for creating Temporary folder and file names :-

Example :-

Code :-
<%
set fso = server.createobject("scripting.FileSystemObject")
tempfolder = fso.GetTempName
tempfile = fso.buildpath(tempfolder, fso.GetTempName)
set txtstream = fso.CreateTextFile tempfile

'processing for the above created file goes here
txtstream.close
fso.deletefolder tempfolder
set txtstream = nothing
set fso = nothing
%>


In ASP, FileSystemObject can be employed to find out whether a given file is read-only or not.

The snippet demonstrates how to go about finding whether a given file is read-only or not.

Example :-

Code :-
<%
set fso = server.createobject("scripting.FileSystemObject")
set thisfile = fso.GetFile(filename)
if thisfile.Attributes and 1 then
Response.write " The File Is Read-only"
else
Response.write " The File Is Not Read-only"
end if

set thisfile = nothing
set fso = nothing
%>

 


A parameter object can be created and initialized on the fly by employing the following ASP Script.

Example :-

Code :-
<%
'create command object
set cmd = server.createobject("ADODB.Command")

set myparam = cmd.createparameter
myparam.name = "param1"
myparam.Type = adInteger
myparam.direction = adParamDirectionInput
myparam.value = 1000
%>

 


The following short ASP snippet demonstrates a method of retrieving files in the desktop of the machine hosting the ASP file.

Example :-

Code :-
<%
set fso = server.createobject("scripting.FileSystemObject")
dsktopfoldername = fso.buildpath(fso.GetSpecialFolder(0), "Desktop")
set desktopfolder = fso.GetFolder(desktopfoldername)
set desktopfiles = desktopfolder.GetFiles

'desktopfiles holds all the files available in the desktop folder of the Host Machine
%>

 



Sometimes, during debugging, it is useful to have a file where we can see what we get from a form.

One way of doing this is as follows :-

Code :-
<% Option Explicit %>
<html>
<!-- head tag -->
<body>
<%

dim fld

response.write "Result from: " & request.serverVariables("HTTP_REFERER")

response.write "<table>"
response.write "<tr><td>Field</td><td>Value</td></tr>"

for each fld in request.form
response.write "<tr><td>" & fld & "</td><td>" &
request.form(fld) & "</td></tr>"

next 'fld

response.write "</table>"

%>
</body>
</html>




You can use the colon character ':' to make your ASP code more readable. ':' allows to put two or more statements on the same line. This is especially useful in variable declarations.

Example :-

Code :-
dim strBlah : strBlah='hello' 'this is an example variable declaration

Declaring a variable and assigning it a value on the same line has an additional benefit of reminding you to delete dim statement in case you decide to get rid of the variable.


Nested conditional statements can get very hard to debug. The easiest way to track them is to add a comment to the end of the end statement which would tell the coder how the statement started.

Example :-

Code :-
if strBlah="hello" then

else 'if strBlah="hello" then

end if 'if strBlah="hello" then

This way, in order to find the beginning of the statement the coder needs to search for "if strBlah="hello" then" higher up in the code.

It is very important not to forget to change the comments if the condition changes. An alternative is to enumerate if statements with unique integers or strings.


It is possible to use GET and POST form methods at the same time.

Example :-

Code :-
in form.asp:

<form action="/hello.asp?foo=true" method="POST">
<input type="text" name="bar"></input>
<input type="submit">
</form>

hello.asp :

<%
dim strFooValue, strBarValue
strFooValue=request.querystring("foo")
strBarValue=request.form("bar")
%>

Note :- Although the submission method of the form is "POST", the GET variables are also being propagated.

This is useful in case you want to preserve some querystring flag that is used on the other parts of the site while submitting a POST form.


There is a little known danger in looping through recordsets.

Example :-

Code :-
'rsFoo is a recordset
on error resume next

do while not rsFoo.eof
response.write rsFoo("bar")
rsFoo.movenext
loop

What would happen if rsFoo did not initialize correctly (or even was not declared)?
The answer is -- there will be an infinite loop, and the page will never stop loading.

If you will try to catch the error like this :-

Code :-
do while not rsFoo.eof and err.number=0
response.write rsFoo("bar")
rsFoo.movenext
loop

The loop will not break because the error will happen on the statement
"rsFoo.eof" and the interpreter will immediately go to the next line.

To successfully quit the loop you need to do this :-

Code :-
err.clear 'make sure that code above does not trigger loop exit
do while not rsFoo.eof
if err.number<>0 then
exit do
end if
response.write rsFoo("bar")
rsFoo.movenext
loop

 



Coding forms may prove very frustrating. The biggest problem is making calls to variables in form or querystring collections without any types.

Example :-

Code :-
<input type="text" name="hello">
request.form("helo")

The typo that you made will not cause an error, but return an empty string. Besides, if you have a lot of variables, this will call for a lot of copying and pasting.

I solved all of these problems with a simple piece of code that actually writes code for me.

Example :-

Lets say you have a form in a file called foo.asp.

Code :-
<form method="POST" action="bar.asp">
<input type="text" name="foo">
<input type="text" name="bar">
<input type="submit">
</form>

And the following code in a file called bar.asp.

Example :-

Code :-
<%
for each strName in request.form
%>

dim str<%=strName%> : str<%=strName%> =
request.form("<%=strName%>")<br>

<%
next
%>

The output of bar.asp after the form is submitted will be :-

Code :-
dim strfoo : strfoo = request.form("foo")
dim strbar : strbar = request.form("bar")

-- the asp code that is necessary to load foo and bar values into ASP variables.



Is 1 > 2? Well, in ASP sometimes it can be.

Example :-

Code :-
<%
foo="1"
bar=2

if foo > bar then
response.write (foo&">"&bar)
else
response.write (foo&"<"&bar)
end if

%>

Well, variable foo is a string and bar is an integer.
In order to be able to use ">" operator correctly you need to cast them both as integers like this :-

Code :-
<%
foo="1"
bar=2

if cint(foo) > cint(bar) then
response.write (foo&">"&bar)
else
response.write (foo&"<"&bar)
end if

%>

This is especially important when you are getting an integer value from a form (which will default to a string) and are trying to compare it to some integer.



To list all Windows Shortcut File information from ASP.

Function ShowShortcutFile(vComputerName)

Dim objLocator, objService, objWEBMCol, objWEBM

Set objLocator = CreateObject("WbemScripting.SWbemLocator")

Example :-

Code :-
'Establish a connection to WMI
If isEmpty(vComputerName) = True then
Set objService = objLocator.ConnectServer
Else
Set objService = objLocator.ConnectServer(vComputerName)
End If

Example :-

Code :-
'Get the Webm Service object
Set objWEBMCol = objService.InstancesOf("Win32_ShortcutFile")

Response.write "<H2> Windows Shortcut File information:</H2><HR><UL>"
'Enumerate
For Each objWEBM in objWEBMCol
Response.write "<LI>Name: " & objWEBM.Name & _
", <BR>Description: " & objWEBM.Description & _
", <BR>Caption: " & objWEBM.Caption & _
", <BR>Access Mask: " & objWEBM.AccessMask & _
", <BR>Compressed: " & objWEBM.Compressed & _
", <BR>Compression Method: " & objWEBM.CompressionMethod & _
", <BR>Encrypted: " & objWEBM.Encrypted & _
", <BR>Encryption Method: " & objWEBM.EncryptionMethod & _
", <BR>Drive: " & objWEBM.Drive & _
", <BR>File Name: " & objWEBM.FileName & _
", <BR>Extension: " & objWEBM.Extension & _
", <BR>Path: " & objWEBM.Path & _
", <BR>DOS File Name: " & objWEBM.EightDotThreeFileName & _
", <BR>File Size: " & objWEBM.FileSize & _
", <BR>File Type: " & objWEBM.FileType & _
", <BR>File In Use Count: " & objWEBM.InUseCount & _
", <BR>File Link Target: " & objWEBM.Target & _
", <BR>Version: " & objWEBM.Version & "<BR></LI>"
Response.write "</UL>"
'Clean up :-
Set objLocator = Nothing
Set objService = Nothing
Set objWEBMCol = Nothing
Set objWEBM = Nothing

Note :- Pass the computer name as the parameter. An empty string will work when you are accessing the local computer.



One of the quickest ways to create an XML document is to use the Microsoft added method "LOAD" in the XML DOM.

Example :-

Code :-
var XMLDoc = Server.CreateObject("MSXML2.DOMDocument")
XMLDoc .async = false
XMLDoc .load("http://ASPToday/xmlfile.xml")

 



Normally you would create an XML data island with the <XML></XML> tags.
However, you can use the script tag to create an XML island as well :-

Example :-

Code :-
<SCRIPT LANGUAGE="xml" ID="XMLID">
<STUDENT>
<NAME>Peter SMith</NAME>
<ID>4456</ID>
</STUDENT>
</SCRIPT>

 


This code can be used to display text from a file.

Example :-

Code :-
<%
Function ReadFile(psfile)
Dim Fs
Dim ts
Dim sFile
Set Fs = CreateObject("Scripting.FileSystemObject")
Set Ts = fs.OpenTextFile(psFile)
sFile = ts.ReadAll
ts.Close
Set Ts = Nothing
Set Fs = Nothing
ReadFile = sFile
End Function

Response.Write ReadFile(Server.MapPath("readme.txt"))
%>

 


Using this code you can write data from a form to a text file.
The function WriteTextFile will create a text file if it does not already exist.

Example :-

Code :-
<%

' Function for creating and appending text file
Function WriteTextFile(strFile, strText)
Const fsoForAppend = 8
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

' Open the text file. Create parameter is true. This will create a
file if it does not exist
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile(strFile, fsoForAppend, True)

' Write the contents to the text file
objTextStream.WriteLine strText

' Close the file and clean up
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing
End Function

Wtext = Request.Form("Email") 'TO DO : Modify the name of the form field
WriteTextFile "d:\email.txt", Wtext 'TO DO : Modify the path of the file.

Response.Redirect "thanks.htm" 'TO DO : Modify the path of the thank you page

%>

 


 

 

E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641