Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old May 13th, 2012, 04:43 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
I noticed that this hadn't happened when my key was set incorrectly.
This is only the case for exception in Application_Error. To avoid endless loops, you won't see the Exception occur for error *on* Application_Error. However, for the other properties, such as the user name and e-mail address access in the contact form, the error is handled correctly by Application_Error (unless it raises an error again because of an incorrect key).

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #12 (permalink)  
Old May 13th, 2012, 06:30 AM
Authorized User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar,

I changed my FromAddress key in web.config to Address, when I sent the contact form I discovered that the exception wasn't thrown because the error was being caught by the catch in ContactForm.ascx.cs. So I commented out the catch, then the program paused at the thrown exception (in debug mode). Then, when I run normally in Internet Explorer I receive the error in a Webpage Error dialog box that states "Line: 885 Error: Sys.WebForms.PageRequestManagerParserErrorExceptio n", not my exception of "AppSetting FromAddress not found in web.config file." Is this expected? Also I tried viewing this in Firefox and Chrome, and they don't report any error message at all.

Kind regards,

Julian
Reply With Quote
  #13 (permalink)  
Old May 13th, 2012, 07:15 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Julian,

Yes, that's all expected behavior. Application_Error is for unhandled exceptions. By having a Try / Catch statement you handle the erorr and thus Application_Error never fires.
Also, since the contact form is inside an UpdatePanel, the error is converted into JavaScript. Other browsers encounter the same error but may not show a dialog. Using the dev tools in Chrome or Firebug in Firefox you should see the error as well.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #14 (permalink)  
Old May 13th, 2012, 07:31 AM
Authorized User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Thanks for explaining that. Just one last thing that has me slightly confused, what is the point of putting text, e.g. "AppSetting FromAddress not found in web.config file.", in the exception if it isn't displayed, or is this text for use in debug mode only?

Kind regards,

Julian
Reply With Quote
  #15 (permalink)  
Old May 13th, 2012, 08:32 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

The AppSetting properties and the concept of raising an exception when a key is not found is not limited to the contact form. You could use it in other scenarios where you would see the error. In the case of the contact form, the code just happens to have a try/catch block that swallows all exceptions or hides them using an UpdatePanel. You could update the catch block to only catch smtp exceptions so you would still see the other exceptions.

Alternatively (and that's something I often do), you can write some code in Global.asax at startup that tries to read from all properties. That way, the site won't even start when you have a missing key so you can immediately fix the problem. Something like this should work:

Code:
 
<%@ Import Namespace="System.Reflection" %>
...
  void Application_Start(object sender, EventArgs e)
  {
   foreach (PropertyInfo info in typeof(AppSettings).GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly))
    {
      info.GetValue(null, null);
    }
  }
This code loops over all properties in the AppSettings class and reads their value. It ignores the result, as the single purpose of this code is to check whether the properties raise an exception or not.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #16 (permalink)  
Old May 13th, 2012, 11:07 AM
Authorized User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Sorry, I'm still having problems with this. I couldn't work out your example, it's a bit too advanced for me. What I have done is this. I have created a new blank page (so I shouldn't get confused with the error being converted to JavaScript) with a button, on the on-click event I have added this code:

Code:
    protected void Button1_Click(object sender, EventArgs e)
    {
      MailMessage myMessage = new MailMessage();
      myMessage.Subject = "Response from web site. Page: ";
      try
      {
        myMessage.From = new MailAddress(AppConfiguration.FromAddress, AppConfiguration.FromName);
      }
      catch
      {
        throw new Exception("AppSetting FromAddress not found in web.config file.");
      }
    }
So, I'm expecting this to throw an exception when I click the button when I don't have the key set correctly in web.config. In debug mode it pauses at the throw new exception line, but in normal mode I just get my custom error page, if I turn custom errors off, I get the yellow screen of death informing me: Server Error in '/' Application.
AppSetting FromAddress not found in web.config file. (<- This is the text I'm looking for, so this looks good!)

Is this how exceptions are shown, as a yellow screen of death, not a dialog box? Also, if I have custom errors on I will never see the exception? I have tried googling this information but can't seem to find it.

Sorry for not grasping this, I'm so close to the end of your book, all up to this point I followed (I think!), but I'm struggling with this bit.

Kind regards,

Julian
Reply With Quote
  #17 (permalink)  
Old May 13th, 2012, 11:26 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Julian,

Quote:
Is this how exceptions are shown, as a yellow screen of death, not a dialog box?
Yes. ASP.NET - as a server side technology - does not have the notion of a message box; only JavaScript does. All server side errors are shown with a YSOD, unless you a) handle the exception (in a catch block) or b) you have custom errors turned on.

Quote:
Also, if I have custom errors on I will never see the exception?
Yes and no. The purpose of the custom error page is to make sure your *users* never see the error. However, in order to know an error occurred in the first place, I set up the code in Application_Error to notify the webmaster of the site of the error. In the example you just described, you won't see the error in the browser with custom errors turned on, but you should receive an e-mail with the error details.

Does that help?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Family (May 13th, 2012)
  #18 (permalink)  
Old May 13th, 2012, 11:42 AM
Authorized User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Excellent, yes that helps a lot, I understand it now. Brilliant, a lightbulb moment!

Thank you for all your help.

Kind regards,

Julian
Reply With Quote
  #19 (permalink)  
Old May 14th, 2012, 04:41 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Excellent. Lightbulb moments is what this is all about ;-)

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
what is global.asax? gaurav_jain2403 General .NET 1 February 3rd, 2006 02:40 PM
global.asax ashokparchuri ADO.NET 1 February 25th, 2005 08:40 AM
Application_Error in global.asax saleyoun@hotmail.com ASP.NET 1.x and 2.0 Application Design 2 April 15th, 2004 03:00 PM



All times are GMT -4. The time now is 02:46 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.