Solved

How to catch errors while executing on C# and storing the errors in a database

Posted on 2011-09-13
6
257 Views
Last Modified: 2013-12-17
Hi I created this table called tbl_Error and i want automatically anytime I execute store procedures in my web applications to store the following data errors
time                storeProc                       error
---------------------------------------------------------------------------------
4/5/2011            sp_SurveyUpdate      Error converting data type nvarchar to int.

Any suggestion where I can instert this fields. I know it is duable.
0
Comment
Question by:shpresa
6 Comments
 
LVL 5

Expert Comment

by:Rainverse
ID: 36531316
Just write a logging class with a method that stores this information in a db, and call that in the "Catch" of any method where you're executing a stored proc (or any other method where you want to log errors). There are a few free frameworks for this kind of thing. Microsoft's own Enterprise Library has a logging feature. Scott Hanselman's ELMAH: http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx

-MJC
0
 
LVL 12

Expert Comment

by:jagssidurala
ID: 36531346
we can achive this error logging 2 ways

1) UI Level

write try catch blocks for every method which is having DB intaraction.
In Catch black you need to call a DB to insert the error and time, any other details

2) DB Level

Write below code

For ex:

Insert
Into
.
.
.
.

If @@Error > 0
Begin
      Insert
      Into tbl_Error
      .
      .
      .
      .

      Select 0 As Success
End
else
Begin
      Select 1 As Success
End
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 36531405
Hello,

You have to put every possiblities of error are store into the Exception and then after you have to store this exception values into the database. You also create a log file of errors.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:shpresa
ID: 36532286
Thank you for the solutions experts.

To jagssidurala:
I find it easier to follow the DB Level. Do I write that store procedure and call it somewhere in the coding..if so where ?


Thanks in advance
0
 
LVL 5

Accepted Solution

by:
Rainverse earned 500 total points
ID: 36532316
You would add that logic to the end of any stored proc you're wanting to capture errors in. This would not, however, capture errors in the code calling the stored proc.  
0
 

Author Closing Comment

by:shpresa
ID: 36532459
Easy and strait forward solution.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question