Hello, I am launching a VB app that I built with a SQL backend. I am getting the following error message: "Unhandled exception has occurred in your application. .... An attempt to attach an auto-named database for file .....mdf has failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share." Does anybody have any idea what could be causing this and what I would need to do to overcome this Here are the details from the error dialog. Thanks for your assistance.
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file C:\Documents and Settings\Erik\Local Settings\Apps\2.0\Data\34CATLWM.YEH\RYD4M9PO.CE3\newd..tion_7ef9fb2004316736_0001.0000_18ae9c26aea5f895\Data\DueDiligence.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at NewDueDiligence.DueDiligenceDataSetTableAdapters.LoginTableAdapter.Fill(LoginDataTable dataTable)
at NewDueDiligence.Login.Login_Load(Object sender, EventArgs e)
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
NewDueDiligence
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Documents%20and%20Settings/Erik/Local%20Settings/Apps/2.0/M70T031Z.9JE/HGJ1CD06.MDN/newd..tion_7ef9fb2004316736_0001.0000_18ae9c26aea5f895/NewDueDiligence.exe
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Runtime.Remoting
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
System.Data
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Transactions
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

Unhandled Exception When Launching Application - Attach Database Failed
binod m paul
Sam4u2e
I have been working in at least 3 (three) different environments (more actually) - and in each case, the connection string variations were, to say the least - at first, cryptic.
Now, before I go on, I'd like to say that notwithstanding the increased complexities associated with establishing a valid database connection string - the additional params are - in a bummer sense - appreciated..... because, it appears that the flexiblity they provide overrides ones frustration while attempting to figure out -What's going on!
I too have experienced a similar issue and - ok, maybe it's old school, but I needed a much more dynamic method to deal with these connection string issues when on site vs. in dev environment... and decided that i would create a .ini file to contain the connection string params and thus allow for the dynamic 'throwing of switches' that would allow a proper connection and 'depending on the given environment'.
Let's consider at the very least 3 environments
1) VS2005 with SQL Server Express (advanced or not)
2) VS2005 with SQL Server 2005 AND SQL Server Express (advanced ed)
3) VS2005 hitting a remote server having SQL Server Standard or higher
Oh, and what about this one;
We develop (for SQL Express) against H:\VBProjects\CustomerApp and when we execute in the development env, the database is copied to the H:\VBProjects\CustomerApp\bin\debug directory and we think all is fine.
But when we install the app on the clients laptop, the mdf and ldf file need to go to say, c:\data directory
Is it any wonder the connection string doesn't work It shouldn't be... but one is simply - at first - caught off guard. Simple as that, and no matter how experienced you are - or not - it always seems to happen to one degree or another. That's programming.
There are more scenarios, but this is a good example of the number of configurations one might encounter.
For example, you may develop against a local instance and deploy to a remote server instance.
In order to be able to 'debug on the fly' (hey, this stuff is tricky) at the client site, we created an ini file (like I said, old school - but useful) as shown below;
'*********************************************************************************
[DATABASE SETTINGS]
USES_SQL_SERVER_EXPRESS=True
USES_SQL_SERVER_2005=False
SERVER_NAME=(local)
INITIAL_CATALOG=TESTHARNESS
NAME_OF_DATABASE_FILE=TESTHARNESS.MDF
LOCATION_OF_DATABASE_FILE=C:\DATA
SQL_AUTHENTICATION=False
UID=sa
PWD=
Integrated_Security=True
User_Instance=True
'*********************************************************************************
You will notice that the UID & PWD are there, and one would have a number of options with that;
1) Realize your debugging and before the build want to know all params associated with a gold code build prior to deploying to the client... so these come out later, but for now - you're simply want it to work at the client site in order to determine exactly what those params need to be.
They may be one thing on the employees laptop using a SQL Server Express mdf (remoting), but another connection string may be used on a desktop that always hits the server.
Create an INI file and place it in app.path
2) Locate a INI file reader class or write your own. (See resources below)
3) Actually, mdsn had it all the time, the keywords used to zero in were simply wrong at first, but persistance paid off. Anyway, theres a function on msdn which you can search for - but - we modified it to read the variables from the .INI file as appears below;
It's not as elegant as it could be, but - it does the job of allowing one to get connectivity going rather quickly.
Create a module or use an existing one and create a string variable called: Application_Connection_String
'*********************************************************************************
Private Function BuildConnectionString() As Boolean
Dim cIniFiles As New INIFile Dim sServerName As String = INIFile.GetValue("DATABASE SETTINGS", "SERVER_NAME", "Persist.dat") Dim sDBFilePath As String = INIFile.GetValue("DATABASE SETTINGS", "LOCATION_OF_DATABASE_FILE", "Persist.dat") Dim sDBFileName As String = INIFile.GetValue("DATABASE SETTINGS", "NAME_OF_DATABASE_FILE", "Persist.dat") Dim bUsesSQLExpress As Boolean = INIFile.GetValue("DATABASE SETTINGS", "USES_SQL_SERVER_EXPRESS", "Persist.dat") Dim bUsesSQLAuthentication As Boolean = INIFile.GetValue("DATABASE SETTINGS", "SQL_AUTHENTICATION", "Persist.dat") Dim strInitialCatalog As String = INIFile.GetValue("DATABASE SETTINGS", "INITIAL_CATALOG", "Persist.dat") Dim sIntegratedSecurity As String = INIFile.GetValue("DATABASE SETTINGS", "User_Instance", "Persist.dat") Dim sUserID As String = INIFile.GetValue("DATABASE SETTINGS", "UID", "Persist.dat") Dim sUserPwd As String = INIFile.GetValue("DATABASE SETTINGS", "User_Instance", "Persist.dat") Dim sUserInstance As String = INIFile.GetValue("DATABASE SETTINGS", "User_Instance", "Persist.dat") Dim strConnectionString As String = ""BuildConnectionString =
False If bUsesSQLExpress Then Try Dim connectString As String = "" If bUsesSQLExpress ThenconnectString =
"Server=" & sServerName & "\sqlexpress;Integrated Security=" & sIntegratedSecurity & "" ElseconnectString =
"Server=" & sServerName & ";" & "Integrated Security=" & sIntegratedSecurity & ";Connect Timeout=10;User Instance=" & sUserInstance End If Dim builder As New SqlClient.SqlConnectionStringBuilder(connectString)builder.AttachDBFilename = sDBFilePath &
"\" & sDBFileNamestrConnectionString = builder.ConnectionString
Using connection As New SqlClient.SqlConnection(builder.ConnectionString)connection.Open()
BuildConnectionString =
True End Using Catch ex As ExceptionMsgBox(ex.Message, MsgBoxStyle.Critical,
"Error In BuildConnectionString") Dim EL As New ErrorLoggerEL.WriteToErrorLog(ex.Message, EventLogEntryType.Error,
"Test Harness - modAcessLayer") End Try End If If Not bUsesSQLExpress Then Try Dim connectString As String = "" If bUsesSQLAuthentication ThenconnectString =
"Data Source=" & sServerName & ";Initial Catalog=" & strInitialCatalog & ";User ID=" & sUserID & ";password=" & sUserPwd & "" ElseIf Not bUsesSQLAuthentication ThenconnectString =
"Data Source=" & sServerName & ";Initial Catalog=" & strInitialCatalog & ";Integrated Security=True" End If Dim builder As New SqlClient.SqlConnectionStringBuilder(connectString)strConnectionString = builder.ConnectionString
Using connection As New SqlClient.SqlConnection(builder.ConnectionString)connection.Open()
BuildConnectionString =
True End Using Catch ex As ExceptionMsgBox(ex.Message, MsgBoxStyle.Critical,
"Error In BuildConnectionString") End Try End IfApplication_Connection_String = strConnectionString
End Function'*********************************************************************************
My intention was not to create an entire article, rather to point you in a direction.
Your next steps would be to call the function via a button and step through it, ensuring that it populates the global Application_Connection_String variable and, of course connects successfully.
Then, perhaps you'd create a resource (My.Resources.ConnectionString) and populate that resourse with the connection string at application startup.
You might embed this within the app - or one could perhaps leave it in the .INI file - and encrypt it or whatever.
I hope this helps.
Resources;
www.codeproject.com --> search on ini <space bar> files
omar_rapid
Thara Riaz
well can you post the connection string of the database Make sure that the computer does also have SQL Server installed (it should be otherwise you would get a Server does not exist or access denied error instead)
From the exception this is the problem:
An attempt to attach an auto-named database for file C:\Documents and Settings\Erik\Local Settings\Apps\2.0\Data\34CATLWM.YEH\RYD4M9PO.CE3\newd..tion_7ef9fb2004316736_0001.0000_18ae9c26aea5f895\Data\DueDiligence.mdf failed
most likely a problem with the database path. Would be ideal if you can post the connection string of the database