User Id :    Password :      New Member   Forgot Password
 
Delete with Dapper ORM and ASP.NET MVC 3
Description Delete with Dapper ORM and ASP.NET MVC 3   No. of Views     1312
  Rating     0
Author Jalpesh Vadgama   Posted On     04 Jun 2011
Tags ASP.NET,ASP.NET-MVC    

Sample Code   Download Code

 

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)
{
 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 {customerId});
         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.




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..
 

kick it on DotNetKicks.com

About Author

About Author I am jalpesh vadgama an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 5 years of experience in .NET Technology.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible. Please also visit my blog at http://jalpesh.blogspot.com/ Jalpesh Vadgama
No Photo
 
Country India
Company N/A
Home Page http://jalpesh.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

     
Write your comment here.  
Comment
Verification Code   
  
    
 
Section sponsored by