When error coding in ASP it’s not as rich an environment as other environments. I really only reports that there was an error with some Numbers and Descriptions. There is only a few way's I've found to report these errors back to the end user. I've seen numerous ways of doing it but found this way the most graceful. Remember you have to explicitly check after everything that might cause an error. The main ones I've experiences are database openings and recordset openings & updates. Here is the sample code I use to check for errors and then redirect them to the error page and record the error into a database.. Note that all my error checking is done before the <html> header is written so if there is an error it can redirect the page without getting an error of Heading already written to the client. If the html header has been sent to the client you can't do a response.redirect command. Page 1 A sample Active Server Page form you would use to submit data<html>
<head> <title>Enter some data into the field</title> </head>
<body>
<p>Enter some data into the field. This form is nothing more than representing a form you would use in real life to submit some data to an ASP page. Note this isn't going to enter the data into database but it will record the error on an Error page and then the some information about the Error. </p>
<form method="POST" action="error2.asp" name="form1"> <div align="left"><table border="1" width="340" height="35"> <tr> <td width="143" height="11">Favorite Computer</td> <td width="185" height="11"><input type="text" name="T1" size="20"></td> </tr> <tr> <td width="143" height="12">Favorite Game: </td> <td width="185" height="12"><input type="text" name="T2" size="20"></td> </tr> </table> </div><p>:<input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> </form> </body> </html> Page 2 the form that is being submitted to and also generates the error that redirects it to the Standard Error Page (Which is Page 3 in this example) <%@ Language="vbscript"%> <% 'Hold the page in memory until response.flush command is issued or the </html> tag is processed. Response.buffer = True
'This forces the page to continue to process even though there was an error. On Error Resume Next
'Declare all variables dim conn dim rs set conn = server.createobject("adodb.connection") conn.open "Example_DSN"
'Standard Error coding if the database won't open an error number will return something else but zero 'I then capture the error number and description and is passed using the querystring method 'Note the description is using the Server.URLEncode function ('This will fill any spaces in the description with 'the correct HTML code If err.number <> 0 Then Response.Redirect "Error3.asp?number=" & err.Number & "&desc=" & Server.URLEncode(err.description) End If set rs = server.createobject("adodb.recordset") rs.open "TableName" conn 3 3 'Explicitly checks to see if there is a problem opening the table If err.number <> 0 Then Response.Redirect "Error3.asp?number=" & err.Number & "&desc=" & Server.URLEncode(err.description) End If
rs.addnew rs("field1") = request.form("field1") rs("field2") = request.form("field2") rs.update
'Explicitly checks to see if there is a problem updating the record If err.number <> 0 Then Response.Redirect "Error3.asp?number=" & err.Number & "&desc=" & Server.URLEncode(err.description) End If rs.close conn.close set rs = nothing set conn = nothing %> <html> <head> <title>Records been added</title> </head>
<body>
<p>Your record has been added to the database!</p> </body> </html> Standard Error coding page I use in most all my apps! You also could easily create some kind of database connection and report the errors your getting! <%@ language="vbscript"%> <% 'buffers the page on the server Response.Buffer = True
'Declare variables dim strNumber dim strdesc dim conn dim rs
'sets a local variable to the connection string strconn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("error.mdb")
'Place values that are in the URL into local variables strNumber = request("Number") strDesc = request("Desc")
'Opens the connection string and recordset object to record the error in a database set conn = server.createobject("adodb.connection") conn.open strconn set rs = server.createobject("adodb.recordset") rs.open "tblError", conn, 2, 2 rs.addnew rs("ErrNumber") = strNumber rs("ErrDesc") = strDesc rs("timeoccurred") = now() rs.update rs.movelast 'Puts the generated ID into a local variable strID = rs("id") rs.close set rs = nothing conn.close set conn = nothing
'Clear errors collections err.clear
%> </p>
<html> <head> <title>Error page</title> </head> <body>
<h1>An Error has occurred</h1> 'Writes out the generated Number that is received from the database 'Idea you also could format an email message with this id to report the error to someone <h2>Error ID is:<% = strID %></h2>
<h3>The Error Number is:</h3> <i><% = strNumber %> </i>
<h3>The Error Description is:</h3> <i><% = strDesc %> </i>
<h3>Please report this error to the webmaster</h3> <b><a href="mailto:webmaster@someurl.com">
<p>Click here to send an email please report the Error Number and Description</a></b> </p> </body> </html>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP Code Articles More By aspfree developerWorks - FREE Tools! | This demonstration gives you an overview of IBM® Rational® Build Forge Express Edition, a global offering that provides a framework to automate and execute software processes. Rational Build Forge provides a software assembly line that can support all of your tools, technologies, and platforms so you can achieve a repeatable, reliable, and traceable build and release process. FREE! Go There Now!
| | | | Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services. FREE! Go There Now!
| | | | CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
| | | | Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects. FREE! Go There Now!
| | | | Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
| | | | Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
| | | | This paper is about the critical role that a discipline called integrated requirements management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrating, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way. FREE! Go There Now!
| | | | Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads. FREE! Go There Now!
| | | | In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |