设为首页
加入收藏
帮助中心
首页 | 红盾通告 | 信息中心 | ASP技术 | 数据库 | 网页设计 | 网管专栏 | OICQ攻略 | 墨客频道 | 网站运营 |
当前位置:首页 >> ASP技术 >> ASP技巧 >> 正文
最新信息
·ASP防止采集的代码
·文字防复制乱码
·关于ASP中的UTF8编码
·ASP精典之常用函数收集
·用正则式除去HTML标记
·ASP国际化多语言详细方案
·改进 ASP 的字符串处理性能
·ASP缓存技术
·巧用ASP技术保护DHTML源代…
·ASP读取系统时区的错误行为…
资料搜索
热点信息
·删除数组中的重复元素
·ASP中转换unicode码为GB码…
·asp得到当前页地址
·关于ASP中的UTF8编码
·判断上一页的来源
·Asp中有关字符编码转换的几…
·用正则式除去HTML标记
·ASP 三十二条精华代码
·动检测上传文件是否含有非…
·常用过滤函数
推荐信息
·ASP 三十二条精华代码
·ASP中转换unicode码为GB码…
·如何拒绝同一张表单被多次…
·常用过滤函数
·常用asp代码
·Asp中有关字符编码转换的几…
·获得上一个月几月
·计算文件下载时间
·三条语句搞定路径
·判断上一页的来源


Google
 
ASP中的错误代码技巧
〖编辑:Cloudy | 浏览:人次〗

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.&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
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.&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 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>


录入时间:2006-06-04 18:57:36 [打印本页] [关闭窗口] [返回顶部]
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。

Copyright © 2006-2014 0733168.Com Inc All Rights Reserved
关于我们 | 广告合作 | 联系我们 | 法律声明 | 友情链接 | 意见反馈
本站所收录信息、社区话题、及本站所做之广告均属其个人行为,与本站立场无关
湘ICP备06008436号