0

I'm trying to create a working installer or setup file for an application I created using C# in VS 2010.

I used InstallShield and also the built-in one and they do created the setup files and I installed the app with no problem.

But when I ran the program, this thing pops up:

enter image description here

My database connections:

On the forms:

SqlConnection cn = new SqlConnection(@"SERVER=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;DATABASE=Database;Integrated Security=True;User Instance=True");

App.Config:

connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"

What could be wrong? Should my connection string on the form the same as the one in the app.config file?

  • Visual Studio 2010 Ultimate
  • SQL Server 2008 Express
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ikawa Afue
  • 39
  • 1
  • 6

1 Answers1

1

It looks like you ran the installer on your development box.

If you use AttachDbFilename in the connection string and you omit the option Database from the connection string the sqlclient will try to create a database with the filename as the name of the database. This fails if the database already exists.

You need to make sure that your SqlConnection uses the connection string from the app.config:

 SqlConnection cn = new SqlConnection(
        ConfigurationManager.
        ConnectionStrings["MyDatabaseNameInConfig"].
        ConnectionString); 

In your app.config you'll need:

<connectionStrings>
    <add name="MyDatabaseNameInConfig" 
         connectionString="Database=PRODUCTION;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf; ...."/>
</connectionStrings>

A way to fix this is to add a Database parameter to your connectionstrings for release builds:

connectionString="Database=PRODUCTION;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf; ...."

You can automate that in Visual Studio with XML Document Transform

Another option is to change your setup to make these changes on install but I'm not sure how easy that is.

A final solution might be to have your application accept a special startup argument to update the connectionstrings. The installer can start your application with that special argument at the end of the setup.

Community
  • 1
  • 1
rene
  • 41,474
  • 78
  • 114
  • 152
  • didn't fix it. problem still persist. – Ikawa Afue Aug 28 '15 at 00:14
  • Hi @rene, After completing installation of window form MSI setup, my raw files(Design and coding) are also showing at installation location. I've developed this application using visual studio 2010. Setup created successfully, but after installation that MSI this problem is happening. Please help me. Thanks in advance. – dilipkumar1007 Aug 02 '17 at 07:25
  • @dilipkumar1007 better ask a new question but make sure to read [ask] as I doubt your exact problem can be both described and solved in exchanging comments here. – rene Aug 02 '17 at 09:15