Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / Visual Basic 12
Tip/Trick

Using Web Configs Key Value pair in classical Asp pages

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
19 Sep 2014CPOL1 min read 14.2K   2  
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:

XML
<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.

VBScript
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

VBScript
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --