locked
Modifying connection String in Web config using Install Wizard RRS feed

  • Question

  • User-1455174074 posted

    Hey,

    Im trying to create a Web Deployment Project, the built in setup and deployment is very good in Visual Studio, i need to able to add an additional step in the setup to change the connection string in the Web config file. Ive seen a lot of articles on how to do this and in particular this http://weblogs.asp.net/scottgu/archive/2007/06/15/tip-trick-creating-packaged-asp-net-setup-programs-with-vs-2005.aspx#7162670 I am however stuck on the final part of this tutorial, im using the code Scott provided but have two errors,  heres part of my code where the errors are



    using System;
    using System.Configuration;
    using System.Configuration.Install;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.IO;
    using System.DirectoryServices;

     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();
            }


    my errors are that "WebConfigurationManager"  and "DATABASE_CONNECTION_KEY" don't exist in current context do i need to add additional assembly references and if so which ones? I'm not used to creating web deployment projects but i think this is one of my last steps, would appreciate any help.

    Thanks

    Thursday, February 11, 2010 10:05 AM

Answers

  • User-725220842 posted

    DATABASE_CONNECTION_KEY means you need to add your DATABASE Connection String there.

    Something like this

    Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

    Replace DATABASE_CONNECTION_KEY with a similar string.

    And for WebConfigurationManager try ading System.Web assembly

    using System.Web;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 12, 2010 2:45 AM