2

I have an error regarding connecting to my database while querying "employee" from browser using this query string:

EmployeeController.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Models;
using System.Net;

namespace MVCDemo.Controllers
{
    public class EmployeeController : Controller
    {
        // GET: Employee
        public ActionResult Details(int id)
        {
            EmployeeContext employeeContext = new EmployeeContext();
            Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id);

            return View(employee);
        }
    }
}

Screenshots from my machine for the error:

https://ibb.co/tPGwSGW https://ibb.co/DbKZgVQ

Web.config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <trace enabled="true" pageOutput="false" />
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>
  </system.webServer>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="EmployeeContext" connectionString="server=.; database=Sample; integrated security=SSPI"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

and from SQL Server Management Studio. I am using SSMS 18.0 (GA)

I connect to SSMS with Server name: .\sqlexpress

enter image description here

I tried different connection strings and enabling TCP/IP using SQL Server Configuration Manager yet the same error do exists.

  • Code and errors are text, not graphics. Please post your code as text within the question, so it is more easily readable, can be copied by those who want to use it in an answer, and so error messages are searchable by those experiencing a similar problem. Thanks. – ADyson May 12 '19 at 18:11
  • Anyway it seems the error is something like you got the wrong details in your connection string, since it was unable to connect to the database. Either that or a firewall is blocking the connection – ADyson May 12 '19 at 18:13
  • we can't really correct your connection string without knowing what type of SQL Server you're running and where it's installed relative to the C# code (e.g. same server, different server, is it SQL Express, is there an instance name?) How do you connect to it in SSMS? – ADyson May 15 '19 at 15:48
  • @ADyson question description edited above. – Mahmoud Arafa May 15 '19 at 16:12
  • So have you tried using `.\sqlexpress` in your .NET connection string? – ADyson May 15 '19 at 16:17
  • Yikes! This solved my problem but another error emerged: "Cannot open database "Sample" requested by the login. The login failed. Login failed for user 'IIS APPPOOL\DefaultAppPool'." – Mahmoud Arafa May 15 '19 at 16:24
  • See https://stackoverflow.com/questions/1933134/add-iis-7-apppool-identities-as-sql-server-logons – ADyson May 15 '19 at 16:25
  • How to get "YourAppPoolName "? – Mahmoud Arafa May 15 '19 at 21:38
  • You can look in the Application Pools view in IIS. In your case, though, the error message you reported above has already told you it! – ADyson May 15 '19 at 21:40
  • Well I created the login and it didn't work out with me. I googled a bit and tried some solutions from web but the problem still do exist. "Cannot open database "Sample" requested by the login. The login failed. Login failed for user 'IIS APPPOOL\DefaultAppPool'." – Mahmoud Arafa May 16 '19 at 07:43
  • The other option is you can configure the Application pool to run under a specific windows user account instead, and then add that user to your SQL server – ADyson May 16 '19 at 09:57

0 Answers0