This sample code shows how to store errors in SQL Server Database. Because of this user will never get yellow page/red page error. It will store "Page Name","Message","Stack trace"and "Date" to the Database. This code has to be included in page base class. To save the error details we have to create an Error table with "Id","Error" columns
protected override void OnError(EventArgs e) { HttpContext ctx = HttpContext.Current;
Exception exception = ctx.Server.GetLastError();
string errorInfo = "\n" + "Offending URL: " + ctx.Request.Url.ToString() + "\n" + "Source: " + exception.Source + "\n" + "Message: " + exception.Message + "\n" + "Stack trace: " + exception.StackTrace + "\n" + "Date: " + DateTime.Now;
errorInfo = errorInfo.Replace("'","''");
System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(); System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader(); myConnection.ConnectionString = ((string)(configurationAppSettings.GetValue("sqlConnection1.ConnectionString", typeof(string)))); string myQuery = "insert into error values('" + errorInfo + "')"; System.Data.SqlClient.SqlCommand myCommand = new System.Data.SqlClient.SqlCommand(myQuery, myConnection); try { myCommand.Connection.Open(); myCommand.ExecuteNonQuery(); } catch{} finally { myCommand.Connection.Close(); } Response.Redirect("~/errorpage.aspx"); }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|