Error deleting a record from the SQL server database asp.net

0

I have tried these two ways.

1
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)// el id llega con toda normalidad
{
    db.Entry(db.Detalle.Find(id)).State = EntityState.Deleted;
    db.SaveChanges();

    return RedirectToAction("Index");
}
2
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id) // el id llega con toda normalidad
{
    Detalle idar = db.Detalle.Find(id);
    db.Detalle.Remove(idar);
    db.SaveChanges();

    return RedirectToAction("Index");
}

and both give me the same error

  

The user code did not control System.ArgumentNullException     HResult = -2147467261     Message = The value can not be null.   Parameter name: entity     ParamName = entity     Source = EntityFramework     StackTrace:          in System.Data.Entity.ModelConfiguration.Utilities.RuntimeFailureMethods.Requires (Boolean condition, String userMessage, String conditionText)          in System.Data.Entity.DbSet 1.Remove(TEntity entity) en SIGA.net.Controllers.ArqueoController.DeleteConfirmed(Int32 id) en C:\Proyectos\SIGA.net\SIGA.net\Controllers\ArqueoController.cs:línea 173 en lambda_method(Closure , ControllerBase , Object[] ) en System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) en System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2 parameters)          in System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod (ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary% co_of% 1.b__7 (IAsyncResult _)          in System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult'1.End ()          in System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod (IAsyncResult asyncResult)          in System.Web.Mvc.Async.AsyncControllerActionInvoker. < > c__DisplayClass37. < > c__DisplayClass39.b__33 ()          in System.Web.Mvc.Async.AsyncControllerActionInvoker. < > c__DisplayClass4f.b__49 ()     InnerException:

    
asked by Rodrigo Rodriguez 21.12.2017 в 15:36
source

2 answers

1

Do not use linq, use it this way and you'll be happy ..

 string query = @"Delete from Detalle where Id = @Id";

            SqlCommand cmdIns = new SqlCommand(query, ConexionDB);
            cmdIns.Parameters.Add("@Id", id);

            ConexionDB.Open();
            cmdIns.ExecuteNonQuery();
            ConexionDB.Close();
    
answered by 21.12.2017 / 16:26
source
0

You can try on the line db.Detalle.Find with db.Detalle.FirstOrDefault although in this case I think it's better db.Detalle.SingleOrDefault or just db.Detalle.Single reference difference between single find and firstorDefaul

try and see how you are doing

    
answered by 21.12.2017 в 16:11