0
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?

1

2 Answers 2

1

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

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

Comments

0

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" />

3 Comments

yes thanks but i tried that but then i get an error like this .. Could not load file or assembly 'MySql.ConnectorInstaller, Version=6.10.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The system cannot find the file specified.
thats just it .. iv used VS with mysql in previous days .. now when i open this currnt project whch uses sql server that meessage comes
Please share a screenshot for project references

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.