Saturday, May 21, 2011

Delete with Dapper ORM and ASP.NET MVC 3

I have been writing few posts 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 posts.

Playing with dapper Micro ORM and ASP.NET MVC 3.0
Insert with Dapper Micro ORM and ASP.NET MVC 3
Edit/Update with dapper ORM and ASP.NET MVC 3

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)
    using (System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(Connectionstring))
        string sqlQuery = "DELETE FROM [dbo].[Customer] WHERE CustomerId=@CustomerId";
        sqlConnection.Execute(sqlQuery, new {customerId});

    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

public ActionResult Delete(int id, FormCollection collection)
    var customerEntities = new CustomerDB();
    return RedirectToAction("Index");

    return View();
Now It’s time to add delete view. So I have added strongly typed view like following.


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.


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


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


So that’s it. Its very easy.. Hope you liked it.. Stay tuned for more..

Shout it
kick it on


Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Search This Blog
About Me
My Photo

Full Stack Developer,Blogger,Microsoft Mvp, Mentor, Life long learner, 10+ Years of Experience for JavaScript, web and Microsoft technologies. ASP.NET MVC,Web Forms,C#,AngularJs,Golang
Mvp profile
Subscribe to my blog

Enter your email address:

Follow us on facebook
Blog Archive
Total Pageviews