Thursday, February 16, 2012

ASP.NET ERROR: Cannot open user default database. Login failed.

Error: Cannot open user default database. Login failed.
                  Login failed for user '[-----]\user'.

Solution:
If you are specifying a database file to attach to by using the AttachDBFilename in your connection string like this:


<add name="MyConnString"
          connectionString="Data Source=(local)\SQLEXPRESS;
          AttachDbFilename=|DataDirectory|\MyDatabase.mdf;
          Integrated Security=True;     />

be sure to include User Instance=True like the one below


<add name="MyConnString"
          connectionString="Data Source=(local)\SQLEXPRESS;
          AttachDbFilename=|DataDirectory|\MyDatabase.mdf;
          Integrated Security=True;
          User Instance=True;"         />

Refer to http://msdn.microsoft.com/en-us/library/ms247257.aspx#sectionToggle2 to understand user instances