|
Page 2 of 3
|
  |
WAPReetings - A WAP Greeting card example, cont.
We will be using Microsoft Active Server Pages (ASP) for
server-side scripting and our script language is VBScript. Our
target audience will persons with WAP enabled devices and our output
language is Wireless Markup Language (WML). The Database I am using
is SQL Server 2000 Beta.
Before getting dirty with the code, let me explain you the
Database Structure:
The Database I am using is SQL Server 2000 Beta
|
Table: tblCards
|
|
Field Name
|
Type
|
Description
|
|
CardID
|
Identity
|
An Auto Number field to uniquely identify the Cards
|
|
CardLoc
|
Varchar (50)
|
URL of the Images / WAPreetings Card.
|
|
CardDescription
|
Varchar (100)
|
A small description of the card
|
|
Table: tblCardsToDeliver
|
|
Field Name
|
Type
|
Description
|
|
ReceiverID
|
Identity
|
An auto number field to identify the unique Receiver
|
|
CardID
|
Numeric
|
ID which references the tblCard Card ID
|
|
SName
|
Varchar (20)
|
Sender Name
|
|
SEmail
|
Varchar (100)
|
Sender Email
|
|
rName
|
Varchar (20)
|
Receiver Name
|
|
rEmail
|
Varchar (100)
|
Receiver Email
|
|
GreetingText
|
Varchar (200)
|
Greeting Text
|
CardKit.asp
Now let move to the coding, we will first explore the CardKit.asp
because it is our main file, which contains the function of creating
a new WAPreeting.
First we list all of the functions and then we will describe them
step-by-step:
- connectMe()
- getCardList()
- getCardDetail()
- sendCard()
- getWAPReetings()
connectMe():
connectMe() is the simple Connection Object generation function,
which creates a ADO Connection object, which passed to that
function:
Function connectMe(connObj)
Set ConnObj = Server.CreateObject("ADODB.Connection")
strConnection = "PROVIDER=SQLOLEDB.1; Data Source=local; Initial Catalog=wreetings;
user id=sa; password=;"
ConnObj.Open strConnection
End Function
Note: Change your Database Name, User ID and Password
according to your setup.
GetCardList():
This is function will generate the List Box in a WML Format about
the Cards which are available in our database. The code snippet
that I like to explain is as follow
Dim strCardList
'Loop through the records... and create the Option Tag
Do while Not cardListRstObj.EOF
strCardList = strCardList & "<option value='" &
cardListRstObj("cardID") & "'>"
strCardList = strCardList & cardListRstObj("cardDescription") &
"</option>"
cardListRstObj.moveNext
Loop
'now encapsulate the Option tag into the Select Tag
strCardlist = "<select name='cardsList'>" & strCardList & "</select>"
getCardList = strCardList
After opening the Recordset we then loop through the Recordset and
start generating the Option tags to generate the card list. But at
the end we encapsulate our existing option tags string into the
select tag. And then we will return this string to the function. And
voila! In our WAP enabled device, we have the following:

Now the question is how to call this function in our file name
cards.asp? Following is the code snippet of cards.asp
file, which is going to return the list of cards in a WML format:
<!--#include file="wapEnable.asp"-->
<!--#include file="cardKit.asp"-->
<%
Dim ConnObj
Call connectMe(connObj)
%>
<wml>
<card id="cardsList" title="Available Cards">
<p>
<b>Select your Card</b>
<%Response.write getCardList(connObj)%>
<do type="accept" label="Detail">
<go href="cardDetail.asp" method="post">
<postfield name="cardID" value="$(cardsList)"/>
</go>
</do>
</p>
</card>
</wml>
The grey lines are very important to consider in this file, in
first part of the ASP code we declare local variable and then pass it
to our connectMe(connObj) to convert that function in
Connection Object.
As you see one more grey line where we call our
getCardList(connObj) function, by providing the connection object
variable, so we don't need any other Connection Object, use the
existing ones.
If you are a beginner and facing difficulty to understand the
WML & ASP code, then I will prefer that you must learn some basic
lessons as you can find on
www.TopXML.com/wap ,
www.anywhereyougo.com and www.allnetdevices.com.
cardDetail.asp
GetCardDetail()
When the user selects card from the Card List, we will take our
user to the cardDetail.asp. Which utilize the
getCardDetail() function, this function has two parameters 1)
CardID 2) ConnObj (we have already seen this in our previous function
therefore we do need to explain these parameter)
Following is the code snippet of the getCardDetail()
function:
strSQL = "SELECT * from tblCards"
strSQL = strSQL & " WHERE cardID = " & cardID
detailRstObj.Open strSQL, connObj,adOpenStatic
'Is Data is available?
If detailRstObj.EOF = false Then
'If so, then we will generate the text, to display the card detail.
returnString = "<b>Detail:</b> " & detailRstObj("cardDescription") & "<br/>"
returnString = returnString & "<b>Image:</b> <img src='." & _
detailRstObj("cardLoc") & "'"
returnString = returnString & " alt='" & detailRstObj("cardDescription") & "'/>"
End If
'Return the generate string...
getCardDetail = returnString
First we will generate the SQL to find the card, which the user
chooses. Then we will check whether the selected Card Data is
available in our database. If it is then we will generate the
string, which actually contains the WML Tags, such as <b>
& <img> tags. I'd like to explain to you something
about the <img> tag. There are two attributes in this tag
src and alt. Both attributes are required, if you
don't use the alt attribute then the error occur. So we will
paste our Description (from database) in this tag, and the image
location that is mandatory will be in Src attribute. The following
screen will be displayed in the WAP device:

<p>
<%Response.write getCardDetail(varCardID, connObj)%>
<do type="accept" label="Confirm">
<go href="generateCard.asp?cardid=<%=varCardID%>"/>
</do>
</p>
In the gray code sections, we are returning the String from the
getCardDetail(), which is similar to the code in the
getCards() routine.
The line start with <go> tag requires your close
attention. In the go tag there is an attribute
name href, which similar to action attribute in form
tag. In the action attribute of the form tag you
define the file name, where you want to submit the file, likewise
occurs in the WML go tag. We refer to another file,
generateCard.asp, passing the QueryString variable name
cardID. Note this is the URL string variable and instead of
using the Post Field technique we can also pass our values using the
URL string variables which is what I did.
GenerateCard.asp
Before we proceed to next function I am going to take you on the
tour of generateCard.asp, as this is the form, which will take
information, such as, Sender Name, Sender Email, Receiver Name,
Receiver Email and Greeting Text. Then to submit it to the
sendCard.asp and I will use the next function in that page which
is sendCard().
sendCard()
First of let me list the parameters which this function require to
work:
CardID = CardID
SName = Sender Name
SEmal = Sender Email
RName = Receiver Name
REmail = Receiver Email
Gtext = Greeting Text
ConnObj = Connection Object Reference
This function is bit lengthy in parameter, and you know in a quick
glance that what we are passing, Card ID which User chosen from the
cards.asp, Sender Name, Sender Email, Receiver Name, Receiver
Email and Greeting Text will be from generateCard.asp and
submitted to the sendCard.asp file.
Let's examine the following code snippet:
deliverRstObj.Open "tblCardsToDeliver", connObj, adOpenKeyset,adLockOptimistic, adCmdTable
deliverRstObj.AddNew
deliverRstObj("cardID") = Cint(lCardID)
deliverRstObj("sName") = lSName
deliverRstObj("sEmail") = lSEmail
deliverRstObj("rName") = lRName
deliverRstObj("rEmail") = lREmail
deliverRstObj("greetingText") = lGText
deliverRstObj.Update
deliverRstObj.Close
'Fetch the last Identity Value and paste it into the receiverID
deliverRstObj.Open "SELECT @@IDENTITY",connObj,adOpenStatic,adLockReadOnly
receiverID = deliverRstObj.Fields.Item(0)
deliverRstObj.Close
'Create the URL, for this v will retrieve the ReceiverID which is
'an Identity Field, and after updating it, we retrieve it and
'concatenate with URL String
varURL = "http://localhost/wapreetings/getCard.asp?rid=" & receiverID
'Subject Line String
varSubject = "You've got WAPReetings"
'Handling Error
if Err.number <> 0 Then
sendCard = Err.number & "-" & Err.Description & "-" & Err.Source
Exit Function
Else
SendCard=true
End If
'Generate the Body Text String which create the body text.
varBody = varBody & chr(13) & "Please pick your WAPReetings from: " & chr(13) & chr(13)
varBody = varBody & "<a href='" & varURL & "'>" & varURL & "</a>" & chr(13)& chr(13)
varBody = varBody & "Regards, " & chr(13)
varBody = varBody & "WAPReetings Staff"
'Sending mail...
Dim cdontsMailObj
Set cdontsMailObj = Server.CreateObject("CDONTS.NewMail")
cdontsMailObj.From = lSEmail
cdontsMailObj.To = lREmail
cdontsMailObj.Subject = varSubject
cdontsMailObj.Body = varBody
cdontsMailObj.Send
We will use the AddNew method of Recordset Object to add the new
card selected into the database. You can use Insert Query as well,
but for sake of simplicity I utilize the above captioned method.
Then we want to get the currently appended record's auto generated
identity. By sending the "SELECT @@IDENTITY" and in this way we have
now Receiver ID which helps us to generate the URL which we are going
to send into the Email. We then generate the string for Subject
& Body and also appended the URL that we generated after adding
our record.
We use the CDONTS to send the Email using our local server.
Therefore I created the CDONTS Object and then utilize its properties
such as: From, To, Subject and Body:
From =
Sender Email in our
case the value is lSEmail variable
To
= Receiver
Email address in our case the lREmail variable
Subject
=
Subject Line, which
will be varSubject variable.
Body
= Body Text,
which will be varBody with concatenated varURL.
The sendCard() function will return two status's:
- if it is successful it will return the True, and
- if any error occur it will return the error description and
number.
We will see in our next code snippet from sendCard.asp how
the sendCard() function implemented
<%
On Error Resume Next
call ConnectMe(connObj)
'Get the variable from the generateCard.asp
varSenderName = Request.form("sName")
varSenderEmail = Request.form("sEmail")
varReceiverName = Request.form("rName")
varReceiverEmail = Request.form("rEmail")
varGreetingText = Request.form("gText")
varCardID = Request("cardID")
strStatus = sendCard( varCardID, varSenderName, varSenderEmail, varReceiverName, _
varReceiverEmail, varGreetingText, connObj)
Dim message
If strStatus = true then
message = message & "<p align='center'><strong>WAPReetings. </strong></p>"
message = message & "<p><br/>WAPReeting Successfully deliver...</p>"
Else
message = "<p>" & strStatus & "</p>"
End If
%>
<wml>
<card id="sendCard" title="Card Send Status">
<%
Response.Write message
%>
<do type="accept" label="Home">
<go href="cards.asp"/>
</do>
</card>
</wml>
First lines of codes get the value from the generateCard.asp HTML
Form Receivers, using the Request.Form method.
After this we pass our information to the sendCard()
method, if the code returns no errors, which indicates that the code
executed successfully and the email has sent. In this case the
message variable will hold a message, which says, "WAPReetings
Successfully delivered…" else it will display the Error
Description and Error Number.
Up until now you have learnt how we send the card using the
CDONTS, well now we will learn that how to read/get our
WAPReetings
|
Page 2 of 3
|
  |
|