Delete From does not work in C #

1

Why does not the code below work (does not delete)?

using (Banco db = new Banco()) { 
    String strSql = "Delete from Cliente where codcliente=" + Session["_uiUserID"];
    db.Cliente.SqlQuery(strSql);
    db.SaveChanges();
}

You are not throwing any Exception and catching the string that is inside the strSql variable and running directly on the normal working bank.

    
asked by anonymous 24.06.2014 / 19:45

1 answer

1

Possibly where is not finding any records.

To test better, put a break point on the line String strSql = "Delete from Cliente where codcliente=" + Session["_uiUserID"]; and get the result of querystring and execute directly on the database.

It may be that Session["_uiUserID"] is not blank or it no longer exists in the Cliente table

Another way to delete with EF

using (Banco db = new Banco()) { 
    var c = db.Cliente.First(x => x.codcliente == Session["_uiUserID"]);
    db.Cliente.Remove(c);
    db.SaveChanges();
}

In this way, you will be sure that the client with Session["_uiUserID"] exists in the Client table and, if it exists, it will be deleted. If it does not exist, the First method will throw an exception.

    
24.06.2014 / 19:57