Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Try Catch message 1

Status
Not open for further replies.

princesszea

Technical User
Aug 11, 2008
33
GB
Hi,

I'm using asp.net and I have my try catch is in a class in my DAL which returns a datatable to my user control. If my datatable does not get returned and an error is thrown in my try catch, what i need to happen is display the error message inside that particular user control instead. Not sure how to best achieve this. I was thinking of putting a label inside my user control but not sure how to pass the error message from my class in DAL to my user control.

Thanks
 
there are a number of guidelines on exception handling. researching them would be a good place to start. One approach would be to wrap the ado.net exception with your own exception and let this bubble up to the UI. in the UI catch this message, log the inner exception and display the friendly message to the user. here is an example
Code:
public class MyDalException : Exception
{
   public class MyDAL Exception(string message, Exception inner)
      : base(message, inner)
   {
   }
}

//some DAL code
public DataTable FetchCustomersInState(string state)
{
   try
   {
       var results = new DataTable();
       ...define command
       results.Load(command.ExecuteReader());
       return results;
   }
   catch (SqlException e)
   {
      string messsage = string.Format("Oops. Something went wrong fetching customers in the state of {0}", state);
      throw new MyDalException(message, e);
   }
}
Code:
public override void Load(EventArgs e)
{
   base.Load(e);
   if(IsPostBack) return;

   try
   {
      TheGrid.DataSource = DAL.FetchCustomersInState("PA");
   }
   catch (MyDalException e)
   {
      Logger.Error(e.InnerException.ToString());
      ErrorLabel.Text = e.Message;
   }
}
One thing you may notice is the try/catch blocks will become repetitive. there are ways handle that using concepts like the Decorator pattern, Dependency Injection and AOP (aspect oriented programming). but this example should be enough to get you started.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks Jason,

I seem to be having problems with the code in my ui code

this line is giving me problems ----base.Load(e);

Logger.Error-- Logger is not recognized

my code added is below

Code:
protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!Page.IsPostBack)
            {

            }
            
             base.Load(e); 
if (IsPostBack) return;
            try
            {
                TheGrid.DataSource = DAL.FetchCustomersInState("PA");            }
            catch ( Data.MyDalException ex)
            {
                Logger.Error(ex.InnerException.ToString());
  ErrorLabel.Text = ex.Message;
            }

        }

Thanks for your help
 
in my example I override the Load method, in you're code you are using Page_Load. Logger would be a class you define. this class will handle all the concerns of logging. log4net is a great logging library. I abstract that through the Logger object.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I have this code now and the logger is fine but using override i 'get cannot override because 'System.Web.UI.Control.Load' is not a function'
Code:
public static class Logger  

    {  

       public static void Log(string message)  

       {

           File.AppendAllText("C:\\MyLogFile.txt", DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ":\t" + message + Environment.NewLine); 

       }  

   } 

        public override void Load(EventArgs e)
        {
        
            base.Load(e);
            if (IsPostBack) return;
            try
            {
                TheGrid.DataSource = DAL.FetchCustomersInState("PA");
            }
            catch ( Data.MyDalException ex)
            {
                Logger.Log(ex.InnerException.ToString()); 
                ErrorLabel.Text = ex.Message;
            }

        }

Thanks again
 
Load(EventArgs) is protected, not public.

not to sound rude, but a simple google/msdn search will provide details into the entire .net library. this is invaluable when learning .net.

also, you will have a problem with your logger when you deploy to a server.
1. the log path is hard coded this should be a configurable option as deployment locations will change(local box, test server, production, etc)

the AppSettings node in web.config is the simplest way to add custom configuration.

2. the current path will only work for local use because you're box is the server and the client and you have full access to your box. If you deploy to a server this will fail because IIS doesn't have access to c:\.

I would recommend logging to a sub directory of App_Data. it's within your website/application so IIS will have access to the file. The App_Data directory will not allow users to view the data from the web, so your logs are secure.


Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Hi Jason,

Thanks for your feedback.

But the code above is just sample code and not the actual code i'm using as i never ever hard code paths as like you mentioned it will cause problems when deploying the project.

Thank you so much for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top