Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-09-29
2
Medium Priority
?
1,738 Views
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.

Raj

namespace CustomWebConfigInstaller
{
    [RunInstaller(true)]
    public class CustomWebConfig:Installer
    {
        const string DATABASE_CONNECTION_KEY = "eAdjudication";
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            //base.Install(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;
 
            config.ConnectionStrings.ConnectionStrings.Clear();
            config.ConnectionStrings.ConnectionStrings.Add(appDatabase);
 
            // Persist web.config settings
            config.Save();
        } 
 
    }
}

Open in new window

0
Comment
Question by:rajan_thakur78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Assisted Solution

by:Gyanendra Singh
Gyanendra Singh earned 400 total points
ID: 22602943
0
 

Accepted Solution

by:
rajan_thakur78 earned 0 total points
ID: 22602955
Thanks..

The above code which i posted worked for me.

Rajan
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

715 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