Create an installer for website with WIX, part 4

This part will continue on the previous parts (part 1, part 2, part 3) where we set up harvesting files, creating our own UI and adding a website in IIS.

In this part we’ll create our database after we ask the connection settings from the end user and update our web.config file with the correct credentials and connection settings.

In part 2 we’ll already provided a dialog where the user can enter the server’s IP address, database name, user and password. We’ll use these credentials further in this part.

Create the database

First we’ll have to create the database scripts. I’ve created 2 scripts, one to create all tables, stored procedures, primary and foreign keys, … and a second one to prefill some default tables (like a default admin user, …). I’m not going into the creation of the scripts, I suppose you’ll now how to do that and otherwise you’ll be able to Google or Bing for it.

Right click your setup project in your solution explorer and add a new folder ‘Database’. Add the 2 scripts to that folder (in this demo CreateTables.sql and FillTables.sql)

image

Add a new .wxs file in the project and name it ‘Database.wxs’ (right click project, add, select installer file, click add). Just as with the IIS configuration in part 3 we’ll have to tell WIX compiler that we’re goiing to use the WiX Util and WiX SQL library. We’ll have to add the namespaces at the top of the file as shown below.

The first items we’ll have to add are the 2 SQL files we’ve created. Add 2 “Binary” elements that point to our files.

Next step will be to use the Util library to create an user element that will connect to the SQL server. The name and password parameter we’ll set to the variables we’ve used in our custom dialog.

As reminder the dialog we’ve created in part 2

Next element to add is a “DirectoryRef” element, this will tell WiX to execute the content of that element when installing the application. In our project we’ll set the ID to the ‘INSTALLFOLDER’.

In this element we’re going to add the ‘Component’ element that contains the ‘SQLDatabase’ element. In the ‘SQLDatabase’ element we can specify the SQL server name or IP address and database name we asked our end user in our custom dialog. Set those items to the variables we used in our dialog.

Set the ‘CreateOnInstall’ parameter to yes to have WiX install the database, choose yes by parameter ‘DropOnINstall’ if you want the database be dropped when your website is uninstalled by Add/remove programs. The ‘User’ parameter we set to the ‘User’ element we’ve created before. Last but not least is the ‘ContinueOnError’ parameter that I’ve set to no to avoid the installation to continue when a database error occurs.

The above is actually enough to create our database. Off course we want to create also our tables and pre fill them. To do that we’ve have to add 2 ‘SQlScript’ elements to the ‘SQLDatabase’ element that will point to ‘Binary’ elements we’ve created before (that on their part point to the scripts we’ve added).

Update the MS Build file

Because we created an extra file to include in the build process we must be sure that the file is added in our build.

We have to update the item groups that contain the list of files to include as shown above.

Now open up the Developer Command Prompt for Visual Studio 2012 again and change the prompt to the setup project folder and type the next statement:

msbuild /t: Build;PublishWebsite;Harvest;WIX;DeleteTmpFiles setup.build

and hit enter.

You’ll see the build process will return an error. We added a reference to the WiX Util and Database library in our ‘.wsx’ file but we didn’t tell the candle.exe and light.exe tools to take that into account. Add in both command the –ext WixUtilExtension and –ext WixSqlExtension and run the script again.

Now you should have a successful build. Run the installer and enter the connection credentials for an existing SQL (Express) server and you’ll see your database will be created.

If the installer receives an error during the database installation you’ll get an error message when installing and all already installed files will be reverted.

Next parts

This concludes the installation of the SQL database. Next part we’ll handle updating our web.config file to have the correct SQL connection string.

  • Install the .NET 4.5 framework if that isn’t installed already
  • Install the MVC 4 framework if that isn’t installed already.
  • Create a folder and copy all needed files to run the application (done)
  • Create a new database on an existing SQL server and prefill the database with the correct tables and values. (the connection details and database name should be entered by the end user running the installer) (done)
  • Create a new website in IIS 7.5 (create website and application pool running under .NET 4.5) (done)
  • Alter the config file so the correct connection settings are used (entered by the end user)

Complete source code

Can be found on Github.

Other posts in this series:

23 thoughts on “Create an installer for website with WIX, part 4

  1. Angel Blandon

    Great tutorial,

    Thank you very much, this was very helpfull for me, i will wait for next tutorial (Alter the config file so the correct connection settings are used (entered by the end user)), i hope it comes very soon.

    Regards.

    Reply
    1. Bart De Meyer Post author

      Hey Angel,
      Thanx for the comment.

      I’ll try to finish the series as soon as possible but time is a valuable thing at the moment.

      Reply
  2. Angel Blandon

    Hi, thanks again, i was trying to create a database using this tutorial, installer is running fine but no database is created.

    I hope you can help me.

    I am using Wix 3.7

    Regards.

    Reply
    1. Bart De Meyer Post author

      Angel,
      Do you get any error message during installation?

      What you can do is run the installer with logging. open up a command prompt and navigate to the folder where your builder installer resides. Enter in the command prompt: msiexec /i [YOURINSTALLER.MSI] /i*v [YOURLOG.TXT]

      Replace [YOURINSTALLER.MSI] by your installer name and [YOURLOG.TXT] by ahow you want to name your log file.
      Check the log file created if you’ll see any errors when creating the database.

      Hope you’ll find some more info that will direct you to a solution.

      Regards,

      Bart

      Reply
  3. Angel Blandon

    Are you creating database in your script? I am just creating tables, May you please provide your scripts.

    Regards.

    Reply
    1. Bart De Meyer Post author

      Hey Angel,

      The scripts only contain the creation of tables,primary keys, foreign keys, indexes, … and to fill default values into these tables. The actual creation of the database is done by WiX.

      Reply
  4. Angel Blandon

    Hi, i am trying to implement this solution to a real project, i am using third party libraries and this files are not being included in my installation.

    I hope you can help me.

    Regards.

    Reply
  5. Delson

    Hi, this tutorial was very usefull for me. But when I run DemoWebsite_Setup.msi I noticed that it didn’t connect and created the tables. In my log file there aren’t anything related to scripts “CreateTables.sql” and “FillTables.sql”. Maybe is there more something that I need do, isn’t it? Isn’t necessary to update the product.wxs?

    This is part of Database.wxs———————————————————————-

    —————————————————————————————————————–

    This is part of setup.build—————————————————————————-

    ——————————————————————————————————————

    Reply
  6. Bart De Meyer Post author

    I’ll try to check your comments as soon as possible. These days I’m stacked at work what prevents me from diving into the problems.

    Will update as soon as a see a small gap in the schedule.

    Reply
  7. Angel Blandon

    Hi, i find the solution, the problem is that database component is not added in product component group, try adding as you did with iis component.

    Reply
  8. Abdennour

    Hi, thanks again for this tuto , thank’s also for Angel Blandon , the solution who propose is was very helpfull for me

    Reply
      1. Spirius

        well… apparently this blog doesn’t support xml in posts=)
        changes to previous message. i replaced ‘less’ with 111 and ‘greater’ with 222:

        In Database.wxs add the following after 111/DirectoryRef222:
        111ComponentGroup Id=”SqlComponentConfiguration”222
        111ComponentRef Id=”SqlComponent” /222
        111/ComponentGroup222

        And in Product.wsx change this:
        111Feature Id=”ProductFeature” Title=”DemoWebsite.Setup” Level=”1″222
        111ComponentGroupRef Id=”MyWebWebComponents” /222
        111ComponentGroupRef Id=”DemoWebsiteIssConfiguration” /222
        111/Feature222

        To this:
        111Feature Id=”ProductFeature” Title=”DemoWebsite.Setup” Level=”1″222
        111ComponentGroupRef Id=”MyWebWebComponents” /222
        111ComponentGroupRef Id=”DemoWebsiteIssConfiguration” /222
        111ComponentGroupRef Id=”SqlComponentConfiguration” /222
        111/Feature222

        Reply
  9. DRaj

    Hii.. THanks for this series. I am really interested in updating Web.config connection string values using the values that are provided in customUI. Can you please share how to do the same.

    Reply
  10. Pingback: Create an installer for website with WIX, part 3 | Bart De Meyer – Blog

  11. Pingback: Create an installer for website with WIX, part 2 | Bart De Meyer – Blog

  12. Pingback: Create an installer for website with WIX, part 1 | Bart De Meyer – Blog

  13. Vincent

    Hi Bart,

    thanks for the good article. The code from github gives an error in the setup project. The componentgroup MyWebWebComponents can’t found.

    regards,
    Vincent

    Reply
  14. Mahesh P M

    Thanks a lot for this article! It helped me get started with WiX website installation in IIS.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *