欢迎光临
我们一直在努力

ASP中的错误代码技巧

建站超值云服务器,限时71元/月

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 ways ive found to

report these errors back to the end user. ive 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 ive 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 cant 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.&nbsp; this form is nothing more than representing a

form you would use in real life to submit some data to an asp page.&nbsp;&nbsp; note this

isnt 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.&nbsp; &nbsp; </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 wont 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>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP中的错误代码技巧
分享到: 更多 (0)