You may receive an error message when you try to add a user control to a Windows Forms-based application if the user control contains a data-binding object that uses an attached local database file in Visual Studio 2005 (908038)



The information in this article applies to:

  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Express Edition
  • Microsoft SQL Server 2005 Express Edition

SYMPTOMS

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.

CAUSE

This problem occurs because Microsoft SQL Server 2005 Express Edition lets you set the DataDirectory keyword. The DataDirectory keyword determines where SQL Server 2005 Express Edition looks for database files. When the connection string contains the text "|DataDirectory|", SQL Server 2005 Express Edition replaces this text with the value of the DataDirectory keyword that you set. Then, SQL Server 2005 Express Edition connects to the database that uses this new connection string. By default, the value of the DataDirectory keyword is the current working folder of the Devenv.exe program that requests the connection to the database file. Because the database file is not located in the current working folder, you receive an error message.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

WORKAROUND

To work around this problem, use one of the following methods:
  • Put code in a method instead of in the Form_Load section of the application. Then, you can call the method when you have to.
  • Do not fill control data at design time. Instead, use the DesignTime property.

MORE INFORMATION

Steps to reproduce the problem

  1. Start Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual Basic Windows under Project types, and then click Windows Application under Template.
  4. Type ApplicationName in the Name box, type Drive:\Path in the Location box, and then click OK.
  5. On the Data menu, click Add New Data Source.
  6. In the Data Source Configuration Wizard, click Database, and then click Next.
  7. In the Choose Your Data Connection dialog box, click New Connection. The Add Connection dialog box appears.
  8. In the Add Connection dialog box, click Change.
  9. In the Change Data Source dialog box, click Microsoft SQL Server Database File, and then click OK.
  10. In the Add Connection dialog box, click Browse, locate and then click northwnd.mdf, and then click OK.
  11. In the Data Souce Configuration Wizard, expand the tables, click the Customers table, and then click Finish.
  12. In Solution Explorer, right-click ApplicationName, click Add, and then click User Control.
  13. In the Add New Item - ApplicationName dialog box, click User Control, and then click Add.
  14. In the Data Sources dialog box, add the Contact table to the UserControl control.
  15. Double-click the UserControl control, and then paste the following code example in the Control Load handler.
    Me.CustomersTableAdapter.Fill(Me.NorthwindDataSet.Customers)
    
  16. Add the UserControl control to the Windows Form.

    You receive the error message that is mentioned in the "Symptoms" section.

Modification Type:MajorLast Reviewed:6/12/2006
Keywords:kbtshoot kbprb KB908038 kbAudITPRO kbAudDeveloper