How to Delete Record with Dapper ORM and ASP.NET MVC 3

No.of Views3694
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  02 Jul 2011 10:07:50
Tag : ASP.NET , ASP.NET MVC
I have been writing few articles about Dapper ORM and ASP.NET MVC3 for data manipulation. In this post I am going to explain how we can delete the data with Dapper ORM. For your reference following are the links my previous articles.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

I have been writing few articles about Dapper ORM and ASP.NET MVC3 for data manipulation. In this post I am going to explain how we can delete the data with Dapper ORM. For your reference following are the links my previous articles.

http://www.codegain.com/articles/aspnet/aspnetmvc/workaround-with-dapper-micro-orm-and-aspnet-mvc-3-0.aspx

http://www.codegain.com/articles/aspnet/aspnetmvc/insert-with-dapper-micro-orm-and-aspnet-mvc-3.aspx

http://www.codegain.com/articles/aspnet/aspnetmvc/how-to-update-data-with-dapper-orm-and-aspnet-mvc-3.aspx

So to delete customer we need to have delete method in Our CustomerDB class so I have delete method into the CustomerDB class like following.

public bool Delete(int customerId)
{
try
{
    using (System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(Connectionstring))
    {
        sqlConnection.Open();
        string sqlQuery = "DELETE FROM [dbo].[Customer] WHERE CustomerId=@CustomerId";
        sqlConnection.Execute(sqlQuery, new {);
        sqlConnection.Close();
 
    }
    return true;
}
catch (Exception exception)
{
    return false;
}
}

Now our delete method is ready It’s time to add ActionResult for Delete in Customer Controller like following. I have added two Action Result first will load simple delete with view and other action result will delete the data.

public ActionResult Delete(int id)
{
var customerEntities = new CustomerDB();
return View(customerEntities.GetCustomerByID(id));
}
 
//
// POST: /Customer/Delete/5
 
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
    var customerEntities = new CustomerDB();
    customerEntities.Delete(id);
    return RedirectToAction("Index");
 
 
}
catch
{
    return View();
}
}

Now It’s time to add delete view. So I have added strongly typed view like following.

Image Loading

Now everything is ready with code. So it’s time to check functionality let’s run application with Ctrl + F5. It will load browser like following.

Image Loading

Now I am clicking on delete it will load following screen to confirm deletion.

Image Loading

Once you clicked delete button it will redirect back to customer list and record is delete as you can see in below screen.

Image Loading

Hope help and if you have any comments please post it.thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Gold
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</