1

How do i add an App_Data in my window application. Actaully i am facing an issue like

An attempt to attach an auto-named database for file c:\inetpub\wwwroot\Demo\App_Data\aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

For this error i got an article saying that keep .mdf file in App_Data folder if we give path as AttachDbFilename = | DataDirectory | / Database.mdf; (as of now i am giving same path) in connection string. so i need an App_Data folder in window application if not how to solve this error.

my connection string as fallows:

<add name="BCT" 
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SQL2005_652242_bct_data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
providerName="System.Data.SqlClient" />
Krishnraj Rana
  • 6,516
  • 2
  • 29
  • 36
user2801432
  • 23
  • 1
  • 6
  • 13

2 Answers2

1

If you don't want to change your connection string. i.e If you want to keep DataDirectory in your connection string then copy your mdf file in bin\Debug folder. This will work for sure. Try it.

Paresh J
  • 2,401
  • 3
  • 24
  • 31
  • When i build the project automatically .mdf is placed in bin\debug folder. @paresh Jadhav – user2801432 Oct 30 '14 at 05:17
  • You will not have problem connecting database and no need to change the connection string. It must be working?? – Paresh J Oct 30 '14 at 05:33
  • I had been tried with your answere but it doesn't worked out for me. I am getting same error as before,no change in an error @paresh Jadhav – user2801432 Oct 30 '14 at 05:45
  • Try AppDomain.SetData to set your DataDirectory path. It must work. Check this URL : http://stackoverflow.com/questions/16632416/c-sharp-windows-form-application-with-database-not-running-on-clients-system – Paresh J Oct 30 '14 at 06:32
0

Just create a folder with the name App_Data. This folder is a normal folder that VS use to place data files.

Richard Dias
  • 220
  • 3
  • 10
  • I have tried your answer doesn't worked out for me is possible to get App_Data to be placed in window application. I Need in-built app_data but not creating manually for window application is it possible ? @Richard Dias – user2801432 Oct 30 '14 at 05:54
  • You already have the file `SQL2005_652242_bct_data.mdf`? If you already have the `.mdf` file, you must put him into `App_Data` folder and go to properties of file, pressing `Alt+Enter`, and set the property `copy local = true`. this will make your file be added to output folder inside the `App_Data` folder. – Richard Dias Oct 30 '14 at 18:00
  • After pressing Alt+enter where should i set the property copylocal=true. ?? is it in app.config ?? @Richard Dias – user2801432 Oct 31 '14 at 07:50
  • No, not in `app.config`. With VS opened, select the file `SQL2005_652242_bct_data.mdf` and press `Alt+Enter`, so a tab named properties will appear, right? One of the properties of the file is `Copy local` and default value is `false`, you have to set to `true`. This will copy your `.mdf` file to output folder. – Richard Dias Oct 31 '14 at 19:46