Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 24th, 2011, 06:33 PM
Registered User
 
Join Date: Feb 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default AppConfiguration

I am having a problem with the AppConfiguration as referenced in Chapter 19.

I added the following to the AppConfiguration.vb file:
Code:
Public Shared ReadOnly Property FromAddress() As String
    Get
      Dim result As String = WebConfigurationManager.AppSettings.Get("FromAddress")
      If Not String.IsNullOrEmpty(result) Then
                Throw New Exception("AppSetting FromAddress not found in web.config file.")
      End If
      Throw New Exception("AppSetting FromAddress not found in web.config file.")
    End Get
  End Property
It states that by doing this an error would be thrown if FromAddress is not found in the web.config file.

I tried running the app without adding a key to the web.config. The problem is that the site runs normal and no error is thrown. Did I forget a step someplace. I have gone back and forth through this chapter and can't seem to find what I may have missed.

I also took the Final code from the download and removed the FromAddress from its web.config and ran the site and again it ran fine.

Any thoughts?

Thanks!
 
Old February 25th, 2011, 02:11 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Take a look at this:

Quote:
If Not String.IsNullOrEmpty(result) Then
Notice the Not that shouldn't be there? This causes the error to occur when the value is *not* empty, while it should be the other way around.

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!
 
Old February 25th, 2011, 03:53 PM
Registered User
 
Join Date: Feb 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

I pasted the wrong code. I was testing to see if I could get the error to throw in either case. The actual code I am using is which as far as I can see if straight from the book on page 702:

Code:
 Public Shared ReadOnly Property ACHEmail() As String
        Get
            Dim result As String = WebConfigurationManager.AppSettings.Get("ACHEmail")
            If Not String.IsNullOrEmpty(result) Then
                Return result
            End If
            Throw New Exception("AppSetting ACHEmail not found in the web.config file.")
        End Get
    End Property

If I am reading this right is should be that if the key ACHEmail does not exist or is empty then throw the error. I tried removing my ACHEmail key as well as renaming it to ACHEmail1 as well as setting ACHEmail to "".

Any thoughts here. As I said in my previous post, I even ran the code from the book for Chapter 19 and the Final and removed one of the keys (specifically the FromAddress) from the web.config and I didn't get the error there either.

I do remember when I was working through the book that I got the error when testing this but now that I am creating a website I can't get it to work

Thanks for your response!
 
Old February 25th, 2011, 05:11 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You won't get the error on startup, but only when you try to access this property. Is that the case?

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!
 
Old April 14th, 2011, 11:37 AM
Registered User
 
Join Date: Feb 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks

Hi Imar,

Sorry for the delay in my response. I have been very busy with things at work. That was the problem. I wasn't actively requesting the config item. I guess I thought that on startup the AppConfiguration would be checked.

Thanks again!
 
Old April 15th, 2011, 03:05 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,
Quote:
I guess I thought that on startup the AppConfiguration would be checked.
Nope, as you found out, the AppConfiguration code only executes when your own code calls it.

You could call them (ignoring their return values) in the start up event in Global.asax:

Code:
  Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs on application startup
   Dim notUsed As String = AppConfiguration.ACHEmail
  notUsed As String = AppConfiguration.SomeOtherProperty
  End Sub
This calls your properties at the start of the application, resulting in a very early exception in case something is missing.

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!









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.