Senal
Senal

Reputation: 13

Cannot open the DB error

int userId = 0;

string roles = string.Empty;

string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

using (SqlConnection con = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand("Validate_User"))
    {
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@Username", Login1.UserName);
        cmd.Parameters.AddWithValue("@Password", Login1.Password);
        cmd.Connection = con;

        con.Open();

in the web.config, the connection string is defined as

<add name="constr" 
     connectionString="data source=.;initial catalog=LoginDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" 
     providerName="System.Data.SqlClient" />

but when I run it, I get an error:

System.Data.SqlClient.SqlException: Cannot open database "LoginDB" requested by the login. The login failed.
Login failed for user 'NT AUTHORITY\SYSTEM'.

Any solutions?

Upvotes: 0

Views: 267

Answers (2)

ElasticCode
ElasticCode

Reputation: 7865

You can give NT AUTHORITY\SYSTEM user permission in SQL Server, which I don't recommend.

Another way is to use SQL Server Authentication and specify the User Id and Password

<add name="constr" providerName="System.Data.SqlClient" connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=False;User Id=userid;Password=password;MultipleActiveResultSets=True" />

Upvotes: 0

Fred Mauroy
Fred Mauroy

Reputation: 1229

As you use integrated security, you must either give the default system user rights to connect to the DB (not a good idea) or configure the application pool the web app is using to run under a specific user which has the required rights on the DB. A third option would be not to use integrated security and store the SQL username and password in the connection string. But integrated security is the best way if you can use it. HTH

Upvotes: 1

Related Questions