Solved

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

Posted on 2011-09-13
6
255 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now