简体   繁体   English

如何捕获错误并将错误详细信息保存在数据库中并重定向到经典ASP中的自定义错误页面?

[英]how to trap an error and save error detail in database and redirect to custom error page in classic asp?

how to trap an error and save error detail in database and redirect to custom error page in classic asp? 如何捕获错误并将错误详细信息保存在数据库中并重定向到经典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. 我希望在我网站的所有页面中都包含一个ASP页面,如果发生任何错误,它将获取该错误详细信息,将其保存到数据库或通过邮件发送到邮件ID并重定向到自定义错误页面。 Please if you have any idea then please help me. 请如果您有任何想法,然后请帮助我。

use try catch methods in javascript to catch the errors. 在javascript中使用try catch方法来捕获错误。 Within the catch block post the data to the server. 在catch块中,将数据发布到服务器。 In server have an aspx page or php page which one you are familiar. 在服务器中有一个您熟悉的aspx页面或php页面。 Get the data to be inserted as parameters from this post back in that aspx/php file and from that file insert into DB. 从该帖子中获取要作为参数插入的数据,返回该aspx / php文件,并将该文件插入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();
      }

}

Classic ASP has no try/catch. 经典ASP没有尝试/捕获。 It also uses VBscript by default and the answer above is, I'm guessing, C#? 默认情况下,它也使用VBscript,上面的答案是C#? Here is VBscript ASP for what you are trying to do: 这是您要执行的操作的VBscript ASP:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM