Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using Web Configs Key Value pair in classical Asp pages

0.00/5 (No votes)
19 Sep 2014 1  
Using Web Configs Key Value pair in classical Asp pages

Introduction

Web.config acts as a central location for storing the information to be accessed by web pages. This information could be a Connection String stored at a centralized location so that it can be accessed in a data-driven page,Or any Key Value pair that will be needed by the page to use in your Code. If the connection string changes its just a matter of changing it at one place.

Background

While We can use Our Web Configs Key Value pair Directly in ASP.NET pages using the `ConfigurationManagers Class` , but can we use it our classical .asp pages?, Actually these class Supported in: 4.5, 4, 3.5, 3.0, 2.0 Only of ASP.NET But how we can do this in Active Server Pages (ASP) 

Using the code

First thing first

AppSettings of Web.config

appSettings element helps us to store the application settings information like connection strings, file paths, URLs, port numbers, custom key value pairs, etc. The following code snippet shows the example of appSettings Section:

<appsettings>
	<add key="AppKey" value="APLJI12345AFAFAF89999BDFG">
</add></appsettings><appsettings>
    <add key="SuperUserValue" value="APLJI12345AFAFAF89999BDFG">
</add></appsettings>

For More On Web.config.. nice Article Here
http://www.codeproject.com/Articles/301726/Web-config-File-ASP-NET

All we need to load the entire web.config in our object and need to find the tag "appSettings" ,in there we need to loop to each "add" tag to find the correct tag which matches our need ie the key value pair which we need to point.

Dim xmlDocObj,xmlappSettingsObj,xmladdObj ,x

set xmlDocObj=server.CreateObject("Microsoft.XMLDOM")

set xmlappSettingsObj=server.CreateObject("Microsoft.XMLDOM")

set xmladdObj=server.CreateObject("Microsoft.XMLDOM")

xmlDocObj.async="false"

IF(xmlDocObj.load(server.MapPath ("/web.config"))) Then
set xmlappSettingsObj = xmldocObj.GetElementsByTagName("appSettings").Item(0) 
set xmladdObj = xmlappSettingsObj.GetElementsByTagName("add")
for each x in xmladdObj 
'Check for the Atrribute Value
if  x.getAttribute("key") ="SuperUserValue" then
MyVariable=x.getAttribute("value")
END IF

 

Points of Interest

The most annoying thing about the code chunk is

xmlDocObj.load(server.MapPath ("/web.config"))

you have to make sure the server maps to correct location ,or else you will fail to enter the If block, We can avoid the if block but if the loads fails the code will return object required error

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here