|
Active Server
Pages Tutor for Beginners
Introduction
ASP is Microsoft’s most recent web server technology that makes it easier to
develop database interactive web applications. It is a server side scripting language. It
allows you to combine HTML with any scripting language such as VBScript and Jscript.
Further it also allows you to create database applications that involve complex
calculations.
Benefits Of Using ASP
*ASP development is easy to learn if you have knowledge of VBScript or Jscript or VB you
can get full advantage of ASP. ASP allows you to write HTML and VBScript or JScript in a
single file.
*ASP helps in creating Web Interface to any ODBC compliant database. You can create user
interface for any ODBC compliant database such as SQL Server, MS-Access.
*ASP development is Compile free. Prior to ASP, development of any database interactive
applications required pre-compilation of an executable file using any application
development environment such as VC++. Even a slightest change to the application required
re-compilation of the whole application again. But ASP provides us a very easy way to
create web applications. In ASP you need not to compile the application. You just save the
file with .asp extension. The ASP dll executes the file itself.
*The ASP environment is extensible. It provides you various tools for developing Database
Interactive Web Applications. You can also create custom control using ASP.
Requirements for developing ASP applications
The minimum software and hardware requirements for developing ASP applications are:
*A Pentium based computer
*32MB of RAM
*About 100 MB of free drive space
*Windows NT server 4.0 with TCP/IP networking support .
*Internet Information Server 3.0 or better if you have Win NT Server. Microsoft Personal
Web Server if you have Win 95 or Win 98 Microsoft Peer Web Services if you have Win NT
Workstation
*A database that supports ODBC
*Microsoft Visual Interdev is recommended but not required
The technical requirements for developing ASPs are
*Familiarity with Win NT 4 and Win NT security and NTFS
*Familiarity with VB or a scripting language such as VBScript or Jscript for getting full
advantage of ASP but is not essential.
ASP Objects
Response Object
Sends text, data and cookies to the browser and control each stage of transmitting the
page.
Server Object
Creates COM objects, some conversion facilities and overall scripting control. Request
Object Reads submitted form data, cookies and server variables.
Session Object
Allows you to attach data to a specific user browsing your site that is isolated and
invisible to other users.
Application Object
Allows you to manipulate global data in your script that will be visible to all users
browsing the site or your script code.
Syntax of ASP Applications
*HTML code
*Script delimiters
*Script code
*ActiveX component
*ASP objects
Example
Different types of loops
You can use various types of loops in your ASP file depending upon the requirements.
To execute a more than one statement more than once ASP provides the following loops: DO,
LOOP
WHILE, WEND
Either of these statements can be followed by UNTIL or WHILE.
DO UNTIL
.....statements to be repeated...
LOOP
DO
.....statements to be repeated...
LOOP UNTIL
IF, at the beginning of the repetitions you know how many repetitions, then use the
For...Next statement. If you do not know how many repetitions you want, then use the Do
Loop structure.
FOR
NEXT
For
.....statements to be repeated...
Next
The loop starts with the first number and counts the number of loops up to the the last
number. After it gets to the last number the repetitions will stop and the execution of
the code jumps to below the NEXT line. There is the possibility that your loop will never
end. To protect yourself use an escape line within an If..Then statement, such as:
If counter>100 then
exit For
End If
SELECT
.CASE
Select case Case
Statement to be executed
Case
.Statement to be executed
End Select
Example
For Loop Example
This example counts the length of word("Hello World") and uses "for..
loop" for creating eleven tables with two columns each.
<%@ Language=VBScript %>
<HTML>
<HEAD></HEAD>
<BODY>
<font size=4 face=arial>
<%
Greeting ="Hello world"
for rowcount =1 to len(greeting)
%>
<table>
<tr>
<td bgcolor=blue width=<%=(rowcount*20)%>></td>
<td><%=left(greeting,1)%></td></tr>
</table>
<%
greeting=right(greeting,len(greeting)-1)
next
%>
</BODY>
</HTML>
If Loop Example
This example uses For..Loop and If...Endif loops both .It displays "I is not equal to
j " four times in bleu color and "I is not equal to j" in red colour.
<%@ Language=VBScript %>
<HTML>
<HEAD>
</HEAD>
<BODY>
<%
j=5
for i=1 to 5
if i=j then
Response.Write "<p><font size=5 color=red>I is equal to
j</font></p>"
else
Response.Write "<p><font color=blue size=5>I is not equal to j
</font></p>"
end if
next
%>
</BODY>
</HTML>
Select Case Example
This example uses two files (Form1.asp, Form2.asp). Form1 displays two
textboxes("first Name", "Last Name"), two buttons ("Reset",
"Submit") and radio buttons for categories. When you click on "Submit"
it displays different messages according to your category.
<!--Form1.asp-->
<html>
<head>
<TITLE></TITLE>
</head><body bgcolor="Blue">
<form action="form2.asp" method=get>
Your First Name<INPUT NAME="FirstName" MaxLength=20><p>
Your Last Name<INPUT NAME="LastName" MaxLength=20><p>
Your Title
<INPUT TYPE="Radio" name="Title"
VALUE="employee">Entry Level
<INPUT TYPE="Radio" name="Title" VALUE="temp"
CHECKED>Temporary Employee
<INPUT TYPE="Radio" name="Title"
VALUE="manager">Management Candidate
<INPUT TYPE="Radio" name="Title"
VALUE="executive">Executive
<INPUT TYPE="Radio" name="Title" VALUE="vice-prez">The
Vice President of...
<INPUT TYPE="Radio" name="Title" VALUE="CEO">The
Boss<p>
<INPUT TYPE=submit id=submit1 name=submit1><p><INPUT TYPE=reset id=reset1
name=reset1><P><FONT face="" size=4>
</form>
</body>
</html>
Data Types
VBScript does not require or allow you to specify data types. It is possible to examine a
variables data type.
VBScript supports following data types: Boolean, Byte, Currency, Date, Decimal, Double,
Empty, Error, Integer, Long, Nothing, Null, Object*, Single, String and Unknown. The
TYPENAME function allows you to determine what type of data a variable is * TYPENAME also
returns specific object types like recordset, command, etc. It only returns Object when
confronted with a generic object. VARTYPE returns a number corresponding to the type of
value stored in a variable. The VarType function returns the following values:
Constant Value Description
VbEmpty
0
Empty (uninitialized)
VbNull
1
Null (no valid data)
VbInteger
2
Integer
VbLong
3
Long integer
VbSingle
4
Single-precision floating-point number
VbDouble
5
Double-precision floating-point number
VbCurrency
6
Currency
VbDate
7
Date
VbString
8
String
VbObject
9
Automation object
VbError
10
Error
VbBoolean
11
Boolean
VbVariant
12 Variant (used only with arrays of Variants)
VbDataObject
13
A data-access object
VbByte
17
Byte
VbArray
8192
Array
Convertion Functions
You can use the following data conversion functions
Convert Function
Data Type
CSTR
string
Cbool
boolean
Cbyte
byte
Ccur
currency
Cdate
date
CDbl
double
Cint
integer
CLng
long
CSng
single
Various methods to obtain user input:
*The InputBox function
It is VBScript function and allows you to accept input from users. Syntax of InputBox is:
InputBox(<Prompt>,<Title>,<Default>,<x>,<y>)
Prompt is the Information or Message given by user
Title is the title of InputBox
Default is the default value given to InputBox
X and y are position coordinates
*The MsgBox function
It is also one VBScript function and allows you to display messages.
Syntax of MsgBox is:
MsgBox(<Prompt>,<Title>,<ButtonStyle>)
Prompt is the Message displayed by user
Title is the title of Message Box
ButtonStyle is an integer value that shows various patterns of buttons e.g. ButtonStyle=0
displays only OK button ButtonStyle=1 displays OK and
Cancel button
*Data Entry objects of HTML forms
You can use all data entry object of HTML forms e.g. TextBox, CheckBox,Radio buttons etc .
*ActiveX controls You can use activeX controls
Example
Creating Forms
We can allow users to fill information using forms. Forms can be in HTML format or ASP
depending on its use for example if you want to add database transactions in your form you
should use ASP form rather than simple HTML form. This is a very simple example that shows
you how to make forms. There are two asp files ( form1.asp and form2.asp) first file
allows user to enter his/her name and second file gets input from first form and displays
Hello .
<%@ Language=VBScript %>
<HTML>
<HEAD>
</HEAD>
<!--This is Form1.asp file-->
<BODY bgcolor=blue>
<font size=4 color=white>
<form name=form1 method=post action=form2.asp>
Please enter your name here</font>
<input type=text name=name id=name width = 100><p>
<input type=submit name=Button1 value="Click Here">
</p>
</form>
</BODY>
</HTML>
<%@ Language=VBScript %>
<HTML>
<HEAD>
</HEAD>
<!--This is Form2.asp -->
<BODY bgcolor=pink>
<form name=form2>
<h1>Hello Dear <%=request.form("name")%> <h1>
</form>
</BODY>
</HTML>
Forms Controls
We can use various form controls for accepting input from users.
These are various form controls with its syntax:
1.TextBox
Allows you to enter text.
Syntax is:
<INPUT id=text1 name=text1>
2.TextArea
Text area is similar to textBox but it allows user to input multiline input.
Syntax is:
<TEXTAREA id=TEXTAREA1 name=TEXTAREA1>
</TEXTAREA>
3.CheckBox
Allows users to select from options. User can select more than one item at a time.
Syntax is:
<INPUT id=checkbox1 name=checkbox1 type=checkbox>
4.Radio Button
Allows users to select from options. User can not select more than one item at a time.
Syntax is:
<INPUT id=radio1
name=radio1 type=radio>
5.DropDown List Box
Shows a listbox that displays one option as default you can drop it down to see othr
options.
Syntax is:
<SELECT id=select1 name=select1>
<OPTION selected></SELECT>
6.List Box
Allows you to select from a list of options .
Syntax is:
<SELECT id=select2 name=select2 size=2>
<OPTION></SELECT>
7.Submit Button
This button allows you to transfer values from one form to another.
Syntax is:
<INPUT id=submit1 name=submit1 type=submit value=Submit>
8.Reset Button
Resets values of all control of a form.
Syntax is:
<INPUT id=reset1 name=reset1 type=reset value=Reset><LABEL></LABEL>
Database Programming
Creating data source connection
For database transactions you can use two type of connections
a connection using DSN
a DSNless connection
A DSN connection requires the server administrator to setup a DSN on the server using the
control panel
DSN connections require a DSN name, user, and password.
This is an example that shows how to create database connection using DSN. In this case
DSN name is "Student" that is created using control panel. The third statement
executes query given in quotes.
set conntemp=server.createobject("adodb.connection")
conntemp.open "DSN=Student; uid=student; pwd=magic"
set rstemp=conntemp.execute("select * from authors")
If we don't have a DSN and we knew the name of the file (i.e. file based databases like
Access, Paradox, FoxPro, etc.) or the name of the data source (SQLserver for example). We
could open a data source without a DSN! Here is an example to open data source without
using DSN.
set conntemp=server.createobject("adodb.connection")
cnpath="DBQ=" & server.mappath("yourtable.mdb")
conntemp.Open "DRIVER={Microsoft Access Driver (*.mdb)}; " & cnpath
set rstemp=conntemp.execute("select * from authors")
- X -