Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An attempt to attach an auto-named database for file ....database1.mdf failed

I am getting the following error while debugging my visual studio 2010 website:

An attempt to attach an auto-named database for file C:\Users...\Desktop\Dpp2012New\App_Data\dppdatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file C:\Users...\Desktop\Dpp2012New\App_Data\dppdatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Here is my connection string from my web.config:

<connectionStrings>     <add name="ApplicationServices"           connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"           providerName="System.Data.SqlClient"/>     <add name="ConnectionString"           connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dppdatabase.mdf;Integrated Security=SSPI"           providerName="System.Data.SqlClient"/> </connectionStrings> 

And I access it from my website as:

Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString 

The stacktrace shows the error line as:

 Dim conn As New SqlConnection(connectionString)  Dim dr As SqlDataReader  conn.Open() 'This is the error line as per stacktrace 

I have given the needed permissions to the above folder so it can not be the " or specified file cannot be opened" issue. If we look at all the posts related to the same error in this forum, clearly the profoundness of this error can be found out. However, none of the solutions solves my issue. Some of the resources which I have tried are:

  1. http://weblogs.asp.net/scottgu/archive/2005/08/25/423703.aspx
  2. http://blogs.msdn.com/b/webdevelopertips/archive/2010/05/06/tip-106-did-you-know-how-to-create-the-aspnetdb-mdf-file.aspx
  3. http://forums.asp.net/t/1033225.aspx

I eagerly await a solution.

like image 954
rahulserver Avatar asked Sep 24 '12 13:09

rahulserver


1 Answers

I had this problem also and it was a pain. I configured my connection string and managed to solve the problem. In the connection string I replaced the value |DataDirectory|\dbfilename.mdf for AttachDbFilename property, with the path to the file. |DataDirectory| can only be used if the database file is in the App_Data folder inside same project.

So changing the AttachDbFilename property to the direct path of the mdf file, fixed my issue.

AttachDbFilename=C:\MyApp\App\DAL\db.mdf

I hope this works for you.

like image 160
ray abu Avatar answered Sep 21 '22 03:09

ray abu