Create Update Read Delete Operation In MVC With ADO.NET

Introduction

 
This article will explain how to CURD (Create, Update, Read, and Delete) records from a database without using Entity Framework. I am going to use ADO.NET for this article.
 
Step 1
 
Open the SQL Server with a version of your choice and create a database table and related stored procedure.
  1. CREATE TABLE [dbo].[Employee](  
  2.     [Id] [int] IDENTITY(1,1) NOT NULL,  
  3.     [FirstName] [nvarchar](50) NULL,  
  4.     [LastName] [nvarchar](50) NULL,  
  5.     [Gender] [char](10) NULL,  
  6.     [Age] [intNULL,  
  7.     [Position] [nvarchar](50) NULL,  
  8.     [Office] [nvarchar](50) NULL,  
  9.     [Salary] [intNULL,  
  10.  CONSTRAINT [PK__Employee__3214EC07BA0DD227] PRIMARY KEY CLUSTERED   
  11. (  
  12.     [Id] ASC  
  13. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  14. ON [PRIMARY]  
  15.   
  16. GO  
  17.   
  18. Create procedure [dbo].[spGetEmployees]  
  19. as   
  20. begin  
  21. SELECTFROM Employee  
  22. end  
  23.   
  24. Create procedure [dbo].[ spInsertNew]  
  25. (  
  26. @FirstName nvarchar(50),  
  27. @LastName nvarchar(50),  
  28. @Gender char(10),  
  29. @Age int,  
  30. @Position nvarchar(50),  
  31. @Office nvarchar(50),  
  32. @Salary int  
  33. )  
  34. as   
  35. begin  
  36.     insert into Employee(FirstName,LastName,Gender,Age,Position,Office,Salary)  
  37.     values(@FirstName,@LastName,@Gender,@Age,@Position,@Office,@Salary)  
  38. end  
  39.   
  40.   
  41.   
  42. Create procedure [dbo].[spUpdateRecord]  
  43. (  
  44. @Id int,  
  45. @FirstName nvarchar(50),  
  46. @LastName nvarchar(50),  
  47. @Gender char(10),  
  48. @Age int,  
  49. @Position nvarchar(50),  
  50. @Office nvarchar(50),  
  51. @Salary int  
  52. )  
  53. as  
  54. begin  
  55. update Employee  
  56. set FirstName=@FirstName,LastName=@LastName,Gender=@Gender,Age=@Age,  
  57. Position=@Position,Office=@Position,  
  58. Salary=@Salary where Id=@Id  
  59. end  
  60.   
  61. Create procedure [dbo].[spDeleteRecord]  
  62. (  
  63. @Id int  
  64. )  
  65. as  
  66. begin  
  67.     delete from Employee where Id=@Id  
  68. end  
Step 2
 
Open Visual Studio with a version of your choice and create a new project with an appropriate name.
 
Create Update Read Delete Operation In MVC With ADO.NET 
 
Step 3
 
Choose an "Empty" template, check MVC under "Add folders & core references", and click "Create".
 
Create Update Read Delete Operation In MVC With ADO.NET 
 
Step 4
 
Right-click on the Models folder, select “Add”. Under “Add”, a "Choose class" window will appear. From that window, select Visual C# and give a class name as “Employee”. Then, click "Add".
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Web;  
  6.   
  7. namespace MvcCURDWithoutEntityFramework_Demo.Models  
  8. {  
  9.     public class Employee  
  10.     {  
  11.         [Key]  
  12.         public int Id { getset; }  
  13.   
  14.         [Required(ErrorMessage ="Enter first name")]  
  15.         [Display(Name ="First Name")]  
  16.         public string FirstName { getset; }  
  17.   
  18.         [Required(ErrorMessage = "Enter last name")]  
  19.         [Display(Name = "Last Name")]  
  20.         public string LastName { getset; }  
  21.   
  22.         [Required(ErrorMessage = "Choose Gender")]  
  23.         public string Gender { getset; }  
  24.   
  25.         [Required(ErrorMessage = "Enter your age")]  
  26.         public int Age { getset; }  
  27.   
  28.         [Required(ErrorMessage = "Enter your position")]  
  29.         public string Position { getset; }  
  30.   
  31.         [Required(ErrorMessage = "Enter your office")]  
  32.         public string Office { getset; }  
  33.   
  34.         [Required(ErrorMessage = "Enter your salary")]  
  35.         public int Salary { getset; }  
  36.     }  
  37. }  
Step 5
 
Similarly, add another class in Models folder named as “EmployeeContext”. This class will be a data access layer.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6. using System.Linq;  
  7. using System.Web;  
  8.   
  9. namespace MvcCURDWithoutEntityFramework_Demo.Models  
  10. {  
  11.     public class EmployeeContext  
  12.     {  
  13.         private readonly string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
  14.         public List<Employee> GetEmployees()  
  15.         {  
  16.             List<Employee> employees = new List<Employee>();            
  17.             using (SqlConnection con = new SqlConnection(CS))  
  18.             {  
  19.                 SqlCommand cmd = new SqlCommand("spGetEmployees", con);  
  20.                 cmd.CommandType = CommandType.StoredProcedure;  
  21.                 con.Open();  
  22.                 SqlDataReader rdr = cmd.ExecuteReader();  
  23.                 while (rdr.Read())  
  24.                 {  
  25.                     var employee = new Employee();  
  26.                     employee.Id = Convert.ToInt32(rdr["Id"]);  
  27.                     employee.FirstName = rdr["FirstName"].ToString();  
  28.                     employee.LastName = rdr["LastName"].ToString();  
  29.                     employee.Gender = rdr["Gender"].ToString();  
  30.                     employee.Age = Convert.ToInt32(rdr["Age"]);  
  31.                     employee.Position = rdr["Position"].ToString();  
  32.                     employee.Office = rdr["Office"].ToString();  
  33.                     employee.Salary = Convert.ToInt32(rdr["Salary"]);  
  34.                     employees.Add(employee);  
  35.                 }  
  36.             }  
  37.             return (employees);  
  38.         }  
  39.         public bool AddNewEmployee(Employee employee)  
  40.         {  
  41.             using (SqlConnection con = new SqlConnection(CS))  
  42.             {  
  43.                 var cmd = new SqlCommand("spInsertNew", con);  
  44.                 con.Open();  
  45.                 cmd.CommandType = CommandType.StoredProcedure;  
  46.                 cmd.Parameters.AddWithValue("@FirstName", employee.FirstName);  
  47.                 cmd.Parameters.AddWithValue("@LastName", employee.LastName);  
  48.                 cmd.Parameters.AddWithValue("@Gender", employee.Gender);  
  49.                 cmd.Parameters.AddWithValue("@Age", employee.Age);  
  50.                 cmd.Parameters.AddWithValue("@Position", employee.Position);  
  51.                 cmd.Parameters.AddWithValue("@Office", employee.Office);  
  52.                 cmd.Parameters.AddWithValue("@Salary", employee.Salary);  
  53.                 int i = cmd.ExecuteNonQuery();  
  54.                 if (i >= 1)  
  55.                     return true;  
  56.                 else  
  57.                     return false;  
  58.             }  
  59.   
  60.         }  
  61.         public bool UpdateEmployee(Employee employee)  
  62.         {  
  63.             using (SqlConnection con = new SqlConnection(CS))  
  64.             {  
  65.                 var cmd = new SqlCommand("spUpdateRecord", con);  
  66.                 cmd.CommandType = CommandType.StoredProcedure;  
  67.                 con.Open();  
  68.                 cmd.Parameters.AddWithValue("@Id", employee.Id);  
  69.                 cmd.Parameters.AddWithValue("@FirstName", employee.FirstName);  
  70.                 cmd.Parameters.AddWithValue("@LastName", employee.LastName);  
  71.                 cmd.Parameters.AddWithValue("@Gender", employee.Gender);  
  72.                 cmd.Parameters.AddWithValue("@Age", employee.Age);  
  73.                 cmd.Parameters.AddWithValue("@Position", employee.Position);  
  74.                 cmd.Parameters.AddWithValue("@Office", employee.Office);  
  75.                 cmd.Parameters.AddWithValue("@Salary", employee.Salary);  
  76.                 int i = cmd.ExecuteNonQuery();  
  77.                 if (i >= 1)  
  78.                     return true;  
  79.                 else  
  80.                     return false;  
  81.             }  
  82.   
  83.   
  84.         }  
  85.         public bool DeleteEmployee(Employee employee)  
  86.         {  
  87.             using (SqlConnection con = new SqlConnection(CS))  
  88.             {  
  89.                 var cmd = new SqlCommand("spDeleteRecord", con);  
  90.                 cmd.CommandType = CommandType.StoredProcedure;  
  91.                 con.Open();  
  92.                 cmd.Parameters.AddWithValue("@Id", employee.Id);  
  93.                 int i = cmd.ExecuteNonQuery();  
  94.                 if (i >= 1)  
  95.                     return true;  
  96.                 else  
  97.                     return false;  
  98.             }  
  99.         }  
  100.     }  
  101. }  
Step 6
 
Open the WebConfig file and add a database connection string.
  1. <connectionStrings>  
  2.   <add name="DBCS" connectionString="data source=farhan\sql2014; database=SampleDB; integrated security=true;" providerName="System.SqlClient"/>  
  3. </connectionStrings>  
Step 7
 
Now, right-click on the Controllers folder and add a controller.
 
Create Update Read Delete Operation In MVC With ADO.NET 
 
A window will appear. Choose MVC5 Controller-Empty and click "Add".
 
Create Update Read Delete Operation In MVC With ADO.NET 
 
After clicking on "Add", another window will appear with DefaultController. Change the name to HomeController and click "Add". The HomeController will be added under the Controllers folder. Don’t change the Controller suffix for all controllers, change only the highlight, and instead of Default, just change Home.
 
Create Update Read Delete Operation In MVC With ADO.NET 
 
Controller Class code
  1. using MvcCURDWithoutEntityFramework_Demo.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Web;  
  7. using System.Web.Mvc;  
  8.   
  9. namespace MvcCURDWithoutEntityFramework_Demo.Controllers  
  10. {  
  11.     public class HomeController : Controller  
  12.     {  
  13.         private EmployeeContext db = new EmployeeContext();  
  14.   
  15.         public ActionResult Index()  
  16.         {  
  17.             var employee = db.GetEmployees();  
  18.             return View(employee);  
  19.         }  
  20.         public ActionResult Details(int id)  
  21.         {  
  22.             var employee = db.GetEmployees().Find(e=>e.Id==id);  
  23.             return View(employee);  
  24.         }  
  25.         public ActionResult Create()  
  26.         {  
  27.             return View();  
  28.         }  
  29.         [HttpPost]  
  30.         [ValidateAntiForgeryToken]  
  31.         public ActionResult Create(Employee employee)  
  32.         {  
  33.             if (ModelState.IsValid)  
  34.             {  
  35.                 db.AddNewEmployee(employee);  
  36.                 return RedirectToAction("Index");  
  37.             }  
  38.             return View();  
  39.         }  
  40.   
  41.         public ActionResult Edit(int? id)  
  42.         {  
  43.             if (id == null)  
  44.             {  
  45.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  46.             }  
  47.             Employee employee = db.GetEmployees().Find(e=>e.Id==id);  
  48.             if (employee == null)  
  49.             {  
  50.                 return HttpNotFound();  
  51.             }  
  52.             return View(employee);  
  53.         }  
  54.         [HttpPost]  
  55.         [ValidateAntiForgeryToken]  
  56.         public ActionResult Edit(Employee employee)  
  57.         {  
  58.             if (ModelState.IsValid)  
  59.             {  
  60.                 db.UpdateEmployee(employee);  
  61.                 return RedirectToAction("Index");  
  62.             }  
  63.             return View(employee);  
  64.         }  
  65.         public ActionResult Delete(int? id)  
  66.         {  
  67.             if (id==null)  
  68.             {  
  69.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  70.             }  
  71.             var employee = db.GetEmployees().Find(e=>e.Id==id);  
  72.             if (employee==null)  
  73.             {  
  74.                 return HttpNotFound();  
  75.             }  
  76.             return View(employee);  
  77.         }  
  78.   
  79.         [HttpPost, ActionName("Delete")]  
  80.         [ValidateAntiForgeryToken]  
  81.         public ActionResult DeleteConfirmed(int id)  
  82.         {  
  83.             var employee = db.GetEmployees().Find(e=>e.Id==id);  
  84.             db.DeleteEmployee(employee);  
  85.             return RedirectToAction("Index");  
  86.         }  
  87.     }  
  88. }  
Step 8
 
Right-click on the index of ActionResult, choose “Add View”, and click on it. Now, you will get another window that has a default view name as ActionResult name. Check the "Use a lay page" option and click on “Add”.
 
Create Update Read Delete Operation In MVC With ADO.NET 
 
Index View
  1. @model IEnumerable<MvcCURDWithoutEntityFramework_Demo.Models.Employee>  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5. }  
  6.   
  7. <h2 class="text-center text-uppercase">List of employee</h2>  
  8. <div style="margin-bottom:10px;">  
  9.     @Html.ActionLink("Create New""Create""Home"""new { @class = "btn btn-primary rounded-0" })  
  10. </div>  
  11. <table class="table table-bordered">  
  12.     <thead class="thead-dark">  
  13.         <tr>  
  14.             <th>@Html.DisplayNameFor(m=>m.FirstName)</th>  
  15.             <th>@Html.DisplayNameFor(m=>m.LastName)</th>  
  16.             <th>@Html.DisplayNameFor(m=>m.Gender)</th>  
  17.             <th>@Html.DisplayNameFor(m=>m.Age)</th>  
  18.             <th>@Html.DisplayNameFor(m=>m.Position)</th>  
  19.             <th>@Html.DisplayNameFor(m=>m.Office)</th>  
  20.             <th>@Html.DisplayNameFor(m=>m.Salary)</th>  
  21.             <th>Action(s)</th>  
  22.         </tr>  
  23.     </thead>  
  24.     <tbody>  
  25.         @foreach (var emp in Model)  
  26.         {  
  27.             <tr>  
  28.                 <td>@emp.FirstName</td>  
  29.                 <td>@emp.LastName</td>  
  30.                 <td>@emp.Gender</td>  
  31.                 <td>@emp.Age</td>  
  32.                 <td>@emp.Position</td>  
  33.                 <td>@emp.Office</td>  
  34.                 <td>@emp.Salary</td>  
  35.                 <td>  
  36.                 @Html.ActionLink("Details","Details","Home",new {id=emp.Id},new { @class="btn btn-primary rounded-0"})  
  37.                 @Html.ActionLink("Edit","Edit","Home",new {id=emp.Id},new { @class="btn btn-info rounded-0"})  
  38.                 @Html.ActionLink("Delete","Delete","Home",new {id=emp.Id},new { @class="btn btn-danger rounded-0"})  
  39.                 </td>  
  40.             </tr>  
  41.         }  
  42.     </tbody>  
  43. </table>  
Output
 
Create Update Read Delete Operation In MVC With ADO.NET 
 
Step 9
 
Now similarly, right-click on Details of ActionResult and choose “Add View” and click on it. Now, you will get another window that has a default view named as ActionResult name. Check the "Use a lay page" option and click on “Add”.
 
Details View
  1. @model MvcCURDWithoutEntityFramework_Demo.Models.Employee  
  2.   
  3. @{  
  4.     ViewBag.Title = "Details";  
  5. }  
  6.   
  7.   
  8. <div class="card">  
  9.     <div class="card-header">  
  10.         <h3>Employee Details</h3>  
  11.     </div>  
  12.     <div class="card-body">  
  13.         <dl class="row">  
  14.             <dt class="col-md-3">@Html.DisplayNameFor(m => m.FirstName)</dt>  
  15.             <dd class="col-md-3">@Html.DisplayFor(m => m.FirstName)</dd>  
  16.             <dt class="col-md-3">@Html.DisplayNameFor(m => m.LastName)</dt>  
  17.             <dd class="col-md-3">@Html.DisplayFor(m => m.LastName)</dd>  
  18.         </dl>  
  19.         <dl class="row">  
  20.             <dt class="col-md-3">@Html.DisplayNameFor(m => m.Gender)</dt>  
  21.             <dd class="col-md-3">@Html.DisplayFor(m => m.Gender)</dd>  
  22.             <dt class="col-md-3">@Html.DisplayNameFor(m => m.Age)</dt>  
  23.             <dd class="col-md-3">@Html.DisplayFor(m => m.Age)</dd>  
  24.         </dl>  
  25.         <dl class="row">  
  26.             <dt class="col-md-2">@Html.DisplayNameFor(m => m.Position)</dt>  
  27.             <dd class="col-md-2">@Html.DisplayFor(m => m.Position)</dd>  
  28.             <dt class="col-md-2">@Html.DisplayNameFor(m => m.Office)</dt>  
  29.             <dd class="col-md-2">@Html.DisplayFor(m => m.Office)</dd>  
  30.             <dt class="col-md-2">@Html.DisplayNameFor(m => m.Salary)</dt>  
  31.             <dd class="col-md-2">@Html.DisplayFor(m => m.Salary)</dd>  
  32.         </dl>  
  33.         <div>  
  34.             @Html.ActionLink("Back to list""Index""Home"new { @class = "btn btn-primary rounded-0" })  
  35.             @Html.ActionLink("Edit""Edit""Home"new { id = Model.Id }, new { @class = "btn btn-info rounded-0" })  
  36.         </div>  
  37.     </div>  
  38. </div>  
Step 10
 
Click on "Create ActionResult", choose “Add View”, and click on it. Similarly, you will get another window with default view name as ActionResult name. Check the "Use a lay page" option and click on “Add”.
 
Create Update Read Delete Operation In MVC With ADO.NET 
 
Create View
  1. @model MvcCURDWithoutEntityFramework_Demo.Models.Employee  
  2.   
  3. @{  
  4.     ViewBag.Title = "Create";  
  5. }  
  6.   
  7. <div class="card">  
  8.     <div class="card-header bg-dark text-white">  
  9.         <h3>Create New</h3>  
  10.     </div>  
  11.     <div class="card-body">  
  12.         @using (Html.BeginForm())  
  13.         {  
  14.             @Html.AntiForgeryToken()  
  15.             <div class="row">  
  16.                 <div class="col-md-6">  
  17.                     <div class="form-group">  
  18.                         @Html.LabelFor(m => m.FirstName)  
  19.                         @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control" })  
  20.                         @Html.ValidationMessageFor(m => m.FirstName)  
  21.                     </div>  
  22.                 </div>  
  23.                 <div class="col-md-6">  
  24.                     <div class="form-group">  
  25.                         @Html.LabelFor(m => m.LastName)  
  26.                         @Html.TextBoxFor(m => m.LastName, new { @class = "form-control" })  
  27.                         @Html.ValidationMessageFor(m => m.LastName)  
  28.                     </div>  
  29.                 </div>  
  30.             </div>  
  31.             <div class="row">  
  32.                 <div class="col-md-6">  
  33.                     <div class="form-group">  
  34.                         @Html.LabelFor(m => m.Gender)  
  35.                         @Html.DropDownList("Gender",new List<SelectListItem> {  
  36.                             new SelectListItem { Text="Male",Value="Male" },  
  37.                             new SelectListItem { Text="Female", Value="Female" }  
  38.                         },"Choose Gender",new { @class="form-control"})  
  39.                         @Html.ValidationMessageFor(m => m.Gender)  
  40.                     </div>  
  41.                 </div>  
  42.                 <div class="col-md-6">  
  43.                     <div class="form-group">  
  44.                         @Html.LabelFor(m => m.Age)  
  45.                         @Html.TextBoxFor(m => m.Age, new { @class = "form-control" })  
  46.                         @Html.ValidationMessageFor(m => m.Age)  
  47.                     </div>  
  48.                 </div>  
  49.             </div>  
  50.             <div class="row">  
  51.                 <div class="col-md-4">  
  52.                     <div class="form-group">  
  53.                         @Html.LabelFor(m => m.Position)  
  54.                         @Html.TextBoxFor(m => m.Position, new { @class = "form-control" })  
  55.                         @Html.ValidationMessageFor(m => m.Position)  
  56.                     </div>  
  57.                 </div>  
  58.                 <div class="col-md-4">  
  59.                     <div class="form-group">  
  60.                         @Html.LabelFor(m => m.Office)  
  61.                         @Html.TextBoxFor(m => m.Office, new { @class = "form-control" })  
  62.                         @Html.ValidationMessageFor(m => m.Office)  
  63.                     </div>  
  64.                 </div>  
  65.                 <div class="col-md-4">  
  66.                     <div class="form-group">  
  67.                         @Html.LabelFor(m => m.Salary)  
  68.                         @Html.TextBoxFor(m => m.Salary, new { @class = "form-control" })  
  69.                         @Html.ValidationMessageFor(m => m.Salary)  
  70.                     </div>  
  71.                 </div>  
  72.             </div>  
  73.             <div class="form-group">  
  74.                 <button type="submit" class="btn btn-primary rounded-0">Submit</button>  
  75.             </div>  
  76.         }  
  77.     </div>  
  78. </div>  
Output
 
Create Update Read Delete Operation In MVC With ADO.NET 
 
Step 11
 
Right-click on "Edit" of ActionResult, choose “Add View”, and click on it. Now, you will get another window with a default view named as ActionResult name. Check "Use a lay page" and click on “Add”.
 
Edit View
  1. @model MvcCURDWithoutEntityFramework_Demo.Models.Employee  
  2.   
  3. @{  
  4.     ViewBag.Title = "Edit";  
  5. }  
  6.   
  7. <div class="card">  
  8.     <div class="card-header">  
  9.         <h3>Edit Employee</h3>  
  10.     </div>  
  11.     <div class="card-body">  
  12.         @using (Html.BeginForm())  
  13.         {  
  14.             @Html.AntiForgeryToken()  
  15.             @Html.HiddenFor(m=>m.Id)  
  16.             <div class="row">  
  17.                 <div class="col-md-6">  
  18.                     <div class="form-group">  
  19.                         @Html.LabelFor(m=>m.FirstName)  
  20.                         @Html.EditorFor(m=>m.FirstName,new { htmlAttributes = new { @class = "form-control" } })  
  21.                         @Html.ValidationMessageFor(m=>m.FirstName)  
  22.                     </div>  
  23.                 </div>  
  24.                 <div class="col-md-6">  
  25.                     <div class="form-group">  
  26.                         @Html.LabelFor(m => m.LastName)  
  27.                         @Html.EditorFor(m => m.LastName, new { htmlAttributes = new { @class = "form-control" } })  
  28.                         @Html.ValidationMessageFor(m => m.LastName)  
  29.                     </div>  
  30.                 </div>  
  31.             </div>  
  32.             <div class="row">  
  33.                 <div class="col-md-6">  
  34.                     <div class="form-group">  
  35.                         @Html.LabelFor(m => m.Gender)  
  36.                         @Html.DropDownList("Gender",new List<SelectListItem> {  
  37.                             new SelectListItem { Text="Male",Value="Male"},  
  38.                             new SelectListItem { Text="Female",Value="Female"}  
  39.                     },"Choose Gender",new { @class="form-control"})  
  40.                         @Html.ValidationMessageFor(m => m.LastName)  
  41.                     </div>  
  42.                 </div>  
  43.                 <div class="col-md-6">  
  44.                     <div class="form-group">  
  45.                         @Html.LabelFor(m => m.Age)  
  46.                         @Html.EditorFor(m => m.Age, new { htmlAttributes = new { @class = "form-control" } })  
  47.                         @Html.ValidationMessageFor(m => m.Age)  
  48.                     </div>  
  49.                 </div>  
  50.             </div>  
  51.             <div class="row">  
  52.                 <div class="col-md-4">  
  53.                     <div class="form-group">  
  54.                         @Html.LabelFor(m => m.Position)  
  55.                         @Html.EditorFor(m => m.Position, new { htmlAttributes = new { @class = "form-control" } })  
  56.                         @Html.ValidationMessageFor(m => m.Position)  
  57.                     </div>  
  58.                 </div>  
  59.                 <div class="col-md-4">  
  60.                     <div class="form-group">  
  61.                         @Html.LabelFor(m => m.Office)  
  62.                         @Html.EditorFor(m => m.Office, new { htmlAttributes = new { @class = "form-control" } })  
  63.                         @Html.ValidationMessageFor(m => m.Office)  
  64.                     </div>  
  65.                 </div>  
  66.                 <div class="col-md-4">  
  67.                     <div class="form-group">  
  68.                         @Html.LabelFor(m => m.Salary)  
  69.                         @Html.EditorFor(m => m.Salary, new { htmlAttributes = new { @class = "form-control" } })  
  70.                         @Html.ValidationMessageFor(m => m.Salary)  
  71.                     </div>  
  72.                 </div>  
  73.             </div>  
  74.             <div class="form-group">  
  75.                 <button type="submit" class="btn btn-primary rounded-0">Update</button>  
  76.             </div>  
  77.         }  
  78.     </div>  
  79. </div>  
Create Update Read Delete Operation In MVC With ADO.NET 
 
Step 12
 
Right-click on Delete ActionResult, choose “Add View”, and click on it. Now, you will get another window that has a default view named as ActionResult name. Check the "Use a lay page" option and click on “Add”.
 
Delete View
  1. @model MvcCURDWithoutEntityFramework_Demo.Models.Employee  
  2.   
  3. @{  
  4.     ViewBag.Title = "Delete";  
  5. }  
  6.   
  7.     <h3 class="alert alert-danger text-center text-uppercase">Are you sure you want to delete this?</h3>  
  8.   
  9. <div class="card">  
  10.     <div class="card-header">  
  11.         <h3 class="text-center text-uppercase">Delete Employee Record</h3>  
  12.     </div>  
  13.     <div class="card-body">  
  14.         <dl class="row">  
  15.             <dt class="col-md-3">@Html.DisplayNameFor(m => m.FirstName)</dt>  
  16.             <dd class="col-md-3">@Html.DisplayFor(m => m.FirstName)</dd>  
  17.             <dt class="col-md-3">@Html.DisplayNameFor(m => m.LastName)</dt>  
  18.             <dd class="col-md-3">@Html.DisplayFor(m => m.LastName)</dd>  
  19.         </dl>  
  20.         <dl class="row">  
  21.             <dt class="col-md-3">@Html.DisplayNameFor(m => m.Gender)</dt>  
  22.             <dd class="col-md-3">@Html.DisplayFor(m => m.Gender)</dd>  
  23.             <dt class="col-md-3">@Html.DisplayNameFor(m => m.Age)</dt>  
  24.             <dd class="col-md-3">@Html.DisplayFor(m => m.Age)</dd>  
  25.         </dl>  
  26.         <dl class="row">  
  27.             <dt class="col-md-2">@Html.DisplayNameFor(m => m.Position)</dt>  
  28.             <dd class="col-md-2">@Html.DisplayFor(m => m.Position)</dd>  
  29.             <dt class="col-md-2">@Html.DisplayNameFor(m => m.Office)</dt>  
  30.             <dd class="col-md-2">@Html.DisplayFor(m => m.Office)</dd>  
  31.             <dt class="col-md-2">@Html.DisplayNameFor(m => m.Salary)</dt>  
  32.             <dd class="col-md-2">@Html.DisplayFor(m => m.Salary)</dd>  
  33.         </dl>  
  34.         @using (Html.BeginForm())  
  35.         {  
  36.             @Html.AntiForgeryToken()  
  37.             <div class="form-group">  
  38.                 @Html.ActionLink("Back to List""Index"""new { @class = "btn btn-primary rounded-0" })  
  39.                 <button type="submit" class="btn btn-danger rounded-0">Delete</button>  
  40.             </div>  
  41.         }  
  42.     </div>  
  43. </div>  
Create Update Read Delete Operation In MVC With ADO.NET 
 
Step 13
 
Build your project and run by pressing ctrl+F5
 

Summary

 
In this article, I have explained how to perform CURD (Create, Update, Read and Delete) operations using ADO.NET where I have created a data access layer for database interaction like retrieving data, inserting data, updating existing data, and deleting data from the SQL database table.


Similar Articles