Solved

try catch email errors

Posted on 2011-03-08
6
348 Views
Last Modified: 2012-05-11
need to add a feature to this program to catch any database errors that crop up and fire off an email when they occur, below is the block of code I am working with. Would it be best just to add another method? C# isn't my best area so apologies for simpleness of this question.

Thanks in advance.
OracleConnection OraConn2 = new OracleConnection(connectionString);
            OracleCommand OraCmd2 = new OracleCommand("Select * from table1", OraConn2);
            OraConn2.Open();
            OracleDataReader OraDr = OraCmd2.ExecuteReader();

            try
            {
                while (OraDr.Read())
                {
                    try
                    {
                        OraCmd.CommandText = "CALL X.Y.IMPORT.INS(" + OraDr.GetValue(0).ToString().Trim() + ")";
                        OraCmd.ExecuteNonQuery();


                    }
                    catch (Exception ex)
                    {
                        lbResult.Items.Add(ex.ToString());
                    }
                }
                ok = true;
            }
            catch (Exception ex)
            {
                lbResult.Items.Add(ex.ToString());
                ok = false;
            }

Open in new window

0
Comment
Question by:mtnseeker
6 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
Comment Utility
You could do, it would certainly make it easier to maintain. You could even create a separate class with a static method which you can then call anytime you want to record an exception.

Something like:
public static class Logger
{
     public static void LogException(Exception ex)
     {
            // do something to log the exception, email, etc
     }
}

Open in new window

The call it like:
                    try
                    {
                          // do something that might throw an exception
                    }
                    catch (Exception ex)
                    {
                        Logger.LogException(ex);
                        throw;     // rethrow the exception if you want your app to handle it elsewhere too
                    }

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
My opinion - nothing really wrong with this code.

With inner try/catch you handle sql exceptions, with outer - IO exceptions. Everything is OK. Of course, you may move part of your code to a separate method but this won't change anything...
0
 

Author Comment

by:mtnseeker
Comment Utility
@anarki: right there is nothing wrong with the code I just need to figure out how to manipulate it to send any exceptions to an email.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
rather than pulling the data out of the database and then calling a procedure to push that data back to the database and then raising exceptions to be pushed back to the client app why not simply call one procedure that processes the data within the database and sends the email itself with utl_smtp or utl_mail?
0
 

Author Comment

by:mtnseeker
Comment Utility
so been trying to figure out how to do this and looking at creating a logger class to do this, basically here's where I'm at. I just need to figure out how to get the exception into the email. Do I need to first save the exceptions into a log or can I just email the exceptions straight through? Should note I am using system.web.mail


Thanks


class Logger
    {
        public static void LogException(Exception ex)
        {

            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("x");

            mail.From = new MailAddress("x");
            mail.To.Add("x");
            mail.Subject = "Import Error";
            mail.Body = "test";

     
            SmtpServer.Send(mail);


        }

Open in new window

0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 250 total points
Comment Utility
You can dump it straight into the email. The actual message part of the exception is just a string.
mail.Body = "An exception occurred: " + ex.Message;

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

762 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

6 Experts available now in Live!

Get 1:1 Help Now