I've read through all the posts in this fourm that are related to the problem I'm getting with this error message on my main form, but none of the fixes seen to solve the problem. So I hope someone can get me pointed in the right direction.
I'm running visual studio 2005 professional, and SQL Server 2005 Express Edition and this is a desktop application. I added the db file to the solution with the Add New Data Source wizard, I can create the datasets and preview the data correctly.
An attempt to attach an auto-named database for file C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\dbInventory.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
When I run the program the exception is throw in the Settings.Designer.vb code, Public ReadOnly Property for the connection string. The InnerException is this...
{"An error occurred loading a configuration file: Could not find a part of the path 'C:\Documents and Settings\<UserName>\<ApplicationName>.vshos_StrongName_1sdf1e34hkn1hqmkn2bgjjwstusfj2sg\1.0.0.0\user.config'. (C:\Documents and Settings\<UserName>\Application Data\...\<ApplicationName>.vshos_StrongName_1sdf1e34hkn1hqmkn2bgjjwstusfj2sg\1.0.0.0\user.config)"}
This folder doesn't exist anywhere on the system, any ideas....
TIA,
SQL Express associates specific database names with the paths where those databases reside. At some point, you've created a database with the same name as the one you're using now, but at a different path. When your code attempts to attach the database with the specific autoname, it finds that name already existing, but at a different path than the one you're specifying, so it fails.
How all this works is wrapped up in the bowles of ClickOnce deployment and SQL User Instances.
You need to clear out the pointer to your database name at the old path, which is easiest to accomplish using SSEUtil. Once you download this utility, you'll be able to easily connect to the User Instance that VS created for you and get a list of all the databases it knows about. (sseutil -L) Find the one that is the same file name as your file but in the wrong location and detach it.(sseutil -d name=c:\somepath\dbinventory.mdf) The name of the database will likely be the path to the file, this is the result of the auto naming that the error is talking about. You can learn more about auto naming and User Instances from the link I've given above.
Mike
|||Thanks for your reply,
I have run this utility and only one instance of the database is attached. However, I have finally been able to reproduce the problem. I created two identical projects with a dataview bound to one of the tables in the dataset. In one of the test app's, I checked the "Enable ClickOnce Security Settings" in the Security Tab of MyProject settings and ran the app. This caused the error that I'm getting in my real project.....
I went back and unchecked the the Enable ClickOnce Security Setting, ran the project again and it worked fine! So I checked the real project settings and found that indeed the "Enable ClickOnce Security Settings" was enabled, however,after unchecking it and verfiying the other project settings I still am getting the same error!
I ran the sseutil again to see if something was going on, but only one instance of the database is running.
So I went back to the test app and messed around with the settings to see if I could get it to fail, nothing I did seem to create a condition that I couldn't change and get it back working correctly............until I closed the application and reopened it..........It failed as soon as I ran it and I cannot get it running by restoring the settings. I didn't change any code.
The second test app works fine................I compared the code files in the project and can't find anything different between the two.
Is there anything outside of the project files somewhere that might be causing this, registry key or such.......My brain is turning to mush at this point.
Thanks for the help, Burl
|||
Additional information,
Looks like I had multipe problems. I have a habit of moving the solution files around to different folders in VS-2003, such as ActiveProjects, ReleaseProjects, doing so never gave me so much grief as doing this in VS-2005. Just renaming the parent folder cause's the error's along with the above examples.
So, what is the answer to moving a solution to another location?
|||In your case, it seems your configuration file had a connection string specifying the path to the database. When you moved your project, the path to the database changed and the error was generated cause your connection string was looking in the wrong place. Typically, movement of the data file is handled by the use of the |DataDirectory| macro being used as part of the connection string as in...
AttachDbfilename=|DataDirectory|MyDatabase.mdf
VS knows where the Data Directroy is for your project (or you can set it yourself programatically) and attaches the database for you. If you specify an absolute path, the database has to stay in that path as VS will not automatically change the stored connection string for you.
Mike
|||Thanks for the reply Mike:
After finally getting this application running again, after a few days I ran into this related problem: When I try to open the Main Form in the Designer I get the Big Red X with a stack trace list and the error message as follows.
"An attempt to attach an auto-named database for file C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\dbInventory.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
At this point the application runs fine, but I just can't view the Main Form in the designer without it barking about the database connection. Looking at the trace info, it was complaining about a userControl that gets added to the form. So I commented out the line in the designer.vb file where the userControl is added to the forms collection. The form designer now shows the form layout with no errors. I uncommented that line of code and the error came back. So this time I commented out the line again and ran the application just fine. I again uncommented the code and this time the form layout came back OK, no errors.....
Somewhere, somehow a file is getting out of sync but I sure can't find it. The connection string in the app.config file and in the Settings.Designer.vb file look correct.
App.config file string.
<connectionStrings>
<add name="<MyApplicationName>.My.MySettings.dbInventoryConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbInventory.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Settings.Designer.vb Connection String Property
<Global.System.Configuration.ApplicationScopedSettingAttribute(), _
Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
Global.System.Configuration.SpecialSettingAttribute(Global.System.Configuration.SpecialSetting.ConnectionString), _
Global.System.Configuration.DefaultSettingValueAttribute("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbInventory.mdf;Integra"& _
"ted Security=True;Connect Timeout=30;User Instance=True")> _
Public ReadOnly Property dbInventoryConnectionString() As String
Get
Return CType(Me("dbInventoryConnectionString"),String)
End Get
End Property
End Class
At least I know how to get it fixed at this point, any ideas on the cause. I deleted the bin folder contents and that didn't help.
Thanks, Burl
|||
I'm having a real problem with the path to your database in the error: C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\dbInventory.mdf
I would never expect a database to be located there, that is part of the VS program installation and is not normally used as the Data Directory. Are you doing anything in your application to set the DataDirectory to this location? Where are your project files located? How did you create this database?
Also, is this a Windows Forms application or ASP.NET? Are you running your application by hitting F5?
Thanks for the additional information.
Mike
|||I created the database using the Microsoft SQL Server Management Studio Express, version 9.00.2047.00. Later I detached the database and moved the files into a folder located in C:\MySqlDatabases. This is a Windows form application, I created a Setup Project for it as well, no clickOnce deployment. I get the errors by clicking the "Start Debugging Icon" or by pressing F5.
I started working on some userControls for this project a few weeks ago and didn't need the database until recently. So I go the add the database using the Data Source Configuration Wizard. I browsed to the file location and selected the mdf for this project in the C:\MySqlDatabases folder using Windows authentication and DataSource is Microsoft SQL Server Database(SqlClient), the connection string shows up like this "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\MySqlDatabases\dbInventory.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True". It then asks if I want to copy the data file to my project and I say Yes. It then saves my connection string to the app.config file as "dbInventoryConnectionString". The connection string in the Settings Designer is "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbInventory.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True". The connection string in the app.config file is the same.
I don't understand why the designer is barking about the database in "C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\dbInventory.mdf " when that userControl is trying to load in the main form, again if I comment out the line where it gets added to the forms collection, run the application and uncomment the line the error goes away. However, in that userControl I have one call to the database to fill a dataset, I had that code commented out while I tried to figure out what was going on. Here is the code for that call.
Private Sub CreateCategoryButtons()
Dim da As New dsCategoriesTableAdapters.CategoriesTableAdapter
Dim ds As New dsCategories da.Fill(ds.Categories)
Me.CategoryStackStrip.Items.Clear()
For Each row As DataRow In ds.Categories
Dim btn As New ToolStripButton
With btn
.Text = row.Item("Category").ToString
.Font = New Font("Tahoma", 8.25, FontStyle.Bold, GraphicsUnit.Point)
.Image = My.Resources.alarmclock
.ImageScaling = ToolStripItemImageScaling.SizeToFit
.CheckOnClick = True
.Alignment = ToolStripItemAlignment.Left
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.ImageAlign = ContentAlignment.MiddleLeft
.Margin = New Padding(0)
.Padding = New Padding(2)
.TextAlign = ContentAlignment.MiddleRight
End With
' Add to Category stack.
Me.CategoryStackStrip.Items.Add(btn)
' Add click event handler for category buttons.
AddHandler btn.Click, AddressOf OnCategoryButton_Click Next
' Activates the first button in the stack.
Me.CategoryStackStrip.Items(0).PerformClick()
End Sub
Now that this code is back into the game, the application fails on trying to read the connection string property in the Settings.Designer.vb code. Exception is {"Configuration system failed to initialize"} and the innerException is {"An error occurred loading a configuration file: Could not find file 'C:\Documents and Settings\Burl\<ApplicationNameHere>.exe_Url_trdwo0grhpv0v5se5luy1xl11k1tlbl2\1.0.0.0\user.config'. (C:\Documents and Settings\Burl\Application Data\...\<ApplicationNameHere>.exe_Url_trdwo0grhpv0v5se5luy1xl11k1tlbl2\1.0.0.0\user.config)"}.
I have no idea why its looking in this location for the config file. I even deleted the dataset and rebuilt it by drag and drop from the Server Explorer onto a new dataset file. Same problem.... All the connection string information in this solution is the same.........
"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbInventory.mdf;Integra"& _
"ted Security=True;Connect Timeout=30;User Instance=True"
This was all working a few days ago, then I started working on it the next day and this is the mess I'm in now...........ugh.
Burl
|||I finally uncovered the root cause of the problem......
When you try to add a user control to a Microsoft Windows Forms-based application in Microsoft Visual Studio 2005, you may receive the following error message:
An attempt to attach an auto-named database for file DriveLetter:\Program Files\Microsoft Visual Studio 8\Common7\IDE\DatabaseName.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
You experience this problem if the user control contains a data-binding object that uses an attached local database file.
http://support.microsoft.com/default.aspx/kb/908038
Ok, here is the workaround I came up with. In my userControl for the Load Event I have code that hits the database and builds some buttons for a menu strip. I wrapped the code with a designMode test.
Original UserControl Load Event:
Private Sub ucNavigation_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Add Category Items and Overflow items.
CreateCategoryButtons()
AddOverflowItems()
' Set Height.
InitializeSplitter()
End Sub
New UserControl Load Event:
Private Sub ucNavigation_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Add Category Items and Overflow items.
If Not Me.DesignMode Then
CreateCategoryButtons()
End If
AddOverflowItems()
' Set Height.
InitializeSplitter()
End Sub
After adding the code I closed all the open windows in the designer and rebuilt the solution, now all of the forms open with no errors and no more database or connection string errors. I hope this information helps someone else, cause it's been a real nightmare for me, so for now I've got my fingers crossed...........
Burl
|||
Thanks for the update Burl,
This is good information for everyone who is working with user controls.
Mike
No comments:
Post a Comment