CodeSnip: Editing Web.Config File for an Application using Visual Studio 2005
page 1 of 3
Published: 04 Apr 2007
Abstract
In this article Amal demonstrates how to edit connection strings and keys in web.config during installation of a web application using Visual Studio 2005.
by Amal ElHosseiny
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 39447/ 66
Article Contents:

Introduction

Our Solution contains web application, class library, windows application and web setup project. Web application contains the web.config file and web setup project is responsible of installing the web application.

Create a new web site using Visual Studio 2005 and add a web.config file to the web application. In the configuration file you will add keys that you need and connection strings.

Listing 1

<appSettings>
  <add key="Northwind" value="TestKey"/>
  <add key="Pub" value="TestKey"/>
</appSettings>

Add to default.aspx (or any page in the web application) SqlDataSource from the Toolbox or by code and select saving connection string to the web.config.

Figure 1

Check in web.config that the connection strings section has been added.

Listing 2

<connectionStrings>
<add name="NorthwindConnectionString"
 connectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"
 providerName="System.Data.SqlClient" />
</connectionStrings>

Add to solution Class Library project and then add installer class to the project. In this class you will add to AfterInstall event following the event handler.

Listing 3

public ApplicationInstaller()
{
  this.AfterInstall += new InstallEventHandler
    (ApplicationInstaller_AfterInstall);
  InitializeComponent();
}
 
void ApplicationInstaller_AfterInstall(object sender, InstallEventArgs e)
{
  EventLog.WriteEntry("Installer2005", "Starting Application");
  if (File.Exists(Context.Parameters["Website"] + @"\WebConfigEditor.exe"))
  {
    Process.Start(Context.Parameters["Website"+ @"\WebConfigEditor.exe");
  }
  else
    EventLog.WriteEntry("Installer2005""File Not Found");
}

Event handler runs windows application and it is supposed to be opened at the end of installation to edit web.config.  As I cannot get the path where the user will host the website, we will ask the user to enter the path of the website. WebConfigEditor.exe will be loaded to the same path as the website.

Add to solution web setup project and set the project output for the class library and web application.

Figure 2

Figure 3

In the web setup project add dialog (TextBox (A)).

Figure 4

Change the properties of the TextBox dialog in the web setup and change EIDT1Property. Please notice that website path must be correct in the installation as it runs the web.config editor after installation.

Figure 5

In the custom actions of the web setup project, add to install the class library .DLL and add CustomActionData to /WebSite=[WEBSITE].

Figure 6

Add windows application project to the solution (the editor tool project) and add it to the setup project in output as Step 5.

In the windows project you will design a form as shown below.

Figure 7

You will add the method to the load the web.config:.

Listing 4

private void LoadConfigFile()
{
  DialogResult dialogResult = openConfigDialog.ShowDialog();
  if (dialogResult == DialogResult.OK)
  {
    keysList = new List < KeyValueConfigurationElement > ();
    connectionsList = new List < ConnectionStringSettings > ();
    string strFilePath = openConfigDialog.FileName;
    txtPath.Text = strFilePath;
    ExeConfigurationFileMap fileConfig = new ExeConfigurationFileMap();
    fileConfig.ExeConfigFilename = strFilePath;
    config = ConfigurationManager.OpenMappedExeConfiguration(fileConfig,
      ConfigurationUserLevel.None);
 
    foreach (KeyValueConfigurationElement key in config.AppSettings.Settings)
    {
      cbKeyName.Items.Add(key.Key);
      keysList.Add(key);
    }
    foreach (ConnectionStringSettings key in
      config.ConnectionStrings.ConnectionStrings)
    {
      cbConnections.Items.Add(key.Name);
      connectionsList.Add(key);
    }
  }

Load the application keys and connection strings info.

Listing 5

private void LoadKeyInfo()
{
  if (keysList != null)
  {
    bFirst = true;
    foreach (KeyValueConfigurationElement key in keysList)
    {
      if (key.Key == cbKeyName.SelectedItem.ToString())
      {
        txtkeyValue.Text = key.Value;
        return ;
      }
    }
  }
}
 
private void LoadConnectionInfo()
{
  if (keysList != null)
  {
    bFirst = true;
    foreach (ConnectionStringSettings key in connectionsList)
    {
      if (key.Name == cbConnections.SelectedItem.ToString())
      {
        txtConnectionValue.Text = key.ConnectionString;
        return ;
      }
    }
  }
}

Save for editing.

Listing 6

private void SaveKey()
{
  if (cbKeyName.SelectedItem != null)
  {
    config.AppSettings.Settings[cbKeyName.SelectedItem.ToString()].Value =
      txtkeyValue.Text;
    keysList[cbKeyName.SelectedIndex].Value = txtkeyValue.Text;
  }
 
}
 
private void SaveConnection()
{
  if (cbConnections.SelectedItem != null)
  {  
 config.ConnectionStrings.ConnectionStrings[cbConnections.SelectedItem.ToString()].ConnectionString = 
txtConnectionValue.Text;
    connectionsList[cbConnections.SelectedIndex].ConnectionString =
      txtConnectionValue.Text;
  }
}
 
private void SaveConfigFile()
{
  SaveKey();
  SaveConnection();
  config.Save();
  MessageBox.Show("Data is saved", "Save", MessageBoxButtons.OK,
    MessageBoxIcon.Information);
 
}

View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-11-24 6:28:33 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search