Committing and Rolling Back in Classic ASP – Part 1
<%@ TRANSACTION = value %> |
Where value can be any one of the following:
| Requires_New | The script will always start a new transaction. |
| Required | The script will use a transaction, and will participate in any open transaction, or create a new one if none are open. |
| Supported | The script will use any open transaction but will not start a new one if none are currently open. |
| Not_Supported | The script will neither initiate nor participate in a transaction. |
Typically, each ASP page will be its own transaction, however, it is possible to continue transactions across more than one page by using the Server.Transfer and Server.Execute methods (new to IIS 5). If the calling page is transacted, and the current page uses “Transaction = Required” or “Transaction = Supported” then the current page will continue the existing transaction, which makes some very complex applications possible.
Now you simply write ASP code as you would normally, and things will work transactionally.
<%@ TRANSACTION = Required %>
<%
strText = Request.form("Textbox")
if strText = "" then
ObjectContext.SetAbort
Response.write("You did not enter the text<p>")
else
Response.write("Processing transaction...<p>")
end if
%>
<%
'commit and rollback code
sub OnTransactionCommit()
Response.write("Operation was successful")
End Sub
sub OnTransactionAbort()
Response.write("Operation was unsuccessful")
End Sub
%>
Extract Domain Name from URL
<% Function DomainFromUrl( sText ) ' Dim nIndex If LCase(Left( sText, 7 )) = "http://" Then sText = Mid( sText, 8 ) End If If LCase(Left( sText, 8 )) = "https://" Then sText = Mid( sText, 9 ) End If nIndex = InStr( sText, "/") If nIndex > 0 Then sText = Left( sText, nIndex - 1 ) End If DomainFromReq = sText End Function %>
Full path from server virtual path
<%
Function String_GetFullPath( strVirtualPath )
String_GetFullPath = Server.MapPath( strVirtualPath )
String_GetFullPath = Replace(String_GetFullPath,"/","\")
End Function
%>
<%
'Use it like this:
Dim sFullPath
sFullPath = String_GetFullPath("/textfiles/test.txt" )
File_OpenExistingFile ( sFullPath...)
%>
Export Data to Excel in Classic ASP
Function GenHtmlTable()
Dim sRet
sRet = ""
sRet = "<table border=1 class=mysmalltext><tr bgcolor=""#d8ecea"">"
sRet = sRet & "<td>Name</td>"
sRet = sRet & "<td>Country</td>"
sRet = sRet & "</tr>"
'Beginning rows...Might read from Recordset - here it's hardcoded
sRet = sRet & "<tr>"
sRet = sRet & "<td>John Doe</td>"
sRet = sRet & "<td>US</td>"
sRet = sRet & "</tr>"
sRet = sRet & "<tr>"
sRet = sRet & "<td>Stefan Holmberg</td>"
sRet = sRet & "<td>Sweden</td>"
sRet = sRet & "</tr>"
sRet = sRet & "</table>"
GenHtmlTable = sRet
End Function
Response.Clear()
Response.Buffer = True
Response.AddHeader "Content-Disposition", "attachment;filename=export.xls"
Response.ContentType = "application/vnd.ms-excel"
Response.Write GenHtmlTable()
Response.End()
Redirecting with status 301 in ASP
<% Response.Status="301 Moved Permanently" Response.AddHeader "Location", " http://www.devguru.net/PathToNewUrl/" %>
Getting fileextension from filename
<%
Function IncGeneral_GetExtension( sFileName )
if sFileName = "" Then Exit function
if InStr(1, sFileName, ".") = 0 Then Exit function
IncGeneral_GetExtension = Right(sFileName, Len(sFileName) - InStrRev(sFileName, ".") )
End Function
%>
Example:
<%
Response.Write (IncGeneral_GetExtension("hej.asp"))
%>

