Link to home
Start Free TrialLog in
Avatar of shpresa
shpresaFlag for United States of America

asked on

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

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.
Avatar of Rainverse
Rainverse

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
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
Avatar of SAMIR BHOGAYTA
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.
Avatar of shpresa

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Rainverse
Rainverse

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shpresa

ASKER

Easy and strait forward solution.