Tuesday, March 17, 2009

Useful Functions in Classic ASP

1. Mid function to trim text for specific length
for 1 to 5 letter triming.
strtext=mid("text want to trim",1,5)

2. Replace function with Syntax
xyz=replace(Amit pal, " ", "G")

3. Find Server name by server variables
strServerURL = "http://" + Request.ServerVariables("SERVER_NAME")

4. Split Fuction Syntax
current_date=date
current_day=split(current_date,"/")

5. Trim function for trimimg blank spaces
email=trim(request.form("email"))

6. Count recordset's total number of rows for any Query's resultset

Method 1:

rs.open sqlQuery,ObjConn
rs.cursorlocation=3
Dim countRow
countRow=rs.Recordcount

Method 2:

rs.open sqlQuery,ObjConn,3
Dim countRow
countRow=rs.Recordcount

ASP Source Code for sending email

dim tend12
tend12="Dear User How r u?"
Set objMail = Server.CreateObject("CDO.Message")
objMail.From = "
info@sample1.com"
objMail.To = "
abc@sample1.com"
objMail.BCC = "
amit@sample1.com"
objMail.Subject = "Sample Subject"
objMail.HTMLBody = tend12
objMail.Send
Set objMail = nothing

Source Code for sending email with file attachment in Classic ASP

dim tend
tend="body text will be here"
Set objMail = Server.CreateObject("CDO.Message")
objMail.From = "abc@xyz.in"
objMail.To = "abc@xyz.in"
objMail.CC = "info@xyz.in"
objMail.BCC = "abc@xyz.in,mn0@xyz.in,dfg@xyz.in"
objMail.Subject = "subject will be here"
'objMail.BodyFormat = 0
'objMail.MailFormat = 0
objMail.HTMLBody = tend

' For attachement file
Dim strServerURL
'Server Name including http:// protocol
strServerURL = "http://" + Request.ServerVariables("SERVER_NAME")
Dim strServerURLFull
'Path to subweb directory
strServerURLFull = strServerURL & "/2008"
Dim strTargetURL
'Directory where file is located
strTargetURL = strServerURLFull & "/xyz"

dim strpath
strpath = strTargetURL & "/" & locatedfilename
objMail.AddAttachment(strpath)

if locatedfilename2 <> "" then
dim strpath2
strpath2 = strTargetURL & "/" & locatedfilename2
objMail.AddAttachment(strpath2)
end if

objMail.Send

Source code to protect from SQL Injection with Classic ASP

'Function to sql injection by url inputs

Function Injection (strValue)
IF strValue <> "" Then
If ( Instr(strValue,"union") > 0 OR Instr(strValue,"where") > 0 OR Instr(strValue,"insert") > 0 OR Instr(strValue,"delete") > 0 OR Instr(strValue,"Truncate") > 0 OR Instr(strValue,"update") > 0 OR Instr(strValue,"like") > 0 OR Instr(strValue,"drop") > 0 OR Instr(strValue,"create") > 0 OR Instr(strValue,"modify") > 0 OR Instr(strValue,"rename") > 0 OR Instr(strValue,"alter") > 0 OR Instr(strValue,"cast") > 0 OR Instr(strValue,"href") > 0 OR Instr(strValue,"a href") > 0 OR Instr(strValue,";") > 0) Then
Response.redirect(".././index.asp")
End If
End IF
End Function

'End sql injection Function


'call Function to sql injection by url inputs

Injection Request.ServerVariables("QUERY_STRING")

Best Way to protect our site from SQL injection call SQL Injection function in Global file/Global.asa/connection file.

SQL Server Database connection source code in classic ASP

Connection Code

Dim strConnection,objConn
Set objConn = Server.CreateObject("ADODB.Connection")


strConnection ="Provider=SQLOLEDB;Data Source=671.151.47.212; Database=DBNews; uid=sample;pwd=sample@123;"
objConn.open strConnection

End Connection Code