Solved

try catch email errors

Posted on 2011-03-08
6
353 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
ID: 35074116
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
ID: 35074329
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
ID: 35075014
@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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 74

Expert Comment

by:sdstuber
ID: 35082369
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
ID: 35101095
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
ID: 35101201
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
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 video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

856 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