Asked by:
HELP ME!!!!

Question
-
User1692641958 posted
Help, when I make an insert, a duplicate is registered, that is when I register a plate with name: Hamburger, Category: Menu, Price: 22, I get two records of that data
public void Registrar(Plato reg)
{
SqlConnection getcn = new SqlConnection("server=CHRIS;database=repaso;uid=sa;pwd=sql");
try
{
getcn.Open();
SqlCommand cmd = new SqlCommand("sp_registrar_plato", getcn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@nombre", reg.PlatoNombre);
cmd.Parameters.AddWithValue("@idcategoria", reg.CategoriaidP);
cmd.Parameters.AddWithValue("@precio", reg.PlatoPrecio);int id = Convert.ToInt32(cmd.ExecuteScalar());
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
msg = ex.Message;
}
finally
{
getcn.Close();
}
}public ActionResult Registrar()
{
ViewBag.categoriaP = new SelectList(plato.ListadoCategoriaP(), "CategoriaidP", "CategoriaNombreP");
return View();
}
[HttpPost]
public ActionResult Registrar(Plato reg)
{
string msg = "";
if (!plato.Listado().Any(x => x.PlatoNombre == reg.PlatoNombre))
{
plato.Registrar(reg);
msg = "Plato se registro exitosamente";
TempData["Registrado"] = msg;}
else
{
msg = "Plato ya registrado anteriormente,Ingrese otro Plato";
TempData["YaRegistrado"] = msg;
}return RedirectToAction("Registrar");
}@model Res.Models.Plato
@{
ViewBag.Title = "Registrar";}
<!DOCTYPE html><link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script><html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Login</title>
<link href="~/css/RegistroPlato.css" rel="stylesheet" />
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head><body id="LoginForm">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="container">
<h1 class="form-heading">Registrar</h1><div class="login-form">
<div class="main-div">
<div class="panel">
<h2>Registrar Plato</h2>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })</div>
<form id="Login">
<div class="form-group">
<div class="text-info">@TempData["Registrado"]</div>
<div class="text-danger">@TempData["YaRegistrado"]</div><p id="label">Nombre:</p>
@Html.EditorFor(model => model.PlatoNombre)
<br />
@Html.ValidationMessageFor(model => model.PlatoNombre, "", new { @class = "text-danger" })</div>
<div class="form-group"><p id="label">Categoria:</p>
@Html.DropDownList("CategoriaidP", (SelectList)ViewBag.categoriaP)<br />
@Html.ValidationMessageFor(model => model.CategoriaidP, "", new { @class = "text-danger" })</div>
<div class="form-group"><div>
<p id="label">Precio:</p>
@Html.EditorFor(model => model.PlatoPrecio)
<br />
@Html.ValidationMessageFor(model => model.PlatoPrecio, "", new { @class = "text-danger" })</div>
<br />
<br />
<button type="submit" class="btn btn-primary">Registrar</button></div>
<br />
<div>
<div id="lista">
@Html.ActionLink("Ir a Atras", "Listado")
</div></div>
</form>
</div>
</div>
</div>}
</body>
</html>Tuesday, August 7, 2018 7:56 AM
All replies
-
User753101303 posted
Hi,
And what should be unique ? More likely the Name (ie "Hamburger") could be defined as being unique in the db so that the INSERT couldn't work and your app should do a check to show a message if someone tries to enter a name that already exists.
Or another option could be to change your sp_registrar_plato procedure so that if a row with the same name is found the current row is updated.
Tuesday, August 7, 2018 11:14 AM -
User-2146987983 posted
int id = Convert.ToInt32(cmd.ExecuteScalar());
cmd.ExecuteNonQuery();
You only need one of the above. Stick with cmd.ExecuteNonQuery();
Tuesday, August 7, 2018 12:00 PM -
User1724605321 posted
Hi RockChris,
Help, when I make an insert, a duplicate is registered, that is when I register a plate with name: Hamburger, Category: Menu, Price: 22, I get two records of that dataSo same name should be consider as duplicate record ? According to the codes ,you are checking the duplicate with PlatoNombre :
f (!plato.Listado().Any(x => x.PlatoNombre == reg.PlatoNombre)) { }
If name is another condition , you should also filter name as condition .
Best Regards,
Nan Yu
Wednesday, August 8, 2018 3:18 AM