SqlException: The server was not found

Joined
Aug 14, 2021
Messages
18
Programming Experience
Beginner
Hello.I hope there isn't a SQL server connection problem,it seems that it doesn't read the file correctly and can't find the information from the server?Thank you for any help


The code where the problem is(EmployeeManagerController):
C#:
using EmployeeManagerApp.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EmployeeManagerApp.Controllers
{
    public class EmployeeManagerController : Controller
    {
        private AppDbContext db = null;

        public EmployeeManagerController(AppDbContext db)
        {
            this.db = db;
        }

        private void FillCountries()
        {
            List<SelectListItem> countries = (from c in db.Countries
                                              orderby c.Name ascending
                                              select new SelectListItem() { Text = c.Name, Value = c.Name }).ToList();

            ViewBag.Countries = countries;
        }

        [HttpGet]
        public IActionResult Create()
        {
            FillCountries();
            return View();
        }

        [HttpPost]
        public IActionResult Create(Employee emp)
        {
            FillCountries();

            if (ModelState.IsValid)
            {
                db.Employees.Add(emp);
                db.SaveChanges();
            }
            return View(emp);
        }

        public IActionResult Index()
        {
            return View();
        }
    }
}
 

Attachments

  • Screenshot 2021-10-03 152730.png
    Screenshot 2021-10-03 152730.png
    211.4 KB · Views: 15
  • Screenshot 2021-10-03 152742.png
    Screenshot 2021-10-03 152742.png
    66.4 KB · Views: 12
  • Screenshot 2021-10-03 153418.png
    Screenshot 2021-10-03 153418.png
    125.2 KB · Views: 11
  • Screenshot 2021-10-03 153551.png
    Screenshot 2021-10-03 153551.png
    110 KB · Views: 11
Last edited by a moderator:
None of the code provided is relevant to the issue. The error message tells you what the problem is and what to do about it. The issue is that your connection string refers to either a non-existent SQL Server instance or one that the application is unable to access. Check your connection string and your SQL Server configuration. Try connecting to it in the Server Explorer in VS and get the connection string from there.
 
Back
Top Bottom