Solved

try catch email errors

Posted on 2011-03-08
6
351 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 73

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
data lookup in Oracle - need suggestions 55 102
Export table into csv file in oracle 10 45
Not showing JavaScript in the list 5 37
SQL Developer 6 32
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

911 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

24 Experts available now in Live!

Get 1:1 Help Now