What is ASP?
ASP stands for Active Server Pages. It is a server side technology which is used to display dynamic content on web pages. For example you could write code that would give your visitors different information, different images or even a totally different page depending on what browser version they are using.
A Simple program with ASP.
Write the following lines of code in notepad and save this file as helloworld.asp in c:\inetput\wwwroot\ folder.
>body>
<% response.write("Hello World!") %>
>/body>
>/html>
Then open a browser and type
http://helloworld.asp
This will open the asp page and display as follows
Hello World
2. Working with Session Variables
Session variables are stored in the server's computers memory. They are deleted once the visitor closes his browser or after a period of inactivity, usually 20 minutes by default.
Why use session variables?
Session variables come in very handy for tracking purposes
How to declare session variable ?
In the login page, if the login is correct, type this additional line.
The session variable is used in two steps.
Step 1
Declare and set a session variable on the login page.
Session("EngrName") = rs ("Engrname2")
Session("EngrNo") = rs("ENGRNO")
Also a flag on the login page to set, after a user logs-in successfully.
Session("secure_status") = "login"
Aim : To Display Tag Names of COA System Info, where the tag names are put in the code and the values for the corresponding tag is extracted from the Pi System
Open notepad and write the following lines of code
Note : Replace '>' with '<' >$@ Language="VBScript" $>
>$ Option Explicit $>
>!--#include file="menu.asp"-->
>h2> COA SYSTEM INFORMATION >/h2>
>$
Response.AddHeader "Refresh", "10" ' this line will automatically refresh the page every 10 secs.
'declare your variables
Dim strConnection, conn, rs, strSQL
dim tgArray(9) ' an array declaration to store all values of the tag.
strConnection = "Provider=PIOLEDB;Data Source= c2-rhq2-pis; user id=piadmin;password=********;"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open strConnection
Set rs = Server.CreateObject("ADODB.Recordset")
strSQL = "Select time as t1, tag as v1, value as v2 from piarchive..piinterp where tag in ('sinusoid','cdt158','ECC:GENTOT(_KV_MW).C.CRC$','CALC:CONSUM(C2_MISKV_MW).C.CRC$', 'ECC:STDBY(C2_MISKV_MW).C.CRC$','ECC:TOT_UNAVA_CAP(C2_MISKV_MW).C.CRC$','ECC:RYAD-TEMP(C2_MISKV_TEMP).C.CRC$','ECC:RTGEN_LD(C2_MISKV_FLD).C.CRC$','CALC:INDUST_LOAD(C2_MISKV_MW).C.CRC$','SYSTEM_TOTAL:SPINING_RESERVE(_MW).C.COA.CRC$' ) and time = '*' "
rs.open strSQL, conn, 3,3
'first of all determine whether there are any records
If rs.EOF Then
Response.Write("No records returned.")
Else
dim n
n = 0
Do While Not rs.EOF
tgArray(n) = rs.fields("v2")
n = n+1
rs.MoveNext
Loop
End If
$>
>table border="1">
>td> Total Generation Generation >/td>
>td>>$response.Write(tgArray(3))$> >/td>
>tr>
>td> COA DEMAND >/td>
>td>>$response.Write(tgArray(4))$> >/td>
>tr>
>td> RIYADH TEMPERATURE >/td>
>td>>$response.Write(tgArray(5))$> >/td>
>tr>
>td> Standy Generation >/td>
>td>>$response.Write(tgArray(6))$> >/td>
>tr>
>td> Unavailable Generation >/td>
>td>>$response.Write(tgArray(7))$> >/td>
>tr>
>td> Spinning Reserve >/td>
>td>>$response.Write(tgArray(9))$> >/td>
>tr>
>td> Consumer Generation >/td>
>td>>$ response.Write(tgArray(0)) $> >/td>
>/tr>
>tr>
>td> Industrial Load>/td>
>td>>$ response.Write(tgArray(1))$> >/td>
>tr>
>/table>
>/table>
>!--close the connection and rs objects and free up resources -->
>$
rs.Close
Set rs = Nothing
Conn.Close
Set Conn = Nothing
%>
How to generate the output on an excel, from a database form-query !!
This can be achieved by using two asp files. Let us name these files as
1. sendtoexcel.asp
2. piasp4_excel_ok.asp
contents of SendToExcel.asp file (below)
*************************************************************************************************************************
contents of SendToExcel.asp file (below)
*************************************************************************************************************************
>body>
>!--#include file="menuPisystemsub.asp"-->
>!-- I am not displayed by the browser -->
>h2> An output in Excel for Any Pi Tag >/h2>
>form name="form1" action="piasp4_excel_ok.asp" method="post">
>div align="center">
>table width="80%" border="0">
>tr>
>p>
>td> Enter Tag Name : >/td>
>td> >input type="text" name="txttagname" > please use % for wild search. >/p> >/td>
>tr>
>td>>p>startTime : >/td>
>td> >input type="text" name="txtStarTime" value="10/10/08 10:00:00"> dd/mm/yy hh:mi:ss >/p> >/td>
>tr>
>td>End Time >/td>
>td>>input type="text" name="txtEndTime" value="10/11/08 10:00:00"> dd/mm/yy hh:mi:ss >/p> >/td>
>tr>
>td> >p>Interval>/td>
>td> >input type="text" name="txtInterval" value="10m"> >/td>>tr>>td>please specify the Interval as follows >/p> >/td>
>tr>
>ul>
>li>5s = every 5 seconds
>li> 5m = every 5 minutes
>li> 5h = every 5 hours
>/ul>
>/tr>
>tr>
>td>>input type="submit" value="Send to Excel" name="save">>/td>
>/tr>
>/table>
>/div>
>/form>
>!-- end the HTML form-->
contents of piasp4_excel_ok.asp (down
*************************************************************************************************************************
>html>
>body>
>%
'declare your variables
Dim vname, vstime, vetime,vint
Dim sConnString, connection, sSQL
'Receiving values from Form on the SendExcel.asp , assign the values entered to variables
vname = Request.Form("txttagname")
vstime = Request.Form("txtstartime")
vetime = Request.Form("txtendtime")
vint = Request.Form("txtInterval")
Dim count_eoagd
Dim strConnection, conn, rs, strSQL
dim tg
tg = request.querystring("txttagname")
strConnection = "Provider=PIOLEDB;Data Source= c-rhq-pis1; user id=*****;password=*******;"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open strConnection
Set rs = Server.CreateObject("ADODB.recordset")
strSQL = "Select cast(time as string) as t1,tag as v1, value as v2 from piarchive..piinterp where tag = "
strSQL = strSQL + " ' " & vname & " '"
strSQL = strSQL & " and time between "
strSQL = strSQL & "'" & vstime & "'"
strSQL = strSQL & " and '" & vetime & "'"
strSQL = strSQL & " and timestep = '" & vint & "'"
strSQL = strSQL & " order by t1 "
rs.open strSQL, conn, 3,3
'these two lines below while generate the data into excel therefore, if you comment the two lines below, you will 'get the output on a browser page.
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment; filename=Enquiry_List.xls"
%>
>!-- MAIN TABLE -->
>table width=100%" border=0 cellpadding="0" cellspacing="0">
>%
if not rs.eof then
do while not rs.eof
>tr>
>td style="padding:9px;text-align:center">
>table border=0 width=100%" cellpadding="0" cellspacing="1" style="border:1 black solid">
>tr>
>td>>table>>tr>
>td align="left">
>%=rs.fields("t1").value%>
>/td>
>td>
>%=rs.fields("v1").value%>
>/td>
>td>
>%=rs.fields("v2").value%>
>/td>
>/tr>>/table>
>/td>
>/tr>
>/table>>/td>>/tr>
>%
rs.movenext()
loop
else
%>
>%end if%>
>/table>
***********************************************************************************************************************
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx--end of exercise--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
************************************************************************************************************************
To refresh an asp page automatically on a specified interval, write this one line on the asp file, where 30 represents an interval of 30 seconds before next refresh.
Response.AddHeader "Refresh", "30"
************************************************************************************************************************
************************************************************************************************************************
How to display the contents of one database page on another asp page.
************************************************************************************************************************
Write the following lines in the asp page, on the location where you want the values to be displayed.
FileName: main.asp
************************************************************************************************************************
How to add values to a database from a form on the asp page.
************************************************************************************************************************
Files for this exercise
1. Insrec.news.asp
2. AddRec_News.asp
Breif Description :
This exercise will show you a simple procedure of inserting values from an ASP form into a database table.
Step 1.
Design a form on the asp page as follows (
************************************************************************************************************************
contents of INSREC_NEWS.asp
************************************************************************************************************************
>html>
>body>
>!--#include file="menu.asp"-->
>h2> Add a New News Update >/h2>
>form name="form1" action="AddRec_News.asp" method="post">
>div align="center">
>table width="80%" border="0">
>tr>
>td>Date>/td>
>td>>input type="text" name="txtdate"> DD-MON-YY >/td>
>/tr>
>tr>
>td>Title>/td>
>td>>input type="text" name="txttitle">>/td>
>/tr>
>tr>
>td>Author:>/td>
>td> >input type="text" name="txtAuthor">>/td>
>/tr>
>tr>
>td height="149">News Details >/td>
>td>>textarea name="txtdetails" cols="60" rows="10">>/textarea>>/td>
>/tr>
>tr>
>td height="145"> >/td>
>td>>input type="submit" value="Save" name="save">>/td>
>/tr>
>/table>
>/div>
>/form>
>!-- end the HTML form-->
Then create another form to execute the insert on the SQL
************************************************************************************************************************
contents of AddRec_NEWS.asp
************************************************************************************************************************
>html>
>body>
>!--#include file="menu.asp"-->
>%
'declare your variables
Dim vtitle, vdate,vauthor,vdetail
Dim sConnString, connection, sSQL
'Receiving values from Form, assign the values entered to variables
vdate = Request.Form("txtdate")
vtitle = Request.Form("txttitle")
vauthor = Request.Form("txtauthor")
vdetail = Request.Form("txtdetails")
'declare SQL statement that will query the database
'ok -- sSQL="INSERT into trans(name) values ('" & vname & "' ) "
'sSQL="INSERT into trans(name,sdate) values ('" & vname & " ' , ' " & vdate & " ') "
sSQL = "INSERT into news(newsdate,title,details,author) values ('" & vdate & "','" & vtitle & " ','" & vdetail & "','" & vauthor &"') "
'define the connection string, specify database driver and the location of database
sConnString = "dsn=ORCLBKP;uid=taj;pwd=***"
'create an ADO connection object
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")
'Open the connection to the database
connection.Open(sConnString)
'execute the SQL
connection.execute(sSQL)
response.write "The form information was inserted successfully."
'Done. Close the connection object
connection.Close
Set connection = Nothing
%>
>p>
>a href= "insrec_news.asp"> Back >/a>
************************************************************************************************************************
Show as "Processing....." when ajax gets the data !!
************************************************************************************************************************
In the pigetdata.js (javascript file) modify the code as follows to show the "Processing...." while, ajax fetches data for the required query.
function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
else
{
document.getElementById("txtHint").innerHTML="Processing........Please Wait !!";
}
}
************************************************************************************************************************
ASP DATE TIMES
************************************************************************************************************************
ASP has a very handy inbuild function called FormatDateTime().
Let's start with the now() function to get the current date and time into a variable
<%
dim todaysDate
todaysDate=now()
%>
Now we can use the FormatDateTime function to format our date variable in a variety of ways.
First let's see how todaysDate appears :
<%
Response.write todaysDate
%>
RESULT: 1/13/2009 5:54:40 AM
Using 0 - the vbGeneralDate format creates a date as short date (if included), and time as long time.
<%
Response.write FormatDateTime(todaysDate,0)
%>
RESULT: 1/13/2009 5:54:40 AM
Using 1 - the vbLongDate shows the date as long date
<%
Response.write FormatDateTime(todaysDate,1)
%>
RESULT: Tuesday, January 13, 2009
--------------------------------------------------------------------------------
Using 2 - the vbShortDate shows the date as short date
<%
Response.write FormatDateTime(todaysDate,2)
%>
RESULT: 1/13/2009
-------------------------------------------------------------------------------
Using 3 - the vbLongTime format shows the time as long time .
<%
Response.write FormatDateTime(todaysDate,3)
%>
RESULT: 5:54:40 AM
--------------------------------------------------------------------------------
Using 4 - the vbShortTime format creates the current time in 24 format (hh:mm)
<%
Response.write FormatDateTime(todaysDate,4)
%>
RESULT: 05:54
************************************************************************************************************************
Data Navigation on an ASP page (tag Calendar )
************************************************************************************************************************
' Code for Date Navigation
Dim dDate
Function SubtractOneMonth(dDate)
SubtractOneMonth = DateAdd("d", -1, dDate)
End Function
Function AddOneMonth(dDate)
AddOneMonth = DateAdd("d", 1, dDate)
End Function
If IsDate(Request.QueryString("date")) Then
dDate = CDate(Request.QueryString("date"))
Else
dDate = Date()
End If
%>
>table width="500" border="1" cellspacing="0" cellpadding="0">
>tr>
>td align="center">>a href="ppGenHourly7.asp?date=>%= SubtractOneMonth(dDate) %>">>font ><<>/font>>/a>>/td>
>td align="center" >>font color="red">>strong>>%= MonthName(Month(dDate)) & " " & Year(dDate) %>>/strong>>/font>>/td>
>td align="center" >>font color="red">>strong>>%= dDate %>>/strong>>/font>>/td>
>td align="right" >>A HREF="ppGenHourly7.asp?date=>%= AddOneMonth(dDate) %>">>font color="red" size="2">>>>/font>>/a>>/td>
>/tr>
>/table>
************************************************************************************************************************
Converting the date format from dd/mm/yy to mm/dd/yy using DatePart Function
************************************************************************************************************************
>% dim dt,d1,m1,y1
dt = Now()
d1 = DatePart("d",dt)
m1 = DatePart("m",dt)
y1 = DatePart("yyyy",dt)
dt = m1&"/"&d1&"/"&y1
response.write(dt)
To show only the hour part
todaysDate=now()
hh = DatePart("h",todaysDate)
response.write(hh)
%>
To check if the date is only one character example 8, 9 etc,,,and if so, attach a 0 for it. so that it become 09,08 etc.
hh = DatePart("h",dDate)
if len(hh) = 1 then
hh = "0"&hh
end if
Hour Function
This function will return the hour value in military format.
hh = Hour(dDate)
************************************************************************************************************************
Adding values of a recordset
************************************************************************************************************************
>%
dim vrpp3b,vrpp3s,vrpp3tot
vrpp3b = Recordset ("RPP3B")
vrpp3s = Recordset ("RPP3S")
vrpp3tot = vrpp3b + vrpp3s
%>
>/td>>span>>td width=40>
>% Response.Write (vrpp3tot) %>
************************************************************************************************************************
http://localhost/tajasp/stage/anytag_btw_time3.asp?txttagname=8002
************************************************************************************************************************
************************************************************************************************************************
HOURLY NAVIGATION BAR
************************************************************************************************************************
>%
' Code for Date Navigation Bar
Dim dDate
todaysDate = Now()
'response.write("Time now is " & todaysDate)
dDate = Now()
Function SubtractOneMonth(dDate)
SubtractOneMonth = DateAdd("d", -1, dDate)
End Function
Function AddOneMonth(dDate)
AddOneMonth = DateAdd("d", 1, dDate)
End Function
Function SubOneHour(dDate)
SubOneHour = DateAdd("h", -1, dDate)
End Function
Function AddOneHour(dDate)
AddOneHour = DateAdd("h", 1, dDate)
End Function
If IsDate(Request.QueryString("date")) Then
dDate = CDate(Request.QueryString("date"))
Else
dDate = Now()
End If
%>
>table width="500" border="1" cellspacing="0" cellpadding="0">
>tr>
>td align="center">>a href="ppGenPiorcl5.asp?date=>%= SubtractOneMonth(dDate) %>">>font color="red" size="2">PrevDay>/font>>/a>>/td>
>td align="center">>a href="ppGenPiorcl5.asp?date=>%= SubOneHour(dDate) %>">>font color="red" size="2">PrevHour>/font>>/a>>/td>
>td align="center" >>font color="red">>strong>>%= MonthName(Month(dDate)) & " " & Year(dDate) %>>/strong>>/font>>/td>
>td align="center" >>font color="red">>strong>>%= formatdatetime(dDate,1) %>>/strong>>/font>>/td>
>td align="right" >>A HREF="ppGenPiorcl5.asp?date=>%= AddOneHour(dDate) %>">>font color="red" size="2">NextHour>/font>>/a>>/td>
>td align="right" >>A HREF="ppGenPiOrcl5.asp?date=>%= AddOneMonth(dDate) %>">>font color="red" size="2">NextDay>/font>>/a>>/td>
>/tr>
>/table>
>% dim str
'to show a part of the string
str = rs.fields("v1")
str = Mid(str, 6,5) %>
>td>>% response.write(str) %> >br> >/td>
>% dim pgen
pgen = rs.fields("v2")
' to handle nulls, write the following if condition with ISNull
if isnull(pgen) then
pgen = pgen
else
pgen = round(pgen,2)
end if
%>
******************************************************************
Passing Parameters
dim tg,tfx
'load a value from file
' example : substationload3.asp?txtagname=8007
tg = request.querystring("txttagname")
******************************************************************
Font Size in an Input Box
>input type="text" size="5" name="t0" style="font-size:12pt; font-weight: 200;" value= >% = vArray(0) %>
******************************************************************
Font as Bold in an Input Box
>td>>input type="text" size="5" name="t0" style="font-weight:bold;" value= >% = vArray(0) %>
Get the text-box name with a loop
>td>>input style="font-size:7pt" name="t>% response.write(n) %>" type="text" size="5" value=">% response.write(pgen)%>" >/input> >/td>
******************************************************************
>td>>input type="submit" value="Save" name="save" disabled="true">>/td>
Handling Nulls in ASP
tgArray(n) = rs.fields("v2")
if isnull(tgArray(n)) then
tgArray(n) = 0
end if
Active Server Pages