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"); }
|
| Author: Balthazor 10 Mar 2008 | Member Level: Diamond Points : 0 |
I have tried your code for error, That works cool.
Is it worth to show which error is getting?
|
| Author: Jessica 10 Mar 2008 | Member Level: Gold Points : 0 |
Thanks for feedback. Yes it is very useful, to store error in DataBase. Users generally frustrate when they get yellow/red page . But we can avoid to arise this situation by putting the code given in Page Base class. and simply redirect the page to Error page. So we can catch the real problem in the system.
I hope you will find this informative.
regards.
|