I have a form made in MVC ASP.NET, using Entity framework for persistence. I try to save the data, but when I click save, Visual Studio throws me an exception in the line ctx.Empleado.Add(em);
of the Create action.
Then when I click continue it says "Error related to the network or specific to the instance while a connection was established with the SQL Server, the server was not found or the server was not accessible, check that the name of the instance is correct and that SQL Server is configured to support remote connections. (provider: SQL Network Interfaces, error: 26 - Error searching the specified server or instance) ". what's happening?
Controller:
using Ejercicio06.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Ejercicio06.Controllers
{
public class EmpleadoController : Controller
{
// GET: Empleado/Index
public ActionResult Index()
{
return View();
}
// GET: Empleado/Crear
public ActionResult Crear()
{
return View();
}
[HttpPost]
public ActionResult Crear(FormCollection form)
{
Context ctx = new Context();
Empleado em = new Empleado();
em.nombre= form["nombre"];
em.apellido = form["apellido"];
ctx.Empleado.Add(em);
ctx.SaveChanges();
return View();
}
}
}
Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Ejercicio06.Models
{
public class Empleado
{
public int Id { get; set; }
public string Nombre { get; set; }
public string Apellido { get; set; }
}
}
View:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Crear</title>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<div class="panel-body">
<h2 class="text-center">Registrarse</h2><br>
<form action="Crear" method="POST">
<!-- INPUT NOMBRE -->
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input class="form-control input-lg" type="text" name="nombre" placeholder="Ingresar Mail" />
</div>
<br>
<!-- INPUT APELLIDO -->
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input class="form-control input-lg" type="text" name="apellido" placeholder="Ingresar Apellido" />
</div>
<br>
<!-- BOTON GUARDAR -->
<div class="text-center">
<button type="submit" class="btn btn-success">Guardar</button>
<!-- LINK SALIR -->
<a href="../" class="btn btn-default">Cancelar</a>
</div>
</form>
</div>
</div> <!-- container -->
</body>
</html>