Click here to Skip to main content
15,844,607 members
Articles / Programming Languages / C#

Adding connection string during installation

Rate me:
Please Sign up or sign in to vote.
4.73/5 (15 votes)
11 Jul 2013CPOL3 min read 2.1M   42   41
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.
  • C#
    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

Image 1

Alignment order of installation process

Image 2

  • 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.
  • Image 3

  • 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.

    Image 4

  • 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)

Written By
Software Developer (Senior) @ CSC
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Organisation

8 members

Comments and Discussions

QuestionInstaller.cs Pin
Iheb OMRI5-Feb-20 22:44
Iheb OMRI5-Feb-20 22:44 
QuestionShare the files Pin
Member 1122741713-Oct-17 12:03
Member 1122741713-Oct-17 12:03 
QuestionError 1001--> Exception has been thrown by the target of an invocation. Object reference not set to an instance of an object. Pin
Member 1055972710-Jan-16 23:14
Member 1055972710-Jan-16 23:14 
AnswerRe: Error 1001--> Exception has been thrown by the target of an invocation. Object reference not set to an instance of an object. Pin
Purushotham Agaraharam28-Jan-16 21:45
Purushotham Agaraharam28-Jan-16 21:45 
QuestionError during Commit phase. Help! Pin
CringTee18-Nov-15 1:45
CringTee18-Nov-15 1:45 
QuestionNeeded help! Pin
CringTee8-Nov-15 7:04
CringTee8-Nov-15 7:04 
QuestionHelp Pin
aathimuthu27-Aug-15 22:04
aathimuthu27-Aug-15 22:04 
QuestionOuch ! it's incredibly complicated ! Pin
austinlolo6-Jul-15 6:32
austinlolo6-Jul-15 6:32 
QuestionHelp! Pin
ezecape14-Apr-15 8:28
ezecape14-Apr-15 8:28 
QuestionPlease send Intaller.cs Pin
rojasalf18-Mar-15 5:50
rojasalf18-Mar-15 5:50 
GeneralRe: Please send Intaller.cs Pin
PIEBALDconsult18-Mar-15 6:02
mvePIEBALDconsult18-Mar-15 6:02 
Generalchange connection string Pin
ElhamLiri21-Feb-15 7:31
ElhamLiri21-Feb-15 7:31 
GeneralRe: change connection string Pin
Purushotham Agaraharam24-Feb-15 20:40
Purushotham Agaraharam24-Feb-15 20:40 
Questionhelp Pin
Member 1116157617-Oct-14 7:27
Member 1116157617-Oct-14 7:27 
AnswerRe: help Pin
Purushotham Agaraharam20-Oct-14 0:39
Purushotham Agaraharam20-Oct-14 0:39 
Questionsend installer.cs file Pin
Member 1114733512-Oct-14 22:44
Member 1114733512-Oct-14 22:44 
AnswerRe: send installer.cs file Pin
Purushotham Agaraharam16-Oct-14 20:37
Purushotham Agaraharam16-Oct-14 20:37 
QuestionError 1001 Pin
Arsalan Khatri25-Sep-14 2:30
Arsalan Khatri25-Sep-14 2:30 
QuestionApp.Config Pin
Purva Halburge12-Aug-14 3:55
Purva Halburge12-Aug-14 3:55 
GeneralSend me the Installer.cs Pin
Mohamed ansari28-Feb-14 20:14
professionalMohamed ansari28-Feb-14 20:14 
BugI can Under Stand the step Pin
Mohamed ansari28-Feb-14 7:27
professionalMohamed ansari28-Feb-14 7:27 
QuestionTextbox field is ok but can we add any droupdown in Add dialog Pin
nikhilsreeni5-Nov-13 1:08
nikhilsreeni5-Nov-13 1:08 
AnswerRe: Textbox field is ok but can we add any droupdown in Add dialog Pin
Purushotham Agaraharam11-Nov-13 21:12
Purushotham Agaraharam11-Nov-13 21:12 
QuestionI want to impletement this feature in my application Pin
sahabiswarup15-Jul-13 1:38
sahabiswarup15-Jul-13 1:38 
QuestionNot supported in .Net 4.5 Pin
FatCatProgrammer9-Jul-13 7:18
FatCatProgrammer9-Jul-13 7:18 

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.