Fixing error "Unable to make the session state request to the session state server."

If you ever received this error message, you would have known true frustration in trying to find out how to start it and from where.
Continue reading “Fixing error "Unable to make the session state request to the session state server."”

Formatting numbers with Classic ASP

Format Numbers – Reference

Frequently you want a number to appear in a certain format. The most commands requests are for a set total number of digits and a set number of digits to the right of the decimal place. Less frequently there is a call for negative amounts displayed in parenthesis or that there should be leading zeros.

The FormatNumber function takes the contents of a number type variable and returns the contents in the specified format.

Syntax: FormatNumber(expression, iDigits, bleadingDigit, bParen, bGroupDigits)

argument meaning
expression the variable holding the raw number
iDigits number of digits to right of decimal point
bleadingDigit 1 for leading zeros
0 for no leading zeros
bParen 1 for parenthesis around negative numbers
0 for no parenthesis around negative numbers
bGroupDigits 1 to display numbers as per regional settings in the Control Panel
0 to over-ride settings in the Control Panel

</head><body bgcolor=”#FFFFFF”>
 My ASP formatting number sample
response.write “<hr>” & mynumber & “<br>”
response.write “formatnumber(mynumber,0)” & “<br>”
response.write formatnumber(mynumber,0) & “<hr>”
response.write “formatnumber(mynumber,2)” & “<br>”
response.write formatnumber(mynumber,2) & “<hr>”
response.write “formatnumber(mynumber,6)” & “<br>”
response.write formatnumber(mynumber,6) & “<hr>”

response.write mynumber & “<br>”
0 means means no leading zeroes
response.write “formatnumber(mynumber,2,0)” & “<br>”
response.write formatnumber(mynumber,2,0) & “<hr>”
1 means means pad with leading zeroes
response.write “formatnumber(mynumber,2,1)” & “<br>”
response.write formatnumber(mynumber,2,1) & “<hr>”

response.write mynumber & “<br>”
0 means means no parentheses for negative numbers
response.write “formatnumber(mynumber,2,0,0)” & “<br>”
response.write formatnumber(mynumber,2,0,0) & “<hr>”
1 means means yes parentheses for negative numbers
response.write “formatnumber(mynumber,2,0,1)” & “<br>”
response.write formatnumber(mynumber,2,0,1) & “<hr>”

Displaying Table Data with Classic ASP

This page demonstrates the capabilities how to display a table from a SQL statement.


<body bgcolor=”#FFFFFF”>
 this code opens the database
set conntemp=server.createobject(“adodb.connection”) myDSN

 this code retrieves the data
mySQL=”select * from publishers where state=NY
set rstemp=conntemp.execute(mySQL)

 this code detects if data is empty
If  rstemp.eof then
response.write “No records matched<br>”
response.write mySQL & “<br>So cannot make table…”
set connection=nothing
end if
<table border=1>
 This code puts fieldnames into column headings
response.write “<tr>”
for each whatever in rstemp.fields
response.write “<td><B>” & & “</B></TD>”
response.write “</tr>”

 Now lets grab all the records
DO  UNTIL rstemp.eof
 put fields into variables
company_name=rstemp(“company name”)

 write the fields to browser
cellstart=”<td align=””top””>”
response.write “<tr>”
response.write cellstart & pubid & “</td>”
response.write cellstart & name & “</td>”
response.write cellstart & company_name & “</td>”
response.write cellstart & address & “</td>”
response.write cellstart & city & “</td>”
response.write cellstart & state & “</td>”
response.write cellstart & zip & “</td>”
response.write cellstart & telephone & “</td>”
response.write cellstart & fax & “</td>”
response.write cellstart & comments & “</td>”

response.write “</tr>”

 Now close and dispose of resources
set rstemp=nothing
set conntemp=nothing


Select Case in Classic ASP

Using IF-THEN can be cumbersome, prone to programmer errors and slower to execute. A more efficient construct is SELECT CASE. It is optimized for testing one variable against many conditions.

</head><body bgcolor="#FFFFFF">
<form action="case2respond.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><p>

Here is the select case that will determine what the form input means.

</head><body bgcolor="#FFFFFF">
response.write "Nice to Hire You " & fname & " " & lname & "<p>"
Select Case lcase(Title)
case "employee","temp"
response.write("The washroom is in the hall")
case "manager","executive"
response.write("Here is your key to the Executive washroom")
case "ceo", "vice-prez"
response.write("The maid will attend to your private washroom")
End Select%>

Add new record to the database using Classic ASP

Please note the code below. This sequence will add a new record into a database.

The database is identified by the DSN name and is called Student.

Option 1

The code below can be called by passing the parameteres in the query string like: dbnewrecSQL.asp?name=Joey&id=9000&year=1964

'on error resume next
If  auid<9000 then
end if
Set Conn = Server.CreateObject("ADODB.Connection") "DSN=Student;uid=student;pwd=magic"
SQLStmt = "INSERT INTO authors (AU_ID,author,year_born) "
SQLStmt = SQLStmt & "VALUES (" & auid
SQLStmt = SQLStmt & ",'" & auname & "'"
SQLStmt = SQLStmt & "," & int(auyear) & ")"
Set RS = Conn.Execute(SQLStmt)
set rs=nothing
If  err.number>0 then
response.write "VBScript Errors Occured:" & "<P>"
response.write "Error Number=" & err.number & "<P>"
response.write "Error Descr.=" & err.description & "<P>"
response.write "Help Context=" & err.helpcontext & "<P>"
response.write "Help Path=" & err.helppath & "<P>"
response.write "Native Error=" & err.nativeerror & "<P>"
response.write "Source=" & err.source & "<P>"
response.write "SQLState=" & err.sqlstate & "<P>"
response.write "No VBScript Errors Occured" & "<P>"
end if
IF  conn.errors.count> 0 then
response.write "Database Errors Occured" & "<br>"
response.write "<b>" & SQLstmt & "</b><P>"
for counter= 0 to conn.errors.count
response.write "Error #" & conn.errors(counter).number & "<P>"
response.write "Error desc. -> " & conn.errors(counter).description & "<P>"
response.write "No Database Errors Occured!" & "<P>"
end if
set conn=nothing

Option 2

This page demonstrates the capabilities how to add a record to a database using ADO instead of SQL. The script is:

<body bgcolor="#FFFFFF">
<% ' My ASP program that allows you to append a record %>
<form name="myauthor" action="dbnewADOrespond.asp" method="GET">
<p>Author ID: <input type="TEXT" name="id"></p>
<p> Author Name: <input type="TEXT" name="name"></p>
<p> Year Born: <input type="TEXT" name="year"></p>
<p> <input type="SUBMIT"> </p>

The form responder looks like this:

<body bgcolor="#FFFFFF">
<!--#INCLUDE VIRTUAL="/learn/test/lib_errors.asp" -->
on  error resume next
If  auid<9000 then
end if
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open "authors",Conn,adopenstatic,adlockoptimistic
RS("Author") = auname
RS("Year_Born")= int(auyear)
Call ErrorVBscriptReport("Adding Record")
Call ErrorADOReport("Adding Record",RS.activeconnection)
set rs=nothing

Response.Redirect in Classic ASP

Response Object and HTML Encoding

The response object is often used in conjunction with various kinds of coding schemes. No discussion of response would be complete without a discussion of how to “handle” or “escape” special characters. This sample script demonstrates common conversion and transformation commands that make sense to use with the response.write command:

<title>Response object</title>
</head><body bgcolor="#FFFFFF">
' The response object can be used to write text
' but sometimes some functions must be used to transform
' the text instead of sending as is to the browser

response.write "<B>Hyperion</b> by <I>Dan Simmons</i> is a great novel"
response.write "<p>"
response.write server.htmlencode("<B>Hyperion</b> by <I>Dan Simmons</i> is a great novel")
response.write "<p>"

response.write "Joe Smith & Hilda = a team"
response.write "<p>"
response.write server.URLencode("Joe Smith & Hilda = a team")


Response Object – Redirects

The response object can be used to decide what page to send a user to next. Specifically the response.redirect method will work in that capacity. We have made a script formjump.asp that takes advantage of this.

</head><body bgcolor="#FFFFFF">
<form action="FormJumpRespond.asp" method="get">
<SELECT NAME="wheretogo">
<OPTION value="news">Daily News</OPTION>
<OPTION value="docs">ASP IIS3 Roadmap/Docs</OPTION>
<OPTION value="main">MainPage of</OPTION>
<OPTION value="sample">IIS 3 Sample ASP scripts</OPTION>
<input type=submit value="Choose Destination">
The responder that reacts to this form is:

<body bgcolor="#FFFFFF">
' My ASP program that redirects to URL
Select Case where
case "main"
response.redirect thisURL & "/"
case "samples"
response.redirect thisURL & "/aspsamp/samples/samples.htm"
case "docs"
response.redirect thisURL & "/iasdocs/aspdocs/roadmap.asp"
case "news"
response.redirect ""
case "fun"
response.redirect ""
End Select
response.write "All dressed up and I don't know where to go<br>"
response.write "I recommend --> " & "<br>"
response.write server.htmlencode(thisURL & "/learn/test/res2.asp?where=fun") & "<br>"
response.write "for a good laugh!" & "<P>"