Click to See Complete Forum and Search --> : Connection string in web.config file


steve1_rm
December 11th, 2006, 10:25 AM
Hello,

VS 2005 SQL 2005

I would like to change the connection string dynamcially during program run-time. But get the code to work. There is no run-time errors and the code compiles ok. However, the code does not let me get the connection string, and I would like to update the connection string at run-time.

This is my code in the web.config file.

<connectionStrings>
<add name="ServiceMasterConnectionString"
connectionString ="data source=dev01; initial catalog = serviceMaster; user id=; pwd="
providerName="System.Data.SqlClient"/>
</connectionStrings>


The code in my website that is used to retrieve the website is as follows:

imports system.configuration.configuration

Dim rootWebConfig As System.Configuration.Configuration
rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Incidents/")
Dim connString As System.Configuration.ConnectionStringSettings
connString = rootWebConfig.ConnectionStrings.ConnectionStrings("ServiceMasterConnectionString")



incidents is the folder that contains the web.config file. That has a virtual path in the IIS.

Can anyone correct if I have done anything wrong in the code above,

Many thanks in advance,

Steve

HairyMonkeyMan
December 11th, 2006, 11:34 AM
Not sure about changing the connectionstring in web.config dynamically (maybe possible by editing the file with code - like you would a text file).

I store my connectionstrings in the web.config, and to access them the only thing I do is:
ConfigurationManager.ConnectionStrings("ServiceManagerConnectionString").ToString