Making Use of the Logon Information

Having identified our users as they log on, what are we actually going to do with the information? This is open to the design of your applications, and it's not possible to lay down criteria, methods, or examples that are going to be specific to any individual's needs. However, there are a few obvious details that we should consider.

Setting User Preferences

Recall that when we logged an existing user into our application, we extracted the single record from the UserDetail table in our database that contained their user name and password. This record could just as easily contain a great deal more about the user, such as their department, workgroup, page color preferences, or even the name of their dog. All we need to do is change the SQL query to include all the fields in the recordset, and then capture the other details in Session level variables ready for use while the user is touring our site:

strSQL = "SELECT * FROM UserDetails WHERE UserName = '" _
       & Request.Form("txtUserName") & "')"
...
...
Session("BackColor") = oUserRs.Fields("BackColor")
Session("FontColor") = oUserRs.Fields("FontColor")
Session("DogsName") = oUserRs.Fields("DogsName")
...

Now, we can really make them feel at home:

<HTML>
<HEAD>
<TITLE> Welcome to the Wrox Pets Supermarket </TITLE>
</HEAD> 
<BODY BGCOLOR=<% = Session("BackColor") %>>
  <FONT FACE=Arial COLOR=<% = Session("FontColor") %>>
  <CENTER><IMG SRC="welcome.gif"><CENTER><P>
  <H3> Great News... </H3> We now have a new variety of Scrummy<BR>
  <I><B>the dog food for champions</B></I><BR>
  available from the new products page.<P>
  Why not take some home for<B> <% = Session("DogsName")%> </B>- we 
  <I>guarantee</I> it will be a hit!
</BODY>
</HTML>

Checking the Success of Object Creation

One thing we regularly do in the Application_onStart and Session_onStart events is create instances of objects that we need to use throughout the application. In our logon example, we are also creating objects to check the user's identity—database connections and recordsets. If any of these processes fail, the state of our application is invalid. In this case, with the login page for example, we must be sure to set the 'logged on' status to False.

The easiest way is to examine the object variable that is now supposed to be referencing the object instance. We can use either IsEmpty() or IsObject() for this, or compare it the standard value Nothing. It depends on whether we are referring to the object itself or a variable that holds a reference to it. In both cases, for it to work, we have turn off error checking with an On Error Resume Next statement first:

On Error Resume Next
Set oConn = Server.CreateObject("ADODB.Connection")
If IsObject(oConn) Then
  '... success, OK to continue 
Else
  '... error, could not create object
End If
On Error Resume Next
Set Session("MyConnection") = Server.CreateObject("ADODB.Connection")
'now check if it failed
If Session("MyConnection") Is Nothing Then 
  '... got an error
'or use this method
If IsEmpty(Session("MyConnection")) Then 
  '... got an error

© 1997 by Wrox Press. All rights reserved.