Solved

try catch email errors

Posted on 2011-03-08
6
354 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 30

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add more rows to hierarchy 3 35
Need to Understand Resolution to Oracle Error ORA-00600 2 45
Password_rules_securitty.. 12 35
ComboBox to String Not Working 5 21
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
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

730 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