Archive for tag: Classic ASP

ASP Page body sample

<%@ Language="VBScript"%>
<%Option Explicit%>
<%
With Response
.Buffer=true
.Expires=0
.Clear
.CharSet="utf-8"
End With
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled ASP PAGE</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body {
background-color: #FFFFFF;
font-family: Verdana, Geneva, Arial, Tahoma, sans-serif;
font-size: 80%;
}
</style>
</head>
<body>
</body>
</html>

Loop Session Variables

Sample showing how to loop through session variables.

<%
    For Each Item In Session.Contents
        Response.Write Item & " = " & Session(Item)
        Response.Write "<br />"     
    Next
%>

CDO Mail

Example showing how to send mail with Classic ASP

<%
Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort = 2
Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" Const cdoBasic = 1
Const cdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword"

Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields

' Get a handle on the config object and it's fields
Set objConfig = Server.CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields

' Set config fields we care about
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "smtp_server_name"
.Item(cdoSMTPServerPort) = 25
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic
.Item(cdoSendUserName) = "username"
.Item(cdoSendPassword) = "password"
.Update
End With

Set objMessage = Server.CreateObject("CDO.Message")
Set objMessage.Configuration = objConfig
With objMessage
.To = "Display Name <email_address>"
.From = "Display Name <email_address>"
.Subject = "SMTP Relay Test"
.TextBody = "SMTP Relay Test Sent @ " & Now()
.Send
End With

Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
%>

Classic ASP and databases

Connect and get data from a database using Classic ASP

<%@ Language = VBScript%>
<%Option Explicit%>
<!-- Place any includefile here, example: <!--#INCLUDE FILE="filename.asp"--> -->
<%
With Response
.Buffer=true
.Expires=0
.Clear
End With

'**********************************************************************
' *** Sample of how to connect to a database in MS SQL server ***
' *** with classic ASP ***
'**********************************************************************


'**********************************************************************
'*** Declare ADO constants ********************************************
'**********************************************************************
Const adOpenStatic = 3
Const adLockReadOnly = 1
Const adCmdText = &H0001
Const adUseClient = 3
Const adOpenKeyset = 1
Const adLockPessimistic = 2



'**********************************************************************
'*** Declare Variables ************************************************
'**********************************************************************
Dim strServer ' Database server
Dim strDatabase ' Database
Dim strUserId ' UserId in database
Dim strPassword ' Password in database
Dim strConn ' Connection string
Dim oRs ' ADODB Recordset
Dim sSql ' Sql select string
Dim Conn ' Connection Object

'**********************************************************************
'*** Fill Variables ***************************************************
'**********************************************************************
strServer = "DBServer" ' Database server
strDatabase = "DBName" ' The database
strUserId = "TheUserName" ' Username in database
strPassword = "ThePassWord" ' Password in database


'**********************************************************************
'*** Create connection object and connect to database *****************
'**********************************************************************
Set Conn = Server.CreateObject("ADODB.Connection")
strConn = "Provider=SQLOLEDB; Data Source = " & strServer & "; Initial Catalog = " & strDatabase & "; User Id= " & strUserId & "; Password = " & strPassword & ";"
' Access
'strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = " & Server.MapPath(acc.mdb)
' mysql
'strConn = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & strServer & ";DATABASE=" & strDatabase & ";UID=" & strUserId & ";PWD=" & strPassword & ";OPTION=3;"
'--- Open ---
Conn.Open strConn


'**********************************************************************
'*** Write the SQL select string **************************************
'**********************************************************************
sSql = "SELECT LastName,FirstName "_
& "FROM Employees "_
& "ORDER BY LastName"
' ---Debug---
'Response.Write(sSql)


'**********************************************************************
'*** Create and open the RecordSet ************************************
'**********************************************************************
Set oRs = Server.CreateObject("ADODB.Recordset")
oRs.CursorLocation = adUseClient
oRs.Open sSql, Conn, adOpenStatic, adLockReadOnly, adCmdText ' Read
'oRs.Open sSql, Conn, adOpenKeyset, adLockPessimistic, adCmdText ' Add, Delete, Update



'**********************************************************************
'*** Do something with the data we have in the RecordSet **************
'**********************************************************************
Response.Write("RecordCount: " & oRs.RecordCount & "<br>")
Response.Write("Number of Col: " & oRs.Fields.Count & "<br><br>")
Do While NOT oRs.EOF
Response.Write(oRs(0) & " - " & oRs(1) & "<br>")
oRs.MoveNext
Loop
' --- Some tests ---------
oRs.MoveFirst
oRs.Filter="Lastname='King'"
Response.Write("RecordCount: " & oRs.RecordCount & "<br>")


'**********************************************************************
'*** Close and cleanup ************************************
'**********************************************************************
oRs.Close
Set oRs = Nothing
Conn.Close
Set Conn = Nothing
%>

Alternating row colors

 

<style type="text/css">
.color0 {  background-color: #E3E3E3; }
.color1 {  background-color: #F4F4F4; }
</style>

<%
Dim i
i = 0
do until objs.EOF
Response.Write("<tr class=""color" & i & """>")
for each x in objs.Fields
Response.Write("<td>" & x.value & "</td>")
next
Response.Write("</tr>")
i = 1 - i
objs.MoveNext
loop
%>

Error handling

Error handling in Classic ASP / VBScript

'Turn on error handling
On Error Resume Next
'Catch error
If Err.Number <> 0 Then
Response.Write(Err.Description)
End If
'Clear error variable
Err.Clear
'Turn off error handling
On Error GoTo 0

Loop ServerVariables

Example in Classic ASP showing how to loop Request.ServerVariables

<% Dim strKey %>
<table border="1">
   <tr>
      <td><b>Server Variable</b></td>
      <td><b>Value</b></td>
   </tr>
   <% For Each strKey In Request.ServerVariables %>
   <tr>
      <td><%= strKey %></td>
      <td><%= Request.ServerVariables(strKey) %></td>
   </tr>
   <% Next %>
</table>

Loop Request

Looping throug the request.form collection

    for x = 1 to Request.Form.count()
        Response.Write(Request.Form.key(x) & " = ")
        Response.Write(Request.Form.item(x) & "<br />")
    next