Programming in almost language

This is the site where you may share your knowledge and experience to eachother..

  • Categories

  • LinkedIn

  • Tweet Me

  • My footsteps

Read Configuration Settings of Web.config using Javascript

Posted by Praveen Kumar on April 5, 2008

Web.config is a XML file containing the configuration settings of our application. Amongst its other uses, the file is extremely handy when it comes to changing configuration settings of an application, even when the application is live. You can define your own specific application settings, such as a database connection string using the <appSettings> tag and read and write values to the file programmatically. In this article, we see how to read the configuration settings in the web.config using ‘JavaScript’.

ASP.NET provides the Web Site Administration Tool to view and manage the configuration settings for your ASP.NET website. These configuration settings are stored in an xml file called web.config.

“web.config is an application configuration file used to define configuration settings such as connecting strings, authentication, authorization etc., for an ASP.NET application

In this short article, I will see how we can retrieve the values from the  <appSettings> and <connectionStrings> section in the web.config using JavaScript. So let us get started.

Step 1: Create a new ASP.NET website. Add a button control to the Default.aspx.

Step 2: Right click the project > Add New Item > Web Configuration File
Add the following sample entries in the file between the <configuration> tag as shown below:

<configuration>

<appSettings>

<addkey=var1value=SomeValue/>

</appSettings>

<connectionStrings>

<addname=MyConnStringconnectionString=Data Source=(local);Initial Catalog=Northwind;Integrated Security=True;/>

</connectionStrings>

<configuration>
Step 3: We will now read these entries using JavaScript. To do so, add the following script in the <head> tag of your Default.aspx page as shown below:
<head runat=”server”>
<title>Read Config Entries Using Javascript</title>
<script type=”text/javascript”>
function ReadConfigSettings()
{
var conn = ‘<%=ConfigurationManager.ConnectionStrings[“MyConnString”].ConnectionString %>’
alert(conn);
var v1 = ‘<%=ConfigurationManager.AppSettings[“var1”].ToString() %>’
alert(v1);
}
</script>
</head>
Step 4: Call this function on a button click and display the values of the configuration settings
<asp:Button ID=”Button1″ runat=”server” Text=”Button”OnClientClick=”ReadConfigSettings()” /></div>
That’s it. Run the application and click the button. The values of the configuration settings in the web.config will be displayed in the alert boxes.

I hope you liked this short article and I thank you for viewing it. If you liked the article,

then please comment it or give me suggestion to make more effective.

Advertisements

24 Responses to “Read Configuration Settings of Web.config using Javascript”

  1. Satya said

    praveen,
    I have tried the following code in my js file. It is not working. I am using vs.net 2005.

    var v1 = ”
    alert(v1);

  2. Hi Satya, What value you are getting in v1??
    Could you send me your code??

  3. Vinayak said

    i am using sme code given here in vs2005 it is giving me error
    identifier expected

  4. Vinayak said

    Code for ur ref

    Untitled Page

    function ReadConfigSettings()
    {

    var v1 = ‘’
    alert(v1);
    }

  5. I think you are doing mistake in closing mean ‘ or ” you just check it out. could you please send me the line which encountered to error.

  6. cosa_Netlight said

    A js-file is not compiled by the server. Thus, this code will not work for you. Place your javascript inline in an aspx-file or use another method (reading XML files with javascript).

  7. shyam said

    Very good And Excellent . you are doing great job. thank you

  8. jj said

    its no working for me either. its returning the string to the right of the equal sign as it is

  9. Hello jj,
    Do not copy paste the code cause it paste the wild character.
    Just write the code manually.
    Thanks

  10. Todd said

    This does not work

  11. Hi Todd, you should explain some more to get us know what is not working??
    Thanks
    Praveen

  12. Thilina said

    It didnt works for so insted of “ConfigurationManager.ConnectionStrings” I change as bellow

    it works… Thanks

    sorry this was missing

    “var conn = ”;”
    alert(conn);

  13. Thilina said

    This is what I am trying to put. (Every time it ignores, so I convert as string, so syntax not correct)

    “System.Configuration.ConfigurationManager.AppSettings[“sessionDir”].ToString()”

  14. mfchiu said

    var X = ”
    alert(X);

  15. Kaushal said

    I’ve tried your code. It’s working fine in aspx page
    But If i’m putting same code in javascript file & then including that file in to aspx page & tried to set page title. Not working any idea?

  16. Kaushal said

    //MyFunction
    function SetPageTitle() {
    var pageTitle = document.title;
    var environment = ”
    document.title = environment + ” :: ” + pageTitle;
    }

  17. asp.net, c#,javascript…

    […]Read Configuration Settings of Web.config using Javascript « Programming in almost language[…]…

  18. I also tried the same thing by putting the code in .js file it is not working,but it works in aspx page.
    it might be a draw back.

  19. everything about java…

    […]Read Configuration Settings of Web.config using Javascript « Programming in almost language[…]…

  20. That’s awesome man, works perfectly with me, thanks for your efforts.

  21. @.Net Developer thanks 🙂

  22. .Net dev said

    I put the same code as javascript inline statement. It doesn’t work in my aspx. Hit compilation error. Identifier expected.

    function readConfig()
    {
    var secondsCofiguration = ”;
    alert(secondsConfiguration);

    }

    Test

  23. Ramendra Kumar said

    Identifier Expected Dude, any body can help if he did it in JavaScript
    var a = ”;
    i also tried
    var a = “”;

  24. I highly recommended to not to simply copy paste the code. It has some quotation or wild character problem.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: