This error is quite common and I have received it few times while I was working on a recent consultation project.
Cannot open database requested by the login. The login failed.
Login failed for user ‘NT AUTHORITY\NETWORK SERVICE’.
This error occurs when you have configured your application with IIS, and IIS goes to SQL Server and tries to login with credentials that do not have proper permissions. This error can also occur when replication or mirroring is set up.
If you search online, there are many different solutions provided to solve this error, and many of these solutions work fine. However, I will be going over a solution that works always and is very simple.
Fix/Workaround/Solution:
Go to SQL Server >> Security >> Logins and right click on NT AUTHORITY\NETWORK SERVICE and select Properties
In newly opened screen of Login Properties, go to the “User Mapping” tab. Then, on the “User Mapping” tab, select the desired database – especially the database for which this error message is displayed. On the lower screen, check the role db_owner. Click OK.
In almost all such cases, this should fix your problem.
Reference : Pinal Dave (https://blog.sqlauthority.com)
225 Comments. Leave new
I am still getting an error : Cannot open the database even though the user is db_owner . Any suggestion what else I can try to resolve it? Thanks!
Still having this error:
Cannot open database “TheDb” requested by the login. The login failed.
Login failed for user ‘NT AUTHORITY\SYSTEM’.
I should say that the database is restored from a backup got from another machine. And also My SSMS is of version: Microsoft SQL Server Management Studio 13.0.16106.4 (2016)
Thank you soo much its working for me..
Works now. Sorry
Nope, won’t work. As it throws the bloody error when clicking on Properties. Extremely annoying SSMS magic behavior driving me nuts every now and then with this nonsense. IIS was stopped, so it isn’t that.
Ah .. right. Silly me, close the program. Reopen, then click Options before sign-in and change the default database there. That’ll work.
In my case, the database that generates the error has been deleted. So going to User Mapping won´t list that database but it will still generate that error. Any ideas?
Pls try following steps:
1. In Microsoft Visual Studio 2010, open Web Application then
Tools> Connect To DataBase> Add Connection Box is coming up.
In this Box, pls choose your connection correctly. Then click OK.
2. Then Pls write the following code in your Web Application asp.net (Default.aspx.cs tab or page)
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = “Data Source=hp;Initial Catalog=dbname;Persist Security Info=True;User ID=sa;Password=******”;
con.Open();
}
protected void BtnName_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand(“Insert into tblTest” + “(Email)values(@email)”, con);
cmd.Parameters.AddWithValue(“@email”, txtname.Text);
cmd.ExecuteNonQuery();
this.lblname.Text = this.txtname.Text;
}
}
}
Thanks for sharing the code.
It effing did. THANK you!
Solved in 5 sec… thanks!
nice thanks ..
lol sometimes it can be because you wrote a typo.. silly myself had a typo in my database name…
Hello sir,
In mysql database default user is root and password=”. what is the default username and password for SQL database in IIS. i want to connect laravel project with SQL database with username=” and password ” . by i am facing this error
PHP Fatal error: Invalid handle returned. in vendorlaravelframeworksrcIlluminateDatabaseConnectorsConnector.php on line 55
what will be the solution.
Thanks
worked for me
Another and maybe stupid mistake is when you try to connect through management studio, and have forgotten to erase the Additional connection parameters under Options in the login dialogue.
Thanks so much its worked for me
If your hosting server is different than localhost then just restart the server .
I don’t have NT AUTHORITYNETWORK SERVICE in my logins. How do I add that in?
Things are working fine on my system but when same project is run on another system it gives error can any thing be done in generalize way so asp.net core project can run any where without authentication error
Thankyou soo much bro..Its working for me <3
it worked
thanks