Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

try catch email errors

Posted on 2011-03-08
6
Medium Priority
?
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1000 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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 setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

610 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