Introduction

Here, we are going to create a web application using ASP.NET Core MVC and ADO.NET. We will be creating a simple student record management system and performing CRUD operations on it.

Prerequisites

Creating the Table and Stored Procedures

We will be using a DB table to store all the records of the students.

First of all, we will create a database named "StudentManagement”.

  1. CREATE DATABASE StudentManagement

Then, we will create a table named “Student”.

  1. Create table Student(
  2. Id int IDENTITY(1,1) NOT NULL,
  3. FirstName varchar(50) NOT NULL,
  4. LastName varchar(50) NOT NULL,
  5. Email varchar(30) NOT NULL,
  6. Mobile varchar(20) NOT NULL,
  7. Address varchar(220) NULL,
  8. )

Now, we will create stored procedures to add, delete, update, and get student data.

To Insert a Student Record

  1. Create procedure spAddStudent
  2. (
  3. @FirstName VARCHAR(50),
  4. @LastName VARCHAR(50),
  5. @Email VARCHAR(30),
  6. @Mobile VARCHAR(20),
  7. @Address VARCHAR(220)
  8. )
  9. as
  10. Begin
  11. Insert into Student (FirstName,LastName,Email, Mobile,Address)
  12. Values (@FirstName,@LastName,@Email, @Mobile,@Address)
  13. End

To Update a Student Record

  1. Create procedure spUpdateStudent
  2. (
  3. @Id INTEGER ,
  4. @FirstName VARCHAR(50),
  5. @LastName VARCHAR(50),
  6. @Email VARCHAR(30),
  7. @Mobile VARCHAR(20),
  8. @Address VARCHAR(220)
  9. )
  10. as
  11. begin
  12. Update Student
  13. set FirstName=@FirstName,
  14. LastName=@LastName,
  15. Email=@Email,
  16. Mobile=@Mobile,
  17. Address=@Address
  18. where Id=@Id
  19. End

To Delete a Student Record

  1. Create procedure spDeleteStudent
  2. (
  3. @Id int
  4. )
  5. as
  6. begin
  7. Delete from Student where Id=@Id
  8. End

To View all Student Records

  1. Create procedure spGetAllStudent
  2. as
  3. Begin
  4. select *
  5. from Student
  6. order by Id
  7. End

Our database part has been completed.

Create the ASP.NET MVC Web Application

Now, we are going to create an ASP.NET MVC Web Application. The project name is “StudentRecordManagementSystem”.

CRUD Operations Using ASP.NET Core And ADO.NET

Click OK. The below window will appear on the screen.

CRUD Operations Using ASP.NET Core And ADO.NET

Click OK. The solution creation is done with the loading of all needed files. Given below is the picture of the solution structure.

CRUD Operations Using ASP.NET Core And ADO.NET

What is MVC

  1. Model
    Classes that represent the data of the solution
  1. View
    Simple word view means UI (User Interface) dynamically generates HTML responses.
  1. Controller
    A Controller is a link between User and the System. It handles incoming browser requests and after processing it using model data or specific task, returns a response to the browser.

Create a folder named “Utility” in the project. Now, we will create a class named “ConnectionString” within the Utility folder.

  1. public static class ConnectionString
  2. {
  3. private static string cName = "Data Source=.; Initial Catalog=StudentManagement;User ID=sa;Password=123";
  4. public static string CName { get => cName;
  5. }
  6. }

After that, we will create a class named “Student” within model folder.

  1. public class Student
  2. {
  3. public int Id { set; get; }
  4. [Required]
  5. public string FirstName { set; get; }
  6. [Required]
  7. public string LastName { set; get; }
  8. [Required]
  9. public string Email { set; get; }
  10. [Required]
  11. public string Mobile { set; get; }
  12. public string Address { set; get; }
  13. }

We will Rebuild our solution and create a “StudentController” within Controller folder. Right-Add-Controller-Select MVC Controller with read/write actions and click add.

CRUD Operations Using ASP.NET Core And ADO.NET

Another window will appear on screen

CRUD Operations Using ASP.NET Core And ADO.NET

Our StudentController has been created.

  1. public class StudentController : Controller
  2. {
  3. // GET: Student
  4. public ActionResult Index()
  5. {
  6. return View();
  7. }
  8. // GET: Student/Details/5
  9. public ActionResult Details(int id)
  10. {
  11. return View();
  12. }
  13. // GET: Student/Create
  14. public ActionResult Create()
  15. {
  16. return View();
  17. }
  18. // POST: Student/Create
  19. [HttpPost]
  20. [ValidateAntiForgeryToken]
  21. public ActionResult Create(IFormCollection collection)
  22. {
  23. try
  24. {
  25. // TODO: Add insert logic here
  26. return RedirectToAction(nameof(Index));
  27. }
  28. catch
  29. {
  30. return View();
  31. }
  32. }
  33. // GET: Student/Edit/5
  34. public ActionResult Edit(int id)
  35. {
  36. return View();
  37. }
  38. // POST: Student/Edit/5
  39. [HttpPost]
  40. [ValidateAntiForgeryToken]
  41. public ActionResult Edit(int id, IFormCollection collection)
  42. {
  43. try
  44. {
  45. // TODO: Add update logic here
  46. return RedirectToAction(nameof(Index));
  47. }
  48. catch
  49. {
  50. return View();
  51. }
  52. }
  53. // GET: Student/Delete/5
  54. public ActionResult Delete(int id)
  55. {
  56. return View();
  57. }
  58. // POST: Student/Delete/5
  59. [HttpPost]
  60. [ValidateAntiForgeryToken]
  61. public ActionResult Delete(int id, IFormCollection collection)
  62. {
  63. try
  64. {
  65. // TODO: Add delete logic here
  66. return RedirectToAction(nameof(Index));
  67. }
  68. catch
  69. {
  70. return View();
  71. }
  72. }
  73. }

We have to work with the database so we will create a data access layer class within model folder named “StudentDataAccessLayer”

  1. public class StudentDataAccessLayer
  2. {
  3. string connectionString = ConnectionString.CName;
  4. public IEnumerable<Student> GetAllStudent()
  5. {
  6. List<Student> lstStudent = new List<Student>();
  7. using (SqlConnection con = new SqlConnection(connectionString))
  8. {
  9. SqlCommand cmd = new SqlCommand("spGetAllStudent", con);
  10. cmd.CommandType = CommandType.StoredProcedure;
  11. con.Open();
  12. SqlDataReader rdr = cmd.ExecuteReader();
  13. while (rdr.Read())
  14. {
  15. Student student = new Student();
  16. student.Id = Convert.ToInt32(rdr["Id"]);
  17. student.FirstName = rdr["FirstName"].ToString();
  18. student.LastName = rdr["LastName"].ToString();
  19. student.Email = rdr["Email"].ToString();
  20. student.Mobile = rdr["Mobile"].ToString();
  21. student.Address = rdr["Address"].ToString();
  22. lstStudent.Add(student);
  23. }
  24. con.Close();
  25. }
  26. return lstStudent;
  27. }
  28. public void AddStudent(Student student)
  29. {
  30. using (SqlConnection con = new SqlConnection(connectionString))
  31. {
  32. SqlCommand cmd = new SqlCommand("spAddStudent", con);
  33. cmd.CommandType = CommandType.StoredProcedure;
  34. cmd.Parameters.AddWithValue("@FirstName", student.FirstName);
  35. cmd.Parameters.AddWithValue("@LastName", student.LastName);
  36. cmd.Parameters.AddWithValue("@Email", student.Email);
  37. cmd.Parameters.AddWithValue("@Mobile", student.Mobile);
  38. cmd.Parameters.AddWithValue("@Address", student.Address);
  39. con.Open();
  40. cmd.ExecuteNonQuery();
  41. con.Close();
  42. }
  43. }
  44. public void UpdateStudent(Student student)
  45. {
  46. using (SqlConnection con = new SqlConnection(connectionString))
  47. {
  48. SqlCommand cmd = new SqlCommand("spUpdateStudent", con);
  49. cmd.CommandType = CommandType.StoredProcedure;
  50. cmd.Parameters.AddWithValue("@Id", student.Id);
  51. cmd.Parameters.AddWithValue("@FirstName", student.FirstName);
  52. cmd.Parameters.AddWithValue("@LastName", student.LastName);
  53. cmd.Parameters.AddWithValue("@Email", student.Email);
  54. cmd.Parameters.AddWithValue("@Mobile", student.Mobile);
  55. cmd.Parameters.AddWithValue("@Address", student.Address);
  56. con.Open();
  57. cmd.ExecuteNonQuery();
  58. con.Close();
  59. }
  60. }
  61. public Student GetStudentData(int? id)
  62. {
  63. Student student = new Student();
  64. using (SqlConnection con = new SqlConnection(connectionString))
  65. {
  66. string sqlQuery = "SELECT * FROM Student WHERE Id= " + id;
  67. SqlCommand cmd = new SqlCommand(sqlQuery, con);
  68. con.Open();
  69. SqlDataReader rdr = cmd.ExecuteReader();
  70. while (rdr.Read())
  71. {
  72. student.Id = Convert.ToInt32(rdr["Id"]);
  73. student.FirstName = rdr["FirstName"].ToString();
  74. student.LastName = rdr["LastName"].ToString();
  75. student.Email = rdr["Email"].ToString();
  76. student.Mobile = rdr["Mobile"].ToString();
  77. student.Address = rdr["Address"].ToString();
  78. }
  79. }
  80. return student;
  81. }
  82. public void DeleteStudent(int? id)
  83. {
  84. using (SqlConnection con = new SqlConnection(connectionString))
  85. {
  86. SqlCommand cmd = new SqlCommand("spDeleteStudent", con);
  87. cmd.CommandType = CommandType.StoredProcedure;
  88. cmd.Parameters.AddWithValue("@Id", id);
  89. con.Open();
  90. cmd.ExecuteNonQuery();
  91. con.Close();
  92. }
  93. }
  94. }

Create Action

Now we will work with Create Action within Student Controller. There are two Create Actions one is GET and another is POST. Now we will create a view for creating action.

Before creating a view we will create a constructor

  1. StudentDataAccessLayer studentDataAccessLayer = null;
  2. public StudentController()
  3. {
  4. studentDataAccessLayer = new StudentDataAccessLayer();
  5. }

Right click on create (GET) action then click add view; the below window will appear on the screen.

CRUD Operations Using ASP.NET Core And ADO.NET

Click add

  1. @model StudentRecordManagementSystem.Models.Student
  2. @{
  3. ViewData["Title"] = "Create";
  4. }
  5. <h2>Create</h2>
  6. <h4>Student</h4>
  7. <hr />
  8. <div class="row">
  9. <div class="col-md-4">
  10. <form asp-action="Create">
  11. <div asp-validation-summary="ModelOnly" class="text-danger"></div>
  12. <div class="form-group">
  13. <label asp-for="Id" class="control-label"></label>
  14. <input asp-for="Id" class="form-control" />
  15. <span asp-validation-for="Id" class="text-danger"></span>
  16. </div>
  17. <div class="form-group">
  18. <label asp-for="FirstName" class="control-label"></label>
  19. <input asp-for="FirstName" class="form-control" />
  20. <span asp-validation-for="FirstName" class="text-danger"></span>
  21. </div>
  22. <div class="form-group">
  23. <label asp-for="LastName" class="control-label"></label>
  24. <input asp-for="LastName" class="form-control" />
  25. <span asp-validation-for="LastName" class="text-danger"></span>
  26. </div>
  27. <div class="form-group">
  28. <label asp-for="Email" class="control-label"></label>
  29. <input asp-for="Email" class="form-control" />
  30. <span asp-validation-for="Email" class="text-danger"></span>
  31. </div>
  32. <div class="form-group">
  33. <label asp-for="Mobile" class="control-label"></label>
  34. <input asp-for="Mobile" class="form-control" />
  35. <span asp-validation-for="Mobile" class="text-danger"></span>
  36. </div>
  37. <div class="form-group">
  38. <label asp-for="Address" class="control-label"></label>
  39. <input asp-for="Address" class="form-control" />
  40. <span asp-validation-for="Address" class="text-danger"></span>
  41. </div>
  42. <div class="form-group">
  43. <input type="submit" value="Create" class="btn btn-default" />
  44. </div>
  45. </form>
  46. </div>
  47. </div>
  48. <div>
  49. <a asp-action="Index">Back to List</a>
  50. </div>
  51. @section Scripts {
  52. @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
  53. }

Now run our application,

CRUD Operations Using ASP.NET Core And ADO.NET

We will remove Id field from view. We have done auto increment of Id field in database.

Now we will work with Create (POST),

  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public ActionResult Create(Student student)
  4. {
  5. try
  6. {
  7. // TODO: Add insert logic here
  8. studentDataAccessLayer.AddStudent(student);
  9. return RedirectToAction(nameof(Index));
  10. }
  11. catch(Exception ex)
  12. {
  13. return View();
  14. }
  15. }

IndexAction

We have to call GetAllStudent method from StudentDataAccessLayer class for getting all students in Index action,

  1. public ActionResult Index()
  2. {
  3. IEnumerable<Student> students = studentDataAccessLayer.GetAllStudent();
  4. return View(students);
  5. }

Right click on Index action then click add view and the below window will appear on screen.

CRUD Operations Using ASP.NET Core And ADO.NET

Click add

  1. @model IEnumerable<StudentRecordManagementSystem.Models.Student>
  2. @{
  3. ViewData["Title"] = "Index";
  4. }
  5. <h2>Index</h2>
  6. <p>
  7. <a asp-action="Create">Create New</a>
  8. </p>
  9. <table class="table">
  10. <thead>
  11. <tr>
  12. <th>
  13. @Html.DisplayNameFor(model => model.Id)
  14. </th>
  15. <th>
  16. @Html.DisplayNameFor(model => model.FirstName)
  17. </th>
  18. <th>
  19. @Html.DisplayNameFor(model => model.LastName)
  20. </th>
  21. <th>
  22. @Html.DisplayNameFor(model => model.Email)
  23. </th>
  24. <th>
  25. @Html.DisplayNameFor(model => model.Mobile)
  26. </th>
  27. <th>
  28. @Html.DisplayNameFor(model => model.Address)
  29. </th>
  30. <th></th>
  31. </tr>
  32. </thead>
  33. <tbody>
  34. @foreach (var item in Model) {
  35. <tr>
  36. <td>
  37. @Html.DisplayFor(modelItem => item.Id)
  38. </td>
  39. <td>
  40. @Html.DisplayFor(modelItem => item.FirstName)
  41. </td>
  42. <td>
  43. @Html.DisplayFor(modelItem => item.LastName)
  44. </td>
  45. <td>
  46. @Html.DisplayFor(modelItem => item.Email)
  47. </td>
  48. <td>
  49. @Html.DisplayFor(modelItem => item.Mobile)
  50. </td>
  51. <td>
  52. @Html.DisplayFor(modelItem => item.Address)
  53. </td>
  54. <td>
  55. @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
  56. @Html.ActionLink("Details", "Details", new { id=item.Id }) |
  57. @Html.ActionLink("Delete", "Delete", new { id=item.Id })
  58. </td>
  59. </tr>
  60. }
  61. </tbody>
  62. </table>

Now we will save a student and see the student in list.

Save a student:

CRUD Operations Using ASP.NET Core And ADO.NET

Show student list:

CRUD Operations Using ASP.NET Core And ADO.NET

EditAction

Now we will work with Edit Action within Student Controller. There are two Edit Actions; one is GET and another is POST. Now we will create a view for creating action.

We have to call GetStudentData method from StudentDataAccessLayer class for getting student by Id.

  1. public ActionResult Edit(int id)
  2. {
  3. Student student = studentDataAccessLayer.GetStudentData(id);
  4. return View(student);
  5. }

Right click on Edit (GET) action then click add view; the below window will appear on screen.

CRUD Operations Using ASP.NET Core And ADO.NET

Click Add

  1. @model StudentRecordManagementSystem.Models.Student
  2. @{
  3. ViewData["Title"] = "Edit";
  4. }
  5. <h2>Edit</h2>
  6. <h4>Student</h4>
  7. <hr />
  8. <div class="row">
  9. <div class="col-md-4">
  10. <form asp-action="Edit">
  11. <div asp-validation-summary="ModelOnly" class="text-danger"></div>
  12. <div class="form-group">
  13. <label asp-for="Id" class="control-label"></label>
  14. <input asp-for="Id" class="form-control" readonly/>
  15. <span asp-validation-for="Id" class="text-danger"></span>
  16. </div>
  17. <div class="form-group">
  18. <label asp-for="FirstName" class="control-label"></label>
  19. <input asp-for="FirstName" class="form-control" />
  20. <span asp-validation-for="FirstName" class="text-danger"></span>
  21. </div>
  22. <div class="form-group">
  23. <label asp-for="LastName" class="control-label"></label>
  24. <input asp-for="LastName" class="form-control" />
  25. <span asp-validation-for="LastName" class="text-danger"></span>
  26. </div>
  27. <div class="form-group">
  28. <label asp-for="Email" class="control-label"></label>
  29. <input asp-for="Email" class="form-control" />
  30. <span asp-validation-for="Email" class="text-danger"></span>
  31. </div>
  32. <div class="form-group">
  33. <label asp-for="Mobile" class="control-label"></label>
  34. <input asp-for="Mobile" class="form-control" />
  35. <span asp-validation-for="Mobile" class="text-danger"></span>
  36. </div>
  37. <div class="form-group">
  38. <label asp-for="Address" class="control-label"></label>
  39. <input asp-for="Address" class="form-control" />
  40. <span asp-validation-for="Address" class="text-danger"></span>
  41. </div>
  42. <div class="form-group">
  43. <input type="submit" value="Update" class="btn btn-default" />
  44. </div>
  45. </form>
  46. </div>
  47. </div>
  48. <div>
  49. <a asp-action="Index">Back to List</a>
  50. </div>
  51. @section Scripts {
  52. @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
  53. }

Now we will work with Edit (POST):

  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public ActionResult Edit(Student student)
  4. {
  5. try
  6. {
  7. // TODO: Add update logic here
  8. studentDataAccessLayer.UpdateStudent(student);
  9. return RedirectToAction(nameof(Index));
  10. }
  11. catch
  12. {
  13. return View();
  14. }
  15. }

Edit option has been done. Now we will test if it works or not.

CRUD Operations Using ASP.NET Core And ADO.NET
CRUD Operations Using ASP.NET Core And ADO.NET

It is working fine. Now we will work with the Delete action.

Delete action

Now we will work with Delete Action within Student Controller. There are two Delete Actions, one is GET and another is POST. Now we will create a view for deleting action.

We have to call GetStudentData method from StudentDataAccessLayer class for getting student by Id.

  1. public ActionResult Delete(int id)
  2. {
  3. Student student = studentDataAccessLayer.GetStudentData(id);
  4. return View(student);
  5. }

Right click on Delete (GET) action then click add view; the below window will appear on screen.

CRUD Operations Using ASP.NET Core And ADO.NET

Click Add

  1. @model StudentRecordManagementSystem.Models.Student
  2. @{
  3. ViewData["Title"] = "Delete";
  4. }
  5. <h2>Delete</h2>
  6. <h3>Are you sure you want to delete this?</h3>
  7. <div>
  8. <h4>Student</h4>
  9. <hr />
  10. <dl class="dl-horizontal">
  11. <dt>
  12. @Html.DisplayNameFor(model => model.Id)
  13. </dt>
  14. <dd>
  15. @Html.DisplayFor(model => model.Id)
  16. </dd>
  17. <dt>
  18. @Html.DisplayNameFor(model => model.FirstName)
  19. </dt>
  20. <dd>
  21. @Html.DisplayFor(model => model.FirstName)
  22. </dd>
  23. <dt>
  24. @Html.DisplayNameFor(model => model.LastName)
  25. </dt>
  26. <dd>
  27. @Html.DisplayFor(model => model.LastName)
  28. </dd>
  29. <dt>
  30. @Html.DisplayNameFor(model => model.Email)
  31. </dt>
  32. <dd>
  33. @Html.DisplayFor(model => model.Email)
  34. </dd>
  35. <dt>
  36. @Html.DisplayNameFor(model => model.Mobile)
  37. </dt>
  38. <dd>
  39. @Html.DisplayFor(model => model.Mobile)
  40. </dd>
  41. <dt>
  42. @Html.DisplayNameFor(model => model.Address)
  43. </dt>
  44. <dd>
  45. @Html.DisplayFor(model => model.Address)
  46. </dd>
  47. </dl>
  48. <form asp-action="Delete">
  49. <input type="submit" value="Delete" class="btn btn-default" /> |
  50. <a asp-action="Index">Back to List</a>
  51. </form>
  52. </div>

Now we will work with Delete (POST)

  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public ActionResult Delete(Student student)
  4. {
  5. try
  6. {
  7. // TODO: Add delete logic here
  8. studentDataAccessLayer.DeleteStudent(student.Id);
  9. return RedirectToAction(nameof(Index));
  10. }
  11. catch
  12. {
  13. return View();
  14. }
  15. }

Details Action

We have to call GetStudentData method from StudentDataAccessLayer class for getting student by Id in Index action

  1. public ActionResult Details(int id)
  2. {
  3. Student student = studentDataAccessLayer.GetStudentData(id);
  4. return View(student);
  5. }

Right click on Details action then click add view; the below window will appear on screen.

CRUD Operations Using ASP.NET Core And ADO.NET

Click add

  1. @model StudentRecordManagementSystem.Models.Student
  2. @{
  3. ViewData["Title"] = "Details";
  4. }
  5. <h2>Details</h2>
  6. <div>
  7. <h4>Student</h4>
  8. <hr />
  9. <dl class="dl-horizontal">
  10. <dt>
  11. @Html.DisplayNameFor(model => model.Id)
  12. </dt>
  13. <dd>
  14. @Html.DisplayFor(model => model.Id)
  15. </dd>
  16. <dt>
  17. @Html.DisplayNameFor(model => model.FirstName)
  18. </dt>
  19. <dd>
  20. @Html.DisplayFor(model => model.FirstName)
  21. </dd>
  22. <dt>
  23. @Html.DisplayNameFor(model => model.LastName)
  24. </dt>
  25. <dd>
  26. @Html.DisplayFor(model => model.LastName)
  27. </dd>
  28. <dt>
  29. @Html.DisplayNameFor(model => model.Email)
  30. </dt>
  31. <dd>
  32. @Html.DisplayFor(model => model.Email)
  33. </dd>
  34. <dt>
  35. @Html.DisplayNameFor(model => model.Mobile)
  36. </dt>
  37. <dd>
  38. @Html.DisplayFor(model => model.Mobile)
  39. </dd>
  40. <dt>
  41. @Html.DisplayNameFor(model => model.Address)
  42. </dt>
  43. <dd>
  44. @Html.DisplayFor(model => model.Address)
  45. </dd>
  46. </dl>
  47. </div>
  48. <div>
  49. @Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
  50. <a asp-action="Index">Back to List</a>
  51. </div>

CRUD Operations Using ASP.NET Core And ADO.NET

I hope this will be helpful.