Modifying User Stats Based on ASPs and SQL Stored Procedures

 

Introduction.  This is the last big site functionality we need to implement.  It is also the most complicated. 

It is made somewhat more complicated by the fact that the user might modify their e-mail address and password.  So we need to make certain to keep track of what their e-mail and password were when they started the modification in order to identify which record in the database should be modified.  Fortunately this is not a particularly big deal.

But we also need to make certain the user doesn't change their e-mail address to something already in the database table.

Thus we will be making use of three stored procedures with appropriate code segments.

  • a stored procedure to validate the user has entered an existing e-mail address and password
    • yourInitials_RetrieveUser
    • the original e-mail and password will be stored in session variables in case they are written over in the form
  • a stored procedure to validate that the user hasn't changed their e-mail address something that already is in the database table
    • yourInitials_CheckUnique
  • a stored procedure to actually update the user's stats if they've done everything correctly
    • yourInitials_UpdateUser

Notice that two of these stored procedures already exist.  This is another advantage of developing stored procedures.  They are often used in more than one location.

You want to create the following file called sp_UpdateUser.asp.  Then you need to change the name of the table

yourInitials_UserRegistration

and the name of the stored procedure

yourInitials_UpdateUser

in the appropriate places and upload it to your directory that contains a copy of the adovbs.inc.   Then when you execute this ASP via your browser on the web it will create the appropriate stored procedure.

 

<%@ Language=VBScript %>
<% Option Explicit %>
<!--#include File="adovbs.inc"-->

<%
' Open a connection to our SQL Server database
' We will use the ADO Driver connection

Dim connfoxFire, strSQLCreate
Set connfoxFire = Server.CreateObject("ADODB.Connection")
connfoxFire.ConnectionString="Driver={SQL Server}; Server=cisdev.quinnipiac.edu;" & _
"Database=ASP_SQL;UID=learn; PWD=learnSQL"

connfoxFire.Open

' Creating the SQL String to create the stored procedure
strSQLCreate = "CREATE PROCEDURE yourInitials_UpdateUser " & _
"@OriginalEMail varchar(50), " & _
"@OriginalPassword varchar(50), " & _
"@FirstName varchar(50), " & _
"@MiddleName varchar(50), " & _
"@LastName varchar(50), " & _
"@EMail varchar(50), " & _
"@strPassword varchar(50), " & _
"@Age int, " & _
"@ECommerce bit, " & _
"@SmallBusiness bit, " & _
"@WebDevelopment bit, " & _
"@Java bit, " & _
"@CPP bit, " & _
"@VisualBasic bit, " & _
"@CreditCard varchar(50), " & _
"@NoCollege bit, " & _
"@College bit, " & _
"@Masters bit, " & _
"@PhD bit, " & _
"@AnnualSalary money
AS " & _
"update
yourInitials_UserRegistration set " & _
"FirstName = @FirstName, " & _
"MiddleName = @MiddleName, " & _
"LastName = @LastName, " & _
"EMail = @EMail, " & _
"strPassword = @strPassword, " & _
"Age = @Age, " & _
"ECommerce = @ECommerce, " & _
"SmallBusiness = @SmallBusiness, " & _
"WebDevelopment = @WebDevelopment, " & _
"Java = @Java, " & _
"CPP = @CPP, " & _
"VisualBasic = @VisualBasic, " & _
"CreditCard = @CreditCard, " & _
"NoCollege = @NoCollege, " & _
"College = @College, " & _
"Masters = @Masters, " & _
"PhD = @PhD, " & _
"AnnualSalary = @AnnualSalary " & _
"where EMail = @OriginalEMail and strPassword = @OriginalPassword"

connfoxFire.execute(strSQLCreate)

connfoxFire.Close
Set connfoxFire = Nothing

Response.Write "<font size = 5>The SQL has executed</font>"

%>

 

This code will execute only once to create the stored procedure unless you get me to remove it.  After executing this you probably should delete this file from your web directory.

Notice how the portion in purple shows all of the values that need to be passed to the stored procedure in order for it to insert everything the user might have supplied.

It is certainly convenient that the other two stored procedures we need have already been created.

A Form Page.  Now we to develop the form page for the user to input their e-mail address and password.  You should call this page ModifyUser.html.

 

<HTML>
<head><title>Validating You Are a Registered User</title></head>
<body>
<form method=POST action="ModifyGetFeedback.asp">
<center>
<font size = 5>Validating You Are a Registered User</font>
<br> <font size = 4>In order to modify your stats</font>
</center>
<P><table cellpadding = 4>
<tr>
<td align = right><b>E-Mail</b></td>
<td align = left><input type=text name="txtEMail"></td>
</tr>
<tr>
<td align = right><b>Password</b></td>
<td align = left><input type=password name="txtPassword"></td>
</tr>
<tr>
<td align = right></td>
<td align = left><input type=submit value = "Modify My Stats"></td>
</tr>
</table>
</font>
</form>
</body>
</HTML>

 

This is our standard form so I won't even present an image.

The Modification Form Page.  Now we to develop the form page for the user to modify their stats.  This page must

  • validate that the user has entered an e-mail address and password that allows them to modify their stats.
  • find the appropriate record
  • display the current user entries in the form to remind the user what they have inputted previously

You should call this page ModifyGetFeedback.asp.

 

<%@ Language=VBScript %>
<% Option Explicit %>
<!--#include File="adovbs.inc"-->
<!--#include File="ConnectToUserRegistration.asp"-->

<HTML>
<head><title>Modifying the Stats on a Registered User</title></head>
<body>
<center>
<font size = 5>Modifying the Stats on a Registered User
</center>

<%
Dim rsValidateInputs, sqlStatement

' Obtaining what was used for the email and password

Session("OriginalEMail") = "'" & Request.Form("txtEMail") & "'"
Session("OriginalPassword") = "'" & Request.Form("txtPassword") & "'"

Set rsValidateInputs = Server.CreateObject("ADODB.Recordset")
' Call the stored procedure to find
' e-mail and password in the database

sqlStatement = "execute yourInitials_RetrieveUser " & _
Session("OriginalEMail") & ", " & _
Session("OriginalPassword")

Set rsValidateInputs = connUserRegister.Execute(sqlStatement)

If rsValidateInputs.EOF Then

connUserRegister.Close
Set connUserRegister = Nothing
Response.Write "<center><font size = 4><b>The E-Mail Address and Password were not found"
Response.Write "<BR><a href = ModifyUser.html>Try Again</b></font>"
Response.Write "</a></center>"

Else

' Found the entry and need to fill in the form
' with what the user has entered previously

%>

<Form Method = Post Action="ModifyUser.asp">

<P><table cellpadding = 4>
<tr>
<td align = right><b>Name</b></td>
<td align = left><input type=text name="txtFirstName" value="<%=rsValidateInputs("FirstName") %>">
<input type=text name="txtMiddleName" size = 4 value="<%=rsValidateInputs("MiddleName") %>">
<input type=text name="txtLastName" value="<%=rsValidateInputs("LastName") %>"></td>
</tr>
<tr>
<td align = right><b>E-Mail</b></td>
<td align = left><input type=text name="txtEMail" size = 60 value="<%=rsValidateInputs("EMail") %>"></td>
</tr>
<tr>
<td align = right><b>Password</b></td>
<td align = left><input type=password name="txtPassword" value="<%=rsValidateInputs("strPassword") %>"></td>
</tr>
<tr>
<td align = right><b>Age</b></td>
<td align = left><input type=text name="txtAge" size = 5 value="<%=rsValidateInputs("Age")%>"></td>
</tr>
<tr>
<td align = right><b>Interests</b></td>
<td align = left></td>
</tr>
<tr>
<td align = right><b>E-Commerce </b></td>
<td align = left><% If rsValidateInputs("ECommerce") = True Then

Response.Write "<input type=checkbox name='chkECommerce' value='True' CHECKED>"

Else

Response.Write "<input type=checkbox name='chkECommerce' value='True'>"

End If %>
</td>
</tr>

<tr>
<td align = right><b>Small Business </b></td>
<td align = left><% If rsValidateInputs("SmallBusiness") = True Then

Response.Write "<input type=checkbox name='chkSmallBusiness' value='True' CHECKED>"

Else

Response.Write "<input type=checkbox name='chkSmallBusiness' value='True'>"

End If %></td>
</tr>
<tr>
<td align = right><b>Web Development </b></td>
<td align = left><% If rsValidateInputs("WebDevelopment") = True Then

Response.Write "<input type=checkbox name='chkWebDevelopment' value='True' CHECKED>"

Else

Response.Write "<input type=checkbox name='chkWebDevelopment' value='True'>"

End If %></td>
</tr>
<tr>
<td align = right><b>Java </b></td>
<td align = left><% If rsValidateInputs("Java") = True Then

Response.Write "<input type=checkbox name='chkJava' value='True' CHECKED>"

Else

Response.Write "<input type=checkbox name='chkJava' value='True'>"

End If %></td>
</tr>
<tr>
<td align = right><b>C++ </b></td>
<td align = left><% If rsValidateInputs("CPP") = True Then

Response.Write "<input type=checkbox name='chkCPP' value='True' CHECKED>"

Else

Response.Write "<input type=checkbox name='chkCPP' value='True'>"

End If %></td>
</tr>
<tr>
<td align = right><b>Visual Basic </b></td>
<td align = left><% If rsValidateInputs("VisualBasic") = True Then

Response.Write "<input type=checkbox name='chkVB' value='True' CHECKED>"

Else

Response.Write "<input type=checkbox name='chkVB' value='True'>"

End If %></td>
</tr>
<tr>
<td align = right><b>Type of Credit Card</b></td>
<td align = left>
<select size = "1" name="cboCreditCard">
<option value = "Discover" <% if rsValidateInputs("CreditCard") = "Discover" then

Response.Write "Selected"

end if %>>Discover
<option value = "MasterCard" <% if rsValidateInputs("CreditCard") = "MasterCard" then

Response.Write "Selected"

end if %>>Master Card
<option value = "Visa" <% if rsValidateInputs("CreditCard") = "Visa" then

Response.Write "Selected"

end if %>>Visa
</select>
</td>
</tr>

<tr>
<td align = right><b>Extent of Education:</b></td>
<td align = left><input type=radio name="rbEducation" value = "NoCollege"
<% if rsValidateInputs("NoCollege") = True then %>

CHECKED

<% end if %>> No College</td>
</tr>

<tr>
<td align = right></td>
<td align = left><input type=radio name="rbEducation" value = "College"
<% if rsValidateInputs("College") = True then %>

CHECKED

<% end if %>> College</td>
</tr>
<tr>
<td align = right></td>
<td align = left><input type=radio name="rbEducation" value = "Masters"
<% if rsValidateInputs("Masters") = True then %>

CHECKED

<% end if %>> Masters</td>
</tr>
<tr>
<td align = right></td>
<td align = left><input type=radio name="rbEducation" value = "PhD"
<% if rsValidateInputs("PhD") = True then %>

CHECKED

<% end if %>> Ph.D.</td>
</tr>
<tr>
<td align = right><b>Annual Income</b></td>
<td align = left><input type=text name="txtAnnualSalary" size = 10 value="<%=rsValidateInputs("AnnualSalary")%>"></td>
</tr>
<tr>
<td align = right></td>
<td align = left><input type=submit value = "Register"></td>
</tr>
</table>
</font>
</form>

<%End If%>

</body>
</HTML>

 

The logic is pretty simple in that it chooses what happens to the user's efforts based on whether the user's inputs are found in the database table.
  • if the e-mail address and password aren't found then the user is required to try again
  • if the e-mail address and password are found then the form is displayed with what the database currently has for the user

If the user reaches this page it is likely to look like the following.  Notice how the check boxes, combo box and radio buttons are all correctly representing the original inputs.

 

 

Notice we are working with text boxes, combo boxes, check boxes and radio buttons.  We also have a variety of data types such as numeric, money, true/false and date/time.

Example code segments that make use of VBScript in ASPs to coordinate the HTML display with data read from SQL Server has been highlighted in colors.

  • a row of the table that deals with check boxes is in purple
  • a row of the table that deals with option/select boxes is in periwinkle
  • a row of the table that deals with radio buttons is in dark red

Notice how these segments require a mixture of VBScript If - Then - Else statements with criteria based on the entries in the recordset to correctly format the HTML.

The Processing Script Page.  Now we need to develop the ModifyUser.asp which actually is a bit more of a modification of what we did previously than you might think.  Though it largely has a lot of the same code.

This is where we will make use of all our preliminary developments, stored procedures and include file.  Obviously, you need to be able to anticipate certain structuring in order to develop the stored procedures before developing the ASPs.

Here is the ModifyUser.asp which we will discuss after.  You need to change the name of the stored procedure

yourInitials_CheckUnique

and the name of the stored procedure

yourInitials_UpdateUser

 

<%@ Language=VBScript %>
<% Option Explicit %>
<!--#include File="adovbs.inc"-->
<!--#include File="ConnectToUserRegistration.asp"-->

<HTML>
<BODY>
<%


' We first want to check to make sure the user has entered all the required information

Dim rsRegisteredUsers, rsCheckUnique, sqlStatement, mustGoBack

mustGoBack = false

If ((Request.Form("txtFirstName") = "") OR (Request.Form("txtLastName") = "") OR _
(Request.Form("txtEMail") = "") OR (Request.Form("txtPassword") = "")) Then

Response.Write "<center><font size = 5>You must enter all of the following <BR></font>"
Response.Write "<font size = 5>First Name and Last Name <BR> EMail Address"
Response.Write "<BR> Password and UserName"
Response.Write "<P>Click on back to go back to the<BR>"
Response.Write "cached input form or click on<BR>"
Response.Write "<A HREF='ModifyUser.html'>"
Response.Write "to start over"
Response.Write "</A></center>"

Else

' If they have entered all of the required information we want to make sure they
' are not using a username that someone else has chosen.
' While this is helped by using the username as the key
' it still is much better to catch the error before it befuddles the user

' Obtain the email address from the form
Session("EMail") = "'" & Request.Form("txtEMail") & "'"

If Session("EMail") <> Session("OriginalEMail") Then

' Instantiate a recordset object and access the info from the RegisteredUsers table

Set rsCheckUnique = Server.CreateObject("ADODB.Recordset")
' Call the stored procedure to insert the new
' item into the database table
sqlStatement = "execute yourInitials_CheckUnique " & _
Session("EMail")

' Execute the SQL statement
Set rsCheckUnique = connUserRegister.Execute(sqlStatement)

If Not rsCheckUnique.EOF Then

' Then this e-mail address already exists in the database
Response.Write "<center><font size = 5>This email address already<BR></font>"
Response.Write "<font size = 5>exists in our database."
Response.Write "<P>Click on back to go back to the<BR>"
Response.Write "cached input form or click on<BR>"
Response.Write "<A HREF='ModifyUser.html'>"
Response.Write "to start over"
Response.Write "</A></center>"

mustGoBack = True

' Clean up the ADO objects
rsCheckUnique.Close
Set rsCheckUnique = Nothing

End If

End If

If not mustGoBack Then

' Obtain the rest of the inputs from the form
' and get them into a form so they can be
' inserted into the database
Session("FirstName") = "'" & Request.Form("txtFirstName") & "'"
Session("MiddleName") = "'" & Request.Form("txtMiddleName") & "'"
Session("LastName") = "'" & Request.Form("txtLastName") & "'"
' already obtained email
Session("Password") = "'" & Request.Form("txtPassword") & "'"
' notice leaving out date registered
Session("Age") = Request.Form("txtAge")

If Request.Form("chkECommerce") = "True" Then
Session("ECommerce") = 1
Else
Session("ECommerce") = 0
End If

If Request.Form("chkSmallBusiness") = "True" Then
Session("SmallBusiness") = 1
Else
Session("SmallBusiness") = 0
End If

If Request.Form("chkWebDevelopment") = "True" Then
Session("WebDevelopment") = 1
Else
Session("WebDevelopment") = 0
End If

If Request.Form("chkJava") = "True" Then
Session("Java") = 1
Else
Session("Java") = 0
End If

If Request.Form("chkCPP") = "True" Then
Session("CPP") = 1
Else
Session("CPP") = 0
End If

If Request.Form("chkVB") = "True" Then
Session("VisualBasic") = 1
Else
Session("VisualBasic") = 0
End If

Session("CreditCard") = "'" & Request.Form("cboCreditCard") & "'"

' Trying to deal with a radio button
If Request.Form("rbEducation") = "NoCollege" Then

Session("NoCollege") = 1
Session("College") = 0
Session("Masters") = 0
Session("PhD") = 0

ElseIf Request.Form("rbEducation") = "College" Then

Session("NoCollege") = 0
Session("College") = 1
Session("Masters") = 0
Session("PhD") = 0

ElseIf Request.Form("rbEducation") = "Masters" Then

Session("NoCollege") = 0
Session("College") = 0
Session("Masters") = 1
Session("PhD") = 0

ElseIf Request.Form("rbEducation") = "PhD" Then

Session("NoCollege") = 0
Session("College") = 0
Session("Masters") = 0
Session("PhD") = 1

End If

Session("AnnualSalary") = Request.Form("txtAnnualSalary")

' Inserting the values into the database
Set rsRegisteredUsers = Server.CreateObject("ADODB.Recordset")
' Call the stored procedure to insert the new
' item into the database table
sqlStatement = "execute
yourInitials_UpdateUser " & _
Session("OriginalEMail") & ", " & _
Session("OriginalPassword") & ", " & _
Session("FirstName") & ", " & _
Session("MiddleName") & ", " & _
Session("LastName") & ", " & _
Session("EMail") & ", " & _
Session("Password") & ", " & _
Session("Age") & ", " & _
Session("ECommerce") & ", " & _
Session("SmallBusiness") & ", " & _
Session("WebDevelopment") & ", " & _
Session("Java") & ", " & _
Session("CPP") & ", " & _
Session("VisualBasic") & ", " & _
Session("CreditCard") & ", " & _
Session("NoCollege") & ", " & _
Session("College") & ", " & _
Session("Masters") & ", " & _
Session("PhD") & ", " & _
Session("AnnualSalary")

' Execute the SQL statement
Set rsRegisteredUsers = connUserRegister.Execute(sqlStatement)

Response.Write "<P><center><font size = 4><a href='RegisterUserHome.html'>" & _
"Registered Users Home</a></font></center>"

End If

End If

connUserRegister.Close
Set connUserRegister = Nothing


%>
</body>
</HTML>

 

This code can be broken down into several segments.

The largest segment is included in an If - Then - Else - End If block that tests for whether the user has entered the required inputs. 

  • If they haven't then they are displayed a message about what is missing and directed to go back to the form by one means or another.
  • If they have entered the required inputs then they are put into a section of code that then
    • tests to see if the user's e-mail address has been used before using the yourInitials_CheckUnique stored procedure while passing the e-mail address  obtained from the form stored in a session variable.
    • This leads us into another If - Then - Else - End If block that tests whether the stored procedure returns any records.
      • If it does then they are directed back to the form by one way or another.
      • If e-mail address hasn't been used previously
        • all of the values from the form are read into session variables
          • notice how the non-numeric inputs are enclosed between a pair of ' marks.
          • some of the logic gets clever in order to get the values into a numeric form that SQL Server will accept
            • this is particularly true for the check boxes
            • and the radio buttons
        • then the session variables are fed into the yourInitials_UpdateUser stored procedure in order to modify the user's stats in the database table.

Notice that even with this much error handling the logic can get quite nested.  Notice how we really needed two different stored procedures.