Error Handling

ASP provides you with several scripting objects that help with various tasks. One of them is the Err object, which helps you handle errors.

If you try to reload your page after you've already submitted it once, you get an error message. That message isn't very informative, however, and it's certainly not something we want our users to see. We can, however, handle this ourselves and avoid the error page.

You can handle the error with an On Error statement. What this means is that you tell ASP what to do if an error occurs. In VBScript, you don't have many error handling statements to choose from, but you can make do with On Error Resume Next. What this tells ASP is to keep going when it gets to an error. Add this to your page (see Listing 4.15).

Code Listing 4.15. take_registration.asp: Avoiding the error page
0: <%@ LANGUAGE="VBSCRIPT" %>
1: <!--#include virtual="/pagetop.txt"-->
2: <%
3:     On Error Resume Next
001 4:
002 5:     p_userid = Request.querystring("p_name")
003 6:     p_pass1 = Request.querystring ("p_pass1")
004 7:     p_pass2 = Request.querystring ("p_pass2")…
005 

If you reload again, you won't get the error, but you won't get any indication that something bad happened, either. Now that you took that notification out of ASP's hands, you need to check for it yourself.

When an error occurs, three pieces of information are recorded in the Err object: the error number, a description of the error, and the source of the error. If there is no error, the error number is zero, so you can do the insert, and then check this value to see if it was successful, as in Listing 4.16.

Code Listing 4.16. take_registration.asp: Checking for errors
…
26:    for each p_medium in Request.querystring("p_medium")
27:        theSQL = "insert into userid_medium (userid, medium) values —('"&p_userid&"',
 '"&p_medium&"')"
28:        outpostDB.Execute(theSQL)
29:    next
30:
31:    outpostDB.close
32:    set outpostDB = Nothing
33:%>
34:
35:<%  if Err.number = 0 then
36:
37:        'All is well with the world %>
38:
39:        <H2>User Registration</H2>
40:        Thank you for registering with Primary Outpost!
41:
42:<%  else %>
43:
44:        'There was a problem with their registration
45:
46:        <h2>Problem</h2>
47:        There was a problem with your registration.
48:        Please go back and choose a different username.
49:
50:<%  end if %>
51:
52:
53:<!--#include virtual="/pagebottom.txt"-->
54:</BODY>
55:</HTML>

On line 35 we check for any errors, then display the proper message. While you check for errors, make sure that the user didn't mistype his or her password before you allow them to register. We can do this with the second if-then-else statement on lines 12 and 53 through 58 of Listing 4.17.

Code Listing 4.17. take_registration.asp: Making sure the passwords match
0: <%@ LANGUAGE="VBSCRIPT" %>
1: <!--#include virtual="/pagetop.txt"-->
2: <%
3:     On Error Resume Next
4:
5:     p_userid = Request.querystring("p_name")
6:     p_pass1 = request. querystring ("p_pass1")
7:     p_pass2 = request. querystring ("p_pass2")
8:     p_first = request. querystring ("p_first")
9:     p_last = request. querystring ("p_last")
10:    p_email = request. querystring ("p_email")
11:
12:    if p_pass1 = p_pass2 then
13:
14:        set outpostDB = server.createObject("ADODB.Connection")
15:        outpostDB.open "outpost"
16:
17:        theSQL = "insert into members "
18:        theSQL = theSQL & "(username, password, first_name, last_name,
19:        theSQL = theSQL & "email, believes_in_aliens)"
20:        theSQL = theSQL & " values ('"&p_userid&"', '"&p_pass1&"', '"
21:        theSQL = theSQL & p_first&"', '"&p_last&"', '"&p_email&"', '"
22:        theSQL = theSQL & p_believes_in_aliens&"')"
23:
24:        outpostDB.Execute(theSQL)
25:
26:        for each p_medium in Request.form("p_medium")
27:            theSQL = "insert into userid_medium (userid, medium) values ('"
28:            theSQL = theSQL & p_userid&"', '"&p_medium&"')"
29:            outpostDB.Execute(theSQL)
30:        next
31:
32:        outpostDB.close
33:        set outpostDB = Nothing
34:%>
35:
36:<%         if Err.number = 0 then
37:
38:            'All is well with the world %>
39:
40:            <H2>User Registration</H2>
41:            Thank you for registering with Primary Outpost!
42:
43:<%        else %>
44:
45:            'There was a problem with their registration
46:
47:            <h2>Problem</h2>
48:            There was a problem with your registration.
49:            Please go back and choose a different username.
50:
51:<%         end if
52:
53:    else
54:        'p_pass1 doesn't match p_pass2
55:        <h2>Password Error</h2>
56:        Both entries for your password must match.

57:        Please try again.  Thank you!
58:    end if
59:%>
60:
61:
62:<!--#include virtual="/pagebottom.txt"-->
63:</BODY>
64:</HTML>

Notice that you nested the if-then statement. That means that you have statements one inside the other, like a Russian doll. This syntax is perfectly acceptable and is a good argument for commenting your code and keeping it neat. Few things are more frustrating than getting an error message and not knowing where it's coming from because your code is unintelligible.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.7.22