Click here to Skip to main content
13,349,539 members (52,371 online)
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 24 Aug 2012

Adding connection string during installation

, 11 Jul 2013
Rate this:
Please Sign up or sign in to vote.
Setup file creation and adding a connection string custom control.



In this article we are going to see how we can create a setup (MSI) file for a Windows application. Along with setup creation we are adding a custom control to the MSI file which facilitates framing the connection string during installation. Connection string details (instance name, database name) will be taken from the user during the installation process. For adding a custom control to the setup we need to design a page which facilitates taking all the inputs that are necessary to frame the connection string.

How it is useful

  • This custom control enables the end user to make his own connection string during installation process.
  • It eliminates the hard coding of connection string in the App.config file which makes an application more user friendly.


We can divide this into two parts:

  • Creation of setup file
  • Adding custom control to setup

Creation of setup file

  • Step 1: Open or Create an application, add a new project by right click > Solution Explorer >Add >New Project > Other Project types>Setup and Deployment >Visual Studio Installer >Setup Project (Windows app) for Web App (Web setup Project).
  • Step 2: Add Web.config or App.config and the installer class (right click >project>Add new Item >Installer class) to existing project.
  • Step 3: Go to properties of the Setup project and change Author name, Manufacturer, Product name, Title etc.
  • Step 4: Right click on Setup project >Add >Project Output > Select primary out put and Content files too.
  • Step 5: Select the referring DLLs of the application right click> Properties> Set copy to local to True.
  • [__em__]//Installer.cs 
    string dataSource = "Data Source =" + Context.Parameters["DataSource"];
    string initialcatalog = "Initial Catalog=" + Context.Parameters["InitialCatalog"];
    dataSource = dataSource + ";" + initialcatalog;
    dataSource = dataSource + ";Integrated Security=SSPI;";
    MessageBox.Show("instance=" + dataSource);
    ExeConfigurationFileMap map = new ExeConfigurationFileMap();
    MessageBox.Show(Assembly.GetExecutingAssembly().Location + ".config");
    //Getting the path location 
    string configFile = string.Concat(Assembly.GetExecutingAssembly().Location, ".config");
    map.ExeConfigFilename = configFile;
    System.Configuration.Configuration config = System.Configuration.ConfigurationManager.
    OpenMappedExeConfiguration(map, System.Configuration.ConfigurationUserLevel.None);
    string connectionsection = config.ConnectionStrings.ConnectionStrings
    ConnectionStringSettings connectionstring = null;
    if (connectionsection != null)
        MessageBox.Show("removing existing Connection String");
    connectionstring = new ConnectionStringSettings("SqlConnectionString", dataSource);
    config.Save(ConfigurationSaveMode.Modified, true);

Adding custom control

Designing the custom control

We need to design our custom control according to our requirements. In this scenario we require text boxes, we will have inbuilt text boxes, we need to add those to our custom install wizard. We can make four text boxes which can be enabled and disabled with the property "Edit Visible". We can also adjust the display of our designed control with respect to the installation process.

UI design for custom control

Alignment order of installation process

  • Step 1: Right click on Setup project > View >User Interface. It will show the installation process steps. Right click on Start node>Add Dialog>Select Text boxes>Change properties accordingly. In properties it shows four text boxes. If we set property (Edit Visible) to true it will be visible.
  • Step 2: Right click on Setup project > View >Custom Actions> Click on Install node>Add Custom Action>Click on to Application Folder>Choose Primary output file. Perform the same action for Commit, Rollback, Uninstall nodes.
  • Step 3: Go to properties of the primary output file of Install node, Custom Action Data, pass the input parameters of the connection string as follows.
  • /DataSource="[EDITA1]" /InitialCatalog="[EDITA2]" /UserID="[EDITA3]" /Password="[EDITA4]"

    For this data should pass as <Key> and <value>. Text box label name should match the key and property name match the value. We should provide custom action data to the primary output file of install only.

  • Step 4: The setup file will be created once we complete the Build and the setup file is created in the debug folder. Install it and check the config file in the installed location (C:/programming files /path name .....) and you will get the new connection string with user provided details.

Next article: Creation of Custom installer for Web application.

Points to remember

  • Custom Action Data will take all input parameters as a single string.
  • We need to provide input in (Custom Action Data) primary output file of install node only. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Purushotham Agaraharam
Software Developer (Senior) @ CSC
India India
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Organisation

8 members

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Hernán Hegykozi2-Nov-12 12:48
memberHernán Hegykozi2-Nov-12 12:48 
GeneralMy vote of 5 Pin
D-Kishore3-Sep-12 22:55
memberD-Kishore3-Sep-12 22:55 
NewsThank you Pin
tas29-Aug-12 23:09
membertas29-Aug-12 23:09 
GeneralRe: Thank you Pin
Purushotham Agaraharam30-Aug-12 4:39
groupPurushotham Agaraharam30-Aug-12 4:39 
AnswerThanks Pin
J. Wijaya28-Aug-12 19:21
memberJ. Wijaya28-Aug-12 19:21 
GeneralRe: Thanks Pin
Purushotham Agaraharam30-Aug-12 4:43
groupPurushotham Agaraharam30-Aug-12 4:43 
QuestionVery Well Written. My Vote 5 Pin
Grasshopper.iics26-Aug-12 4:35
groupGrasshopper.iics26-Aug-12 4:35 
GeneralMy vote of 5 Pin
Christian Amado24-Aug-12 17:44
memberChristian Amado24-Aug-12 17:44 
Very useful!
GeneralRe: My vote of 5 Pin
seyinow28-Dec-12 4:18
memberseyinow28-Dec-12 4:18 
GeneralRe: My vote of 5 Pin
Purushotham Agaraharam28-Dec-12 4:37
groupPurushotham Agaraharam28-Dec-12 4:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 11 Jul 2013
Article Copyright 2012 by Purushotham Agaraharam
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid