Link to home
Create AccountLog in
Avatar of vu3lmg
vu3lmgFlag for United States of America

asked on

During deployment, Storing Database connection info in web.config

I am developing a web application using ASP.NET 3.5, VS 2008, SQL Server 2005, C#.
I have created a deployment project for installing a web applicaitoin.
How do I get database info from the user (installing application) and store it in the web.config file ?
Avatar of JohnADavidson
JohnADavidson

Add the connection string to your web.config like below and access the data in code like this:

private static string ConnectionString = ConfigurationManager.ConnectionStrings["VariableName"].ToString();

Once the connection string is in place you should be able to access data as you normally would with loops or databinding.
<?xml version="1.0"?>
<configuration>
	<configSections>
		<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
			<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
				<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
				<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
					<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
					<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
					<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
					<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/></sectionGroup>
			</sectionGroup>
		</sectionGroup>
	</configSections>
	<connectionStrings>
		<add name="VariableName" connectionString="user id=userName;data source=serverName; persist security info=True;initial catalog=databaseName;password=passwordForUser"/>
	</connectionStrings>
	<appSettings>

Open in new window

Avatar of vu3lmg

ASKER

JohnADavidson
My question is :
"How to modify the web.config file while the appliation is being installed ? "
What do I do in the Setup-Deploy project to store new DB info in the web.config file ?
Are you wanting to change the web.config  setting after you have deployed?
Avatar of vu3lmg

ASKER

Yes.  During the deployment, user will provide the DB connection info.
ASKER CERTIFIED SOLUTION
Avatar of JohnADavidson
JohnADavidson

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer