Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connection string in web.config file 1

Status
Not open for further replies.

steve1rm

Programmer
Aug 26, 2006
255
0
0
GB
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.
Code:
<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:
Code:
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
 
Anyone have any ideas about this,

Many thanks,

Steve
 
Just guessing here, but isn't web.config loaded at application start? I wasn't aware that it was possible to edit it on the fly from within the application it governs.

Brian Begy
BugSentry - Automatic error reporting
 
From my experience, BrianB is right. You cant edit it on the fly. What I did to work around this issue was I created a seperate XML file that stores my connection string and other settings. That way you can read and write to it at will from any point on the website.
 
You can't edit. You will have to create as many connection strings as you need, and choose the right one for what you want to do.

Also, This is an ASP.NET question and should be posted here: forum855
 
Your web.config is correct. You will need to add a reference to system.configuration to your project and then simply access your connection string like so:

Private m_ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ServiceMasterConnectionString").ConnectionString


One thing you will want to keep in mind, though, is that any time you edit your web.config file and save it IIS will restart your application. It can cause some session issues if you have clients surfing the application.
 
Thanks for your help,

I have done this in windows application and i have been able to update the connection string at run-time. Sometimes the customer doesn't know until the program is installed which server the program will connect to.

thank,

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top