0

I am using default ASP.NET membership provider and aspnetdb.mdf in App_Data folder. I then decided to delete aspnetdb.mdf from the App_Data folder and create a new one in SQL Server via aspnet_regsql.exe and modified the connection string accordingly.

Now while checking the role of the user I get an exception error which indicates that it still looks for aspnetdb.mdf at the old path (and not in SQL Server).

This is my code:

void Application_Start(object sender, EventArgs e)
{
        // Code that runs on application startup
        if (!Roles.RoleExists("Administrator"))
        {
            Roles.CreateRole("Administrator");
        }

        if (Membership.GetUser("Admin") == null)
        {
            Membership.CreateUser("Admin", "mrtcn.1907");
            Roles.AddUserToRole("Admin", "Administrator");
        }
}

This is the exception I get:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Web.dll but was not handled in user code

Additional information: An attempt to attach an auto-named database for file C:\Users\muratcan\Documents\Visual Studio 2013\Projects\MedicalBootStrap\MedicalBootStrap\App_Data\aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Here is the connection string;

<add name="ApplicationServices" 
     connectionString="data source=MRTCN-PC\SQLEXPRESS;User Id=medicalusr;Password=123456;AttachDBFilename=|DataDirectory|\aspnetdb.mdf" 
     providerName="System.Data.SqlClient" />

There is most probably another place to be modified accordingly that it looks for the aspnetdb.mdf at the right path.

After Win's suggestion, I have changed the connection string accordingly

This is the modified connection string;

<add name="ApplicationServices" 
     connectionString="data source=MRTCN-PC\SQLEXPRESS;User  Id=medicalusr;Password=123456;AttachDBFilename=aspnetdb.mdf" 
     providerName="System.Data.SqlClient" />

And this is the new exception;

{"An attempt to attach an auto-named database for file aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."}

2
  • Have you updated the ConnectionString to point to new database? About connection string is still pointing to old LocalDB. Commented Aug 22, 2014 at 16:29
  • @Win I have added my connection string too, if "AttachDBFilename=|DataDirectory|\aspnetdb.mdf" "DataDirectory" points to the MS SQL Path (not App_Data folder) then it probably points to new database Commented Aug 22, 2014 at 16:32

1 Answer 1

2

AttachDBFilename=|DataDirectory|\aspnetdb.mdf means your LocalDB.

If you connect to SQL Server instead of LocalDB, ConnectionString should be something like this -

<connectionStrings>
   <add name="ApplicationServices" 
     connectionString="Data Source=MRTCN-PC\SQLEXPRESS;Initial Catalog=DATABASE_NAME;Persist Security Info=True;User ID=medicalusr;Password=123456" 
     providerName="System.Data.SqlClient"/>
</connectionStrings>

Sample SqlConnection string.

Sign up to request clarification or add additional context in comments.

3 Comments

I have changed the connection string as you suggested now I get another exception, it still try to create an aspnetdb.mdf automatically. You can see the new exception in my question
@mctuna Do not use AttachDBFilename which is causing the problem. Instead, you want to use Initial Catalog.
It still can not login to database with the username medicalusr but this is due to permissions probably, I gave it full control though, strange

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.