I have created a web service which is saving some data into to db. But I am getting this error:
Cannot open database "test" requested by the login. The login failed. Login failed for user 'xyz\ASPNET'.
My connection string is
Data Source=.\SQLExpress;Initial Catalog=IFItest;Integrated Security=True
It also happen when you type wrong name of DB
Sometime, it's just a stupid mistake . I take about more than 1 hours to find out this :( because i just try alot of difficult thing first
In my case the asp.net application can usually connect to database without any problems. I noticed such message in logs. I turn on the SQL server logs and I find out this message:
So it seems that server was restarting and that SQL server whad been shutting down a bit earlier then ASP.NET application and the database was not available for few seconds before server restart.
Most times, it's not a login issue, but an issue with creating the database itself. So if there is an error creating your database, it would not be created in the first place. In which case if you tried to log in, regardless of the user, login would fail. This usually happens due to logical misinterpretation of the db context.
Visit the site in a browser and REALLY read those error logs, this can help you spot the problem with you code (usually conflicting logic problems with the model).
In my case, the code compiled fine, same login problem, while I was still downloading management studio, I went through the error log, fixed my db context constraints and site started running fine....meanwhile management studio is still downloading
Even if you've set the login as DB owner and set the user mapping for the database that will use the login, check that the actual DB user (not just the login) has the role of 'owner'.
The Issue
The error presents itself as a message similar to this:
The Fix
The solution is laid in the following steps. You will not lose any data in your database and you should not delete your database file!
Pre-requisite: You must have installed SQL Server Management Studio (Full or Express)
The source of the solution: https://www.codeproject.com/Tips/775607/How-to-fix-LocalDB-Requested-Login-failed
The best solution for the login problem is to create a login user in sqlServer. Here are the steps to create a SQL Server login that uses Windows Authentication (SQL Server Management Studio):
For example, if the user name is
xyz\ASPNET
, then enter this name into Login name Box.Also you need to change the User mapping to allow access to the Database which you want to access.