Click here to Skip to main content
Click here to Skip to main content

Adding connection string during installation

By , 1 Nov 2012
 

Contents

Introduction

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

How It Is Useful 

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

Methodology

  • 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 installer class (right Click >project>Add new Item >Installer class) to existing project.
  • Step 3: Go to properties of 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
  •  //Installer.cs  
    protected override void OnAfterInstall(IDictionary savedState)
    {
        base.OnAfterInstall(savedState);
        string dataSource = Context.Parameters["DataSource"];
        dataSource = dataSource.Replace('/', ';');
        MessageBox.Show("instance=" + dataSource);
        dataSource = dataSource.Replace("$", @"\");
        dataSource = dataSource.Replace("UserID", @"User ID");
       // During Connection String Formation an Extra forward slash is found 
         dataSource=dataSource.Replace(@"\\","\");
        dataSource = "Data source = " + dataSource.Replace("InitialCatalog",
         @"Initial Catalog");
         string pwd = Context.Parameters["Password"];
        ExeConfigurationFileMap map = new ExeConfigurationFileMap();
        MessageBox.Show(Assembly.GetExecutingAssembly().Location + ".config");
        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
        ["SqlConnectionString"].ConnectionString;
        //Removing Existing Connection string if available Adding new one
    
         ConnectionStringSettings connectionstring = null;
        if (connectionsection != null)
        {
            config.ConnectionStrings.ConnectionStrings.Remove("SqlConnectionString");
        }
        connectionstring = new ConnectionStringSettings("SqlConnectionString", dataSource);
        config.ConnectionStrings.ConnectionStrings.Add(connectionstring);
        config.Save(ConfigurationSaveMode.Modified,true);
        ConfigurationManager.RefreshSection("connectionStrings");
        SqlConnection con = new SqlConnection(connectionstring.ConnectionString);

Adding Custom Control

Designing Custom Control

We need to design our custom control according to our requirement. In this scenario we required 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 disable with this property "Edit Visible". We can also adjust the display of our designed control with respective to installation process.

UI design for Custom Control

Alignment order of Installation Process

  • Step 1: Right Click on Setup project > View >User Interface> It will shows the installation process steps >Right click on Start Node>Add Diloug>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 Primary output file of Install node, Custom Action data pass the input parameters of 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 to the key and property name matched to Value. We should provide custom action data to primary output file of install only.

  • Step 4: Setup file will create once we Complete the Build and setup file was created in debug folder, install it and check the config file in installed location (C:/programming files /path name .....) you will get new connection string with user provided details.

Points to Remember

  • Custom Action Data Will take all input parameters as an single string.
  • We need to provide input in (Custom Action Data) primary output file of install node only.
  • Another way to pass parameters in custom Action Data is as follows:
/DataSource="[EDITA1]" /InitialCatalog="[EDITA2]" /UserID="[EDITA3]" /Password="[EDITA4]"

If we pass the custom action data parameter in this method we can get each one separately.

License

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 Congruent Info-Tech
India India
Member
Organisation
2 members

No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5 PinmemberHernán Hegykozi2 Nov '12 - 11:48 
GeneralMy vote of 5 PinmemberD-Kishore3 Sep '12 - 21:55 
NewsThank you Pinmembertas29 Aug '12 - 22:09 
AnswerThanks PinmemberJ. Wijaya28 Aug '12 - 18:21 
QuestionVery Well Written. My Vote 5 PingroupGrasshopper.iics26 Aug '12 - 3:35 
GeneralMy vote of 5 PinmemberChristian Amado24 Aug '12 - 16:44 

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

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