views:

1055

answers:

4

hello,

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..

A: 

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.

chris
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...
tpae
What does the <membership> section of your web.config look like?
chris
Strangely, I don't have a section on membership.. I have this: <authentication mode="Forms"> <forms loginUrl="Login.aspx" /> </authentication>
tpae
A: 

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.

Sergey
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.
tpae
Can you remove 'initial catalog' section? I don't believe you need that if you use user instances.
Sergey
SOLVED! I removed initial catalog and it works. Thank you!
tpae
Glad i could help :)
Sergey
A: 

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)
Pavel Chuchuva
Where can i find the machine.config file?
tpae
On my machine it's in `C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG`
Pavel Chuchuva
A: 

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 :)

azure_ardee