Avatar of vu3lmg
vu3lmg
Flag 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 ?
SoftwareASP.NET

Avatar of undefined
Last Comment
JohnADavidson

8/22/2022 - Mon
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

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 ?
JohnADavidson

Are you wanting to change the web.config  setting after you have deployed?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
vu3lmg

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

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.