问题
how to trap an error and save error detail in database and redirect to custom error page in classic asp?
I want that I should include a asp page in all page of my website and if any error occur it get that error detail, saved it to database or mail it to mail id and redirect to custom error page. Please if you have any idea then please help me.
回答1:
Classic ASP has no try/catch. It also uses VBscript by default and the answer above is, I'm guessing, C#? Here is VBscript ASP for what you are trying to do:
<%
Set conn = Server.CreateObject("ADODB.Connection")
SQL_server_string = "Provider=SQLOLEDB; Data Source=myMachine; Initial Catalog=pubs; User ID=sa; Password=pw"
ConnectionString = SQL_server_string
conn.Open ConnectionString
s = "INSERT INTO"
s = s & " tablename "
s = s & "("
s = s & " fieldname1 "
s = s & ",fieldname2 "
s = s & ") "
s = s & "VALUES"
s = s & "( "
s = s & "'" & stringvalue1 & "'"
s = s & ",'" & stringvalue2 & "'"
s = s & ") "
conn.execute(s)
if (err.number<>0) then
m = "error on page ___ line ____<br>"
m = m & "error number: " & err.number & "<br>"
m = m & "error description: " & err.description & "<br>"
m = m 7 "sql: " & s & "<br>"
session("msg") = m
set conn=nothing
response.redirect("error_report.asp")
end if
'got past error checking... do stuff...
%>
回答2:
use try catch methods in javascript to catch the errors. Within the catch block post the data to the server. In server have an aspx page or php page which one you are familiar. Get the data to be inserted as parameters from this post back in that aspx/php file and from that file insert into DB.
<script>
var txt="";
function ProcessData()
{
try
{
....
}
catch(err)
{
var message = err.message;
$ajax({
type:"POST",
url:"Errorhandler.aspx/InsertErrordetails",
data:"{error: '" + message + "'}",
clientType:"application/json; charset=utf-8",
datatype:"json",
async: false,
success: function () { alert("success"); },
error: function(){alert("error");}
});
}
}
</script>
The server side code appears like this.
public static void InsertErrordetails(string error)
{
SqlConnection Con = new SqlConnection(@"Server=db;Integrated Security=True;" + "Database=userdb");
string query = "INSERT INTO [LogTable]([Message])" +
"VALUES (@error)";
SqlCommand cmd = new SqlCommand(query, Con);
cmd.Parameters.AddWithValue("@Message", error);
try
{
Con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
Con.Close();
}
}
来源:https://stackoverflow.com/questions/16234447/how-to-trap-an-error-and-save-error-detail-in-database-and-redirect-to-custom-er