0

I have an ASP.NET app, and when I launch the app from Visual Studio and try to list items from a table by accessing the corresponding route, I get this error:

SqlException: Cannot open database "appointments" requested by the login. The login failed. Login failed for user 'DESKTOP-KRBN07G\juan_'.

I use SQL SERVER MANAGEMENT STUDIO, in login I have DESKTOP-KRBN07G\juan_ with the table checked in User Mapping and db_owner checked as role. I also added a user DESKTOP-KRBN07G\juan_ to that DB.

I can connect to the DB from Visual Studio and SSMS.

I'm new in the backend and I don't know how to fix this or what should I check, I get some scripts from chatGTP attempting to get some info, I run those script in SSMS and I get this:

Permission

In my appSettings.json file I use:

"ConnectionStrings": {
    "Appointments": "Data Source=DESKTOP-KRBN07G; 
  Initial Catalog=appointments;Integrated 
  Security=True;
  Trust Server Certificate=True"
  }

A colleague tried the app on his local development environment and didn’t get the error. I’m not sure what I should check or what to do.

3
  • Please let me know if more screenshots showing the settings are needed. As I mentioned, I’m not sure what I should check. Commented Sep 14 at 6:14
  • See if the SQL browser is running as service
    – nbk
    Commented Sep 14 at 10:18
  • 2
    Your Windows Event Log (on the machine where the SQL Server instance is running) should have more detailed error information on why the login failed.
    – J.D.
    Commented Sep 14 at 12:20

2 Answers 2

0

It might be that the SQL Server is set not to allow access except by Windows Authentication, I've seen this allow connections from DB tooling but not Apps etc.

If you check your logs it will be clear in there.

Steps to fix are along these lines:-

  • On the Object Explorer window right click on the server name and go to Properties.
  • Select the Security section.
  • Under Server Authentication change the selection from Windows Authentication mode to SQL Server and Windows Authentication mode.
  • Click Ok
1
  • 1
    The OP is using Windows Authentication in their app (via Integrated Security=True; in their connection string), so this shouldn't be the issue.
    – J.D.
    Commented Sep 14 at 12:16
0

Well, guys, I found the problem, it was a wrong and 'hidden' Connection String.

I created a new database with a different name, but the error in the browser after launching the app in my localhost kept pointing to the old database name.

After that, I searched for the old database name throughout the project and discovered that my colleague had added a partial class inside the DataAccess project of the ASP.NET application, which runs in development mode and contains his connection string.

I changed that, and now everything is working fine. All I knew was that the connection string should be in Program.cs or appsettings.json, and no other places.

1
  • You ate allowed to tick in 24hrs time. Commented Sep 15 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.