Getting Started
3 snippetsBasic Classic ASP syntax
Hello World
<%@ Language=VBScript %>
<html>
<body>
<%
Response.Write "Hello, World!"
%>
</body>
</html>ASP Delimiters
<%@ Language=VBScript %>
<% ' Server-side code %>
<%= variable %> <%' Output shorthand %>Comments
<% ' Single line comment %>
<%
' Multi-line comment
' can span multiple lines
%>Variables & Data Types
3 snippetsVBScript variable declarations
Variable Declaration
<%
Dim name
Dim age, salary
name = "John Doe"
age = 30
salary = 75000.00
%>Constants
<%
Const TAX_RATE = 0.0825
Const MAX_ITEMS = 100
Const COMPANY_NAME = "ACME Corp"
%>Arrays
<%
Dim colors(2)
colors(0) = "Red"
colors(1) = "Green"
colors(2) = "Blue"
' Dynamic array
Dim items()
ReDim items(5)
%>Control Structures
4 snippetsConditionals and loops
If-Else
<%
If age >= 18 Then
Response.Write "Adult"
ElseIf age >= 13 Then
Response.Write "Teenager"
Else
Response.Write "Child"
End If
%>Select Case
<%
Select Case grade
Case "A"
Response.Write "Excellent"
Case "B"
Response.Write "Good"
Case "C"
Response.Write "Average"
Case Else
Response.Write "Failed"
End Select
%>For Loop
<%
For i = 1 To 10
Response.Write i & "<br>"
Next
%>For Each Loop
<%
Dim colors
colors = Array("Red", "Green", "Blue")
For Each color In colors
Response.Write color & "<br>"
Next
%>Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Request & Response
4 snippetsHandling HTTP requests and responses
Query String
<%
Dim userId
userId = Request.QueryString("id")
Response.Write "User ID: " & userId
%>
<!-- URL: page.asp?id=123 -->Form Data
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
Dim username, password
username = Request.Form("username")
password = Request.Form("password")
Response.Write "Welcome " & username
End If
%>Response Methods
<%
' Write output
Response.Write "Hello"
' Redirect
Response.Redirect "login.asp"
' End processing
Response.End
%>Cookies
<%
' Set cookie
Response.Cookies("username") = "JohnDoe"
Response.Cookies("username").Expires = DateAdd("d", 30, Now())
' Read cookie
Dim user
user = Request.Cookies("username")
%>Session & Application
3 snippetsState management objects
Session Variables
<%
' Set session variable
Session("username") = "JohnDoe"
Session("userId") = 1001
' Get session variable
Dim user
user = Session("username")
' Check if exists
If IsEmpty(Session("userId")) Then
Response.Redirect "login.asp"
End If
%>Application Variables
<%
' Set application variable (global)
Application.Lock
Application("totalVisits") = Application("totalVisits") + 1
Application.Unlock
' Read application variable
Response.Write "Total Visits: " & Application("totalVisits")
%>Session Timeout
<%
' Set session timeout to 30 minutes
Session.Timeout = 30
' Abandon session
Session.Abandon
%>Database Access (ADO)
3 snippetsActiveX Data Objects
Connect & Query
<%
Dim conn, rs, sql
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=MyDB;User ID=sa;Password=pass;"
sql = "SELECT * FROM Customers WHERE Country='USA'"
Set rs = conn.Execute(sql)
Do While Not rs.EOF
Response.Write rs("Name") & "<br>"
rs.MoveNext
Loop
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
%>Parameterized Query
<%
Dim conn, cmd, rs
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=MyDSN"
Set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = "SELECT * FROM Users WHERE UserID = ?"
cmd.Parameters.Append cmd.CreateParameter("@UserID", 3, 1, , 123)
Set rs = cmd.Execute
%>Insert Record
<%
Dim conn, sql
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=MyDSN"
sql = "INSERT INTO Customers (Name, Email) VALUES ('John Doe', 'john@example.com')"
conn.Execute sql
conn.Close
Set conn = Nothing
%>Server Objects
2 snippetsServer-side object creation
Server.CreateObject
<%
' File System Object
Dim fso, file
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile(Server.MapPath("data.txt"), 1)
' Dictionary Object
Dim dict
Set dict = Server.CreateObject("Scripting.Dictionary")
dict.Add "name", "John"
dict.Add "age", 30
%>Server Properties
<%
' Map virtual path to physical
Dim physicalPath
physicalPath = Server.MapPath("/images/logo.png")
' HTML encode
Dim encoded
encoded = Server.HTMLEncode("<script>alert('xss')</script>")
' URL encode
Dim urlEncoded
urlEncoded = Server.URLEncode("hello world")
%>Server-Side Includes
2 snippetsReusing code with includes
Include Files
<!-- #include file="header.asp" -->
<!-- #include virtual="/includes/footer.asp" -->
<%
' File: relative path from current file
' Virtual: relative to site root
%>Modular Code
<!-- functions.asp -->
<%
Function CalculateTax(amount)
CalculateTax = amount * 0.0825
End Function
%>
<!-- main.asp -->
<!-- #include file="functions.asp" -->
<%
Dim total
total = CalculateTax(100)
Response.Write total
%>