Link to home
Start Free TrialLog in
Avatar of rajan_thakur78
rajan_thakur78Flag for India

asked on

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

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

SOLUTION
Avatar of Gyanendra Singh
Gyanendra Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial