C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to save application errors(Exceptions occurs) in SQL Server Database


Posted Date: 05 Mar 2008    Resource Type: Code Snippets    Category: Exceptions

Posted By: Jessica       Member Level: Gold
Rating:     Points: 10



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");
}




Responses

Author: Balthazor    10 Mar 2008Member 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 2008Member 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.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Exceptions in C#
Previous Resource: Writing Log for the applications
Return to Discussion Resource Index
Post New Resource
Category: Exceptions


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use