How to add connectionstring and appsettings to a web.config uisng Web Setup.

Posted on 2008-09-29
Last Modified: 2013-11-07
Hi All,

I am building the web setup fo my site for production. Now my requirement is to add a connection string and couple of appsettings to the web config during the setup of the web app. The set up should give me an option of setting the connection string and appsettings.

I am trying the following code, found at some post but without any success.

where targetVDir, targetSite  are coming from the custom action in the setup which has the following setup. connection string is coming from the textbox whihc is custom dialog box actin in the setup.

/connectionstring="[EDITA1]" /targetvdir="[TARGETVDIR]" /targetsite="[TARGETSITE]"

Please advice ASAP.


namespace CustomWebConfigInstaller



    public class CustomWebConfig:Installer


        const string DATABASE_CONNECTION_KEY = "eAdjudication";

        public override void Install(System.Collections.IDictionary stateSaver)



            string targetSite = "W3SVC";//Context.Parameters["targetsite"]; //

            if (targetSite == null)


                throw new Exception("Site name is null");


            string targetVDir = Context.Parameters["targetvdir"]; //"MyApp";//

            if (targetVDir == null)


                throw new Exception("Virtual Dir is null");



            string connectionString = Context.Parameters["connectionstring"];//"Data Source=localhost;Initial Catalog=eAdjudication_Cap;Persist Security Info=True;User ID=devsa;Password=welcome2adc!";

            if (connectionString == null)


                throw new Exception("Conection String is null");


            this.ConfigureDatabase(targetSite, targetVDir, connectionString);


        void ConfigureDatabase(string targetSite, string targetVDir, string connectionString)


            // Retrieve "Friendly Site Name" from IIS for TargetSite

            DirectoryEntry entry = new DirectoryEntry("IIS://LocalHost/" + targetSite);

            string friendlySiteName = entry.Properties["ServerComment"].Value.ToString();

            // Open Application's Web.Config

            Configuration config = WebConfigurationManager.OpenWebConfiguration("/" + targetVDir, friendlySiteName);

            // Add new connection string setting for web.config

            ConnectionStringSettings appDatabase = new ConnectionStringSettings();

            appDatabase.Name = DATABASE_CONNECTION_KEY;

            appDatabase.ConnectionString = connectionString;



            // Persist web.config settings





Open in new window

Question by:rajan_thakur78
LVL 16

Assisted Solution

by:Gyanendra Singh
Gyanendra Singh earned 100 total points
ID: 22602943

Accepted Solution

rajan_thakur78 earned 0 total points
ID: 22602955

The above code which i posted worked for me.


Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now