Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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.
0
shpresa
Asked:
shpresa
1 Solution
 
RainverseCommented:
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
 
jagssiduralaCommented:
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
 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
shpresaAuthor Commented:
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
 
RainverseCommented:
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
 
shpresaAuthor Commented:
Easy and strait forward solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now