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: 10 Mar 2008    Resource Type: Code Snippets    Category: SQL

Posted By: idris gani       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


No responses found. Be the first to respond and make money from revenue sharing program.

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: Find all store procedure and table details
Previous Resource: Datatypes in SQL Server
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

efax

Contact Us    Privacy Policy    Terms Of Use