0

I am using membership class for my user management, and it created a database called ASPNETDB.MDF.. I decided to use the same database to handle my other data, and so I added some of my own tables in there...

When I try to access it:

    <connectionStrings>
    <add name="connString" connectionString="Initial Catalog=MyProject;Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\MyName\Documents\Visual Studio 2008\Projects\Project\MyProject\App_Data\ASPNETDB.MDF;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
</connectionStrings>

Using this:

Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("connString").ToString)

It gives me this error after I log in through Membership class:

    Cannot open database "MyProject" requested by the login. The login failed.
Login failed for user 'My-PC\Myuser'.

I am not sure what's going on?

Edit: If i don't use Membership class, I can use the database fine.. but when after I login using the membership class, it stops to work..

4 Answers 4

1

Make sure My-Pc\MyUser has access appropriate permissions to 'MyProject' database. For the test environment that my local projects run on, i generally assign myself as the database owner to the database i want to access. That is if i am the only user accessing it. You can do so by running EXEC sp_changedbowner 'My-Pc\MyUser'. Obviously, you want to dedicate a separate account with limited access for your production environment.

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

2 Comments

The owner is set correctly.. It's already My-Pc/MyUser. I can connect to it if I don't use the Membership class, but when I do, and log-in, I can't seem to connect to it.
Can you remove 'initial catalog' section? I don't believe you need that if you use user instances.
0

You'll probably need to make sure that the user the web server is running as has read/write access to the db file and directory.

3 Comments

the user has read/write access to both db file and directory... If I don't use membership class and try to access the database, it works fine...
What does the <membership> section of your web.config look like?
Strangely, I don't have a section on membership.. I have this: <authentication mode="Forms"> <forms loginUrl="Login.aspx" /> </authentication>
0

Try removing "connString" from web.config and using "LocalSqlServer" connection string instead (it is defined in machine.config):

Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("LocalSqlServer").ToString)

2 Comments

Where can i find the machine.config file?
On my machine it's in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG
0

I also encountered this problem, and I noticed that it does indeed work if I changed the owner of the database to 'My-Pc\MyUser' (via EXEC sp_changedbowner 'My-Pc\MyUser') as pointed out above.

You might also try running this on your database:

exec sp_grantlogin 'My-PC\MyUser2'

This is certainly useful if you want MyUser AND MyUser2 having access to your database. I encountered this scenario when I have to execute aspnet_regsql.exe using MyUser2 credentials (as the owner of my database is ASPNET, which does NOT have login/runas capabilities).

Hope this helps someone :)

Comments

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.