ASP中的錯誤碼技巧

來源:互聯網
上載者:User
錯誤|技巧 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>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.