Solved

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

Posted on 2011-09-13
6
254 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

18 Experts available now in Live!

Get 1:1 Help Now