Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

ASP.NET: Creating an Application Configuration Class

Most of you are probably aware that the web.config file in an ASP.NET project controls the behavior of your Web site. If you make a change to one of the built-in settings in this file, ASP.NET automatically detects those changes and applies them immediately. Wouldn't it be nice if you could have your own settings in this file applied immediately as well? In this article you will learn how to do just that. You will also learn the difference between the Application object and creating your own Configuration class.


advertisement
ost programmers have used the Application object to store system-wide values. While this is a convenient storage facility, it does have a few disadvantages:

  • There is no IntelliSense when retrieving or setting values into the Application object. If you are setting a value and you do not remember what you called the key of the stored value, you will need to look up the key manually by searching through your program. If you do not spell the key correctly, you will inadvertently create a new variable in the Application object.
  • If you read values from the web.config file, you will need to refresh the values within the Application object yourself. There is no automatic detection of changes.
Retrieving Application Variables Using web.config
The most common method of storing application-wide settings is to place these values into the <appSettings> section of your web.config file as shown in the following code fragment:

<appSettings> <add key="ConnectString" value="Server=(local);Database=Northwind;uid=sa" /> <add key="SiteName" value="ConfigSample" /> </appSettings>

You can then read these values during the Application_Start event by using the following code:

Sub Application_Start(ByVal sender As Object, _ ByVal e As EventArgs) Application("ConnectString") = System.Configuration. _ ConfigurationSettings.AppSettings("ConnectString") Application("SiteName") = System.Configuration. _ ConfigurationSettings.AppSettings("SiteName") End Sub

To retrieve the values, you would write code like the following:



Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load lblAppSiteName.Text = Application("SiteName").ToString lblAppConnectString.Text = _ Application("ConnectString").ToString End Sub

The values SiteName and ConnectString that you pass to the Application object are not verified until runtime. This means that if you misspell "SiteName" as "SileName," you will not notice this error until runtime. This type of error can be very hard to track down. In addition, you must apply the ToString method to convert the object within the Application object to a string. If you store any other data type within the Application object, you need to call the CType() or use the Convert class and one of its appropriate methods. This means more typing and time to perform the conversion. (See Sidebar 1: Do I Have to Use web.config?)


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap