How to delete record from database in asp.net/MVC
How to delete record of any table from database?
How to delete record in Asp.net/mvc?
Step 1: Make a controller method with the following code, in this method we have to pass id of the category table on the base of this id which we have to delete the record.
public ActionResult DeleteCategory(int ID)
{
try
{
BLL_Category obj = new BLL_Category(); // Bll object creation
obj.Bll_Delete_Category(ID); //call to BLL method and pass id as a parameter
return RedirectToAction(“Category”, “Admin”); //this is simple the redirect action where you want to redirect after a complete call in this method.
}
catch (Exception)
{
throw;
}
}
Step 2: Don’t create any page on the base of above controller method we just have to call this method from already created display page, first of all go to display page we have created previously while displaying category table data . following is the complete display page code, in this code you have to change last <td> to call delete and edit method, by just calling the controller method name like this highlighted in below code.
@model IEnumerable<Pick_n_Buy.Models.MdlCategory>
@{
ViewBag.Title = “Category”;
Layout = “~/Views/Shared/_LayoutPage1.cshtml”;
}
<h2 class=”text-secondary text-center”>Categories</h2>
<table class=”table table-secondary table-hover”>
<tr class=”row col-md-12″>
<th class=”col-md-3 text-center”>
@Html.DisplayNameFor(model => model.Name)
</th>
<th class=”col-md-3 text-center”>
@Html.DisplayNameFor(model => model.Description)
</th>
<th class=”col-md-4 text-center”>
@Html.DisplayNameFor(model => model.Thumbnail)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr class=”row text-primary text-center”>
<td class=”col-md-3″>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td class=”col-md-3″>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td class=”col-md-4″>
<img src=”@Url.Content(item.Thumbnail)” width=”200″ height=”100″ />
</td>
<td>
@Html.ActionLink(“Edit”, “UpdateCategory”, new { id = item.ID }) |
@Html.ActionLink(“Delete”, “DeleteCategory”, new { id = item.ID }) @*delete is the name to display in view page, deletecategory is the name of controller method , this method will call and we will pass id from here because every record id is binded while displaying category data *@
</td>
</tr>
}
</table>
Step 2: Defined Bll Method , id is passed as a parameter and further we will call DAL class method from this.
public void Bll_Delete_Category(int ID) // id is passed as a parameter
{
DAL_Category obj = new DAL_Category(); // DAL object creation
obj.DLL_Delete_Category(ID); // Dal Method is call with id as a parameter
}
Step 3: Defined DAL class method where we have to ma id to store procedure variable and pass connection string, command type, stored procedure name, and parameter, and call to sqlhelp class method to execuate
public void DLL_Delete_Category(int ID)
{
try
{
SqlParameter[] parm = new SqlParameter[1];
parm[0] = new SqlParameter(“@ID”, SqlDbType.Int)
{
Value = ID
};
SqlHelper.SaveData(this._ConnString, CommandType.StoredProcedure, “SPW_DeleteCategory”, parm);
}
catch (Exception)
{
throw;
}
}
Step 4: SQL helper class method
public static int SaveData(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandparameters)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = commandType;
cmd.CommandText = commandText;
for (int i = 0; i < commandparameters.Length; i++)
{
cmd.Parameters.Add(commandparameters[i]);
}
cmd.Connection = con;
return cmd.ExecuteNonQuery();
}}
Step 5: Stored procedure
Create procedure [dbo].[SPW_DeleteCategory]
(
@ID int
)
AS
BEGIN
DELETE FROM Tbl_Category WHERE ID=@ID
END