This article shows how to do Create, Read, Update and Delete (CRUD) operations in MVC 5, WEB API 2 and AngularJS with a Stored Procedure.
The following is the table I will use to do the CRUD operations:
![design table]()
Image 1.
The following is sample data for the table:
![Data in my Table]()
Image 2.
The following are the four Stored Procedures.
1. ADDNewEmployee
- CREATE PROCEDURE [dbo].[ADDNewEmployee]
- (
- @Emp_Name VARCHAR(100) = null,
- @Email VARCHAR(500) = null,
- @Country VARCHAR(100) = null ,
- @ProjectID INT,
- @ManagerName VARCHAR(100) = null,
- @ProfileImage VARCHAR(100) = null
- )
- AS
-
- BEGIN
- INSERT INTO Employee (Name, Email,Country,ProjectID,ManagerName,ProfileImage)
- VALUES (@Emp_Name, @Email, @Country,@ProjectID,@ManagerName, @ProfileImage)
- END
![ADDNewEmployee]()
Image 3.
2. UpdateEmployee
- CREATE PROCEDURE [dbo].[UpdateEmployee]
- (
- @Emp_ID INT,
- @Emp_Name VARCHAR(100) = null,
- @Email VARCHAR(500) = null,
- @Country VARCHAR(100) = null ,
- @ManagerName VARCHAR(100) = null
- )
- AS
-
- BEGIN
- UPDATE Employee
- SET Name=@Emp_Name,
- Email=@Email,
- Country=@Country,
- ManagerName=@ManagerName
- WHERE ID=@Emp_ID
- END
![UpdateEmployee]()
Image 4.
3. DeleteEmployee.
- ALTER PROCEDURE [dbo].[DeleteEmployee]
- (
- @Emp_ID INT
- )
- AS
- BEGIN
- DELETE FROM Employee WHERE ID=@Emp_ID
- END
![DeleteEmployee]()
Image 5.
4. ReadAllEmployee
-
- ALTER PROCEDURE [dbo].[ReadAllEmployee]
- (
- @Emp_Name VARCHAR(100) = null,
- @Country VARCHAR(100) = null,
- @ManagerName VARCHAR(100) = null
- )
- AS
- BEGIN
- IF (@Emp_Name IS NULL)
- BEGIN
- SET @Emp_Name=''
- END
- IF (@Country IS NULL)
- BEGIN
- SET @Country=''
- END
- IF (@ManagerName IS NULL)
- BEGIN
- SET @ManagerName=''
- END
-
- SELECT ID, Name, Email, Country, ManagerName
- FROM Employee
- WHERE Name like @Emp_Name +'%'
- AND Country like @Country +'%'
- AND ManagerName like @ManagerName +'%'
- ORDER BY Name
- END
![query]()
Image 6.
Now open Visual Studio 2015 and create new ASP.NET web application.
![Create New Application]()
Image 7.
![mvc]()
Image 8.
Now right-click on the project in the Solution Explorer as in the following:
![Solution Explorer]()
Image 9.
Select ADO.NET Entity Data Model.
![Data Model]()
Image 10.
![Entity Data Model]()
Image 11.
Select your Data Source and database.
![Select your Data Sourc]()
Image 12.
![connection]()
Image 13.
Select your data table and Stored Procedure.
![Select your Data table]()
Image 14.
![Stored Procedure]()
Image 15.
Now to add a Web API controller. So right-click on Controllers and Add New Controller.
![add Web API controller]()
Image 16.
Select
Web API 2 Controller – Empty.
Image 17.
Provide it a name.
![controller name]()
Image 18.
Here's the code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using CRUD_MVC5_WebAPI_SP.Models;
-
- namespace CRUD_MVC5_WebAPI_SP.Controllers
- {
- public class ManageEmployeeController : ApiController
- {
- TestDBEntities obj = new TestDBEntities();
-
- [HttpGet]
- public IEnumerable<ReadAllEmployee_Result> ListAllEmployee(string emp_Name, string country, string managerName)
- {
- return obj.ReadAllEmployee(emp_Name, country, managerName).AsEnumerable();
- }
-
- [HttpGet]
- public void addNewEmployee(string emp_Name, string email, string country, string managerName)
- {
- obj.ADDNewEmployee(emp_Name, email, country, 1, managerName, "Profile.jpg");
- }
-
- [HttpGet]
- public void updateEmployee(int? emp_ID, string emp_Name, string email, string country, string managerName)
- {
- obj.UpdateEmployee(emp_ID, emp_Name, email, country, managerName);
- }
-
- [HttpGet]
- public void deleteEmployee(int emp_ID)
- {
- obj.DeleteEmployee(emp_ID);
- obj.SaveChanges();
- }
- }
- }
![code]()
Image 19.
Now to add the AngularJs reference to your project. So right-click on your project in the Solution Explorer as in the following, then click
Manage NuGet Packages...![manage nuget package]()
Image 20.
![install]()
Image 21.
![click uninstall]()
Image 22.
Now create a new folder under the Scripts Folder, then right-click MyScripts and select AngularController as in the following:
![Select AngularController]()
Image 23.
Here's the code:
-
-
-
-
-
- var app;
- (function () {
- app = angular.module("RESTClientModule", ['ngAnimate']);
- })();
-
- app.controller("AngularJs_ManageEmployeeInfoController", function ($scope, $timeout, $rootScope, $window, $http) {
- $scope.date = new Date();
- $scope.empNameSearch = "";
- $scope.empCountrySearch = "";
- $scope.ManagerNameSearch = "";
-
- $scope.ManageEMP_Info = false;
- $scope.ListEmployee = true;
- $scope.searchEmployee = true;
-
-
- $scope.EmpIds = 0;
- $scope.empName = "";
- $scope.empEmail = "";
- $scope.empCountry = "";
- $scope.empManager = "";
-
- getEmployeeList($scope.empNameSearch, $scope.empCountrySearch, $scope.ManagerNameSearch);
-
- function getEmployeeList(EmployeeName, Country, ManagerName) {
- $http.get('/api/ManageEmployee/', { params: { emp_Name: EmployeeName, country: Country, managerName: ManagerName } }).success(function (data) {
- $scope.Employees = data;
- $scope.ManageEMP_Info = false;
- $scope.ListEmployee = true;
- $scope.searchEmployee = true;
- if ($scope.Employees.length > 0) {
- }
- })
- .error(function () {
- $scope.error = "Some Error.";
- });
- }
-
-
- $scope.searchEmp = function () {
-
- getEmployeeList($scope.empNameSearch, $scope.empCountrySearch, $scope.ManagerNameSearch);
- }
-
-
- $scope.editEmployeeInfo = function editEmployeeInfo(Employee_ID, Name, Email, Country, ManagerName) {
- cleardetails();
- $scope.EmpIds = Employee_ID;
- $scope.empName = Name
- $scope.empEmail = Email;
- $scope.empCountry = Country;
- $scope.empManager = ManagerName;
-
- $scope.ManageEMP_Info = true;
- $scope.ListEmployee = true;
- $scope.searchEmployee = true;
- }
-
-
- $scope.Employee_Delete = function Employee_Delete(EmpIds, Name) {
- cleardetails();
- $scope.EmpIds = EmpIds;
- var delConfirm = confirm("Are you sure you want to delete the Employee " + Name + " ?");
- if (delConfirm == true) {
-
- $http.get('/api/ManageEmployee/deleteEmployee/', { params: { emp_ID: $scope.EmpIds } }).success(function (data) {
- alert("Employee Deleted Successfully!!");
- cleardetails();
- $scope.empNameSearch = "";
- $scope.empCountrySearch = "";
- $scope.ManagerNameSearch = "";
- getEmployeeList($scope.empNameSearch, $scope.empCountrySearch, $scope.ManagerNameSearch);
- })
- .error(function () {
- $scope.error = "Some Error.";
- });
-
- }
- }
-
- $scope.AddEmployeeForm = function () {
- cleardetails();
- $scope.ManageEMP_Info = true;
- $scope.ListEmployee = true;
- $scope.searchEmployee = true;
- }
-
- function cleardetails() {
- $scope.EmpIds = 0;
- $scope.empName = "";
- $scope.empEmail = "";
- $scope.empCountry = "";
- $scope.empManager = "";
- }
-
-
- $scope.Message = "";
- $scope.IsFormSubmitted = false;
- $scope.IsFormValid = false;
- $scope.$watch("f1.$valid", function (isValid) {
- $scope.IsFormValid = isValid;
- });
-
-
- $scope.saveDetails = function () {
-
- $scope.IsFormSubmitted = true;
- if ($scope.IsFormValid) {
- if ($scope.EmpIds == 0) {
- $http.get('/api/ManageEmployee/addNewEmployee/', { params: { emp_Name: $scope.empName, email: $scope.empEmail, country: $scope.empCountry, managerName: $scope.empManager } }).success(function (data) {
- alert("Employee Added Successfully.");
- cleardetails();
- $scope.empNameSearch = "";
- $scope.empCountrySearch = "";
- $scope.ManagerNameSearch = "";
- getEmployeeList($scope.empNameSearch, $scope.empCountrySearch, $scope.ManagerNameSearch);
-
- })
- .error(function () {
- $scope.error = "Some Error.";
- });
- }
- else {
- $http.get('/api/ManageEmployee/updateEmployee/', { params: { emp_ID: $scope.EmpIds, emp_Name: $scope.empName, email: $scope.empEmail, country: $scope.empCountry, managerName: $scope.empManager } }).success(function (data) {
-
- alert("Employee Updated Successfully");
- cleardetails();
- $scope.empNameSearch = "";
- $scope.empCountrySearch = "";
- $scope.ManagerNameSearch = "";
- getEmployeeList($scope.empNameSearch, $scope.empCountrySearch, $scope.ManagerNameSearch);
-
- })
- .error(function () {
- $scope.error = "Some Error.";
- });
- }
-
- }
- else {
- $scope.Message = "All the fields are required.";
- }
- }
- });
Now for adding a new controller right-click on the Controller, select Add and click Controller as in the following:
![add controller]()
Image 24.
![controller]()
Image 25.
![type controller name]()
Image 26.
![add view in project]()
Image 27.
![add view]()
Image 28.
Here's the code in Index.cshtml:
- <html data-ng-app="RESTClientModule">
- @{
- ViewBag.Title = "Manage Employee Information Uisng MVC 5 , WebAPI2, AngularJS with Stored Procedure";
- }
- <body data-ng-controller="AngularJs_ManageEmployeeInfoController">
- <table style='width: 99%; background-color:skyblue; text-align:center;'>
- <tr ng-show="searchEmployee">
- <td>
- <table style=" background-color:crimson; border: solid 4px green; padding: 5px;width: 99%; color:white;" cellpadding="2" cellspacing="2">
- <tr>
- <td>
- <b>Name</b>
- : <input type="text" name="txtEmpName" ng-model="empNameSearch" value="" />
- </td>
- <td>
- <b> Country </b> :
- <input type="text" name="txtCountry" ng-model="empCountrySearch" />
- </td>
- <td>
- <b> Manager Name </b> :
- <input type="text" name="txtManager" ng-model="ManagerNameSearch" />
- </td>
- <td>
- <input type="submit" value="Search Employee" ng-click="searchEmp()" />
- </td>
- <td align="right">
- <input type="button" value="Add New Employee" ng-click="AddEmployeeForm()" />
- </td>
- </tr>
- </table>
- </td>
- </tr>
- <tr ng-show="ManageEMP_Info">
- <td>
- <table style="border:solid 4px blue; background-color:orange; width:100%;">
- <tr>
- <td width="20"></td>
- <td valign="top">
- <form novalidate name="f1" ng-submit="saveDetails()">
- <table style="color:#0d3d2b; font-weight:bold; font-size:12pt; text-align:right;">
- <tr>
- <td>
- Employee ID :
- </td>
- <td>
- <input type="text" name="txtEmpId" ng-model="EmpIds" value="0" style="background-color:tan" readonly />
- </td>
- <td>
- Name :
- </td>
- <td>
- <input type="text" name="txtEmpName" ng-model="empName" placeholder=" Employee Name..." required />
- <br />
- <span class="error" ng-show="(f1.file.$dirty || IsFormSubmitted) && f1.txtEmpName.$error.required">Name required!</span>
- </td>
- </tr>
- <tr>
- <td>
- Email :
- </td>
- <td>
- <input type="text" name="txtEmail" ng-model="empEmail" placeholder=" Email..." required />
- <br />
- <span class="error" ng-show="(f1.file.$dirty || IsFormSubmitted) && f1.txtEmail.$error.required">Email required!</span>
- </td>
- <td>
- Country :
- </td>
- <td>
- <input type="text" name="txtCountry" ng-model="empCountry" placeholder=" Country ..." required />
- <br />
- <span class="error" ng-show="(f1.file.$dirty || IsFormSubmitted) && f1.txtCountry.$error.required">Country required!</span>
- </td>
- </tr>
- <tr>
- <td>
- Manager Name:
- </td>
- <td>
- <input type="text" name="txtManager" ng-model="empManager" placeholder=" Manager ..." required />
- <br />
- <span class="error" ng-show="(f1.file.$dirty || IsFormSubmitted) && f1.txtManager.$error.required">Manager Name required!</span>
- </td>
- <td colspan="2">
- <input type="submit" value="Save Employee" style="background-color:#336699;color:#FFFFFF" required />
- </td>
- </tr>
- </table>
- </form>
- </td>
- </tr>
- </table>
- </td>
- </tr>
- <tr ng-show="ListEmployee">
- <td>
- <table style="width:100%;">
- <tr style="background-color:darkgreen; color:white; font-weight:bold;">
- <td width="120px" align="center">Name</td>
- <td width="120" align="center">Email</td>
- <td width="80" align="center">Country</td>
- <td width="200" align="center">Manager Name</td>
- <td width="40" align="center">Edit</td>
- <td width="40" align="center">Delete</td>
- </tr>
- <tbody data-ng-repeat="emp in Employees">
- <tr style="background-color:whitesmoke; font-family:Cambria, Cochin, Georgia, Times, Times New Roman, serif; font-size:11pt; color:midnightblue; text-align:left; padding-left:10px;">
- <td style="width:120px;">
- {{emp.Name}}
- </td>
-
- <td>
- {{emp.Email}}
- </td>
-
- <td>
- {{emp.Country}}
- </td>
-
- <td>
- {{emp.ManagerName}}
- </td>
- <td align="center">
- <span style="color:#9F000F;">
- <input type="submit" value="Edit" ng-click="editEmployeeInfo(emp.ID,emp.Name,emp.Email,emp.Country,emp.ManagerName)" />
- </span>
- </td>
-
- <td align="center">
- <span style="color:#9F000F;">
- <input type="submit" value="Delete" ng-click="Employee_Delete(emp.ID,emp.Name)" />
- </span>
- </td>
-
- </tr>
- </tbody>
- </table>
-
- </td>
- </tr>
- </table>
- </body>
- </html>
-
- <script src="~/Scripts/angular-animate.js"></script>
- <script src="~/Scripts/angular.js"></script>
- <script src="~/Scripts/angular.min.js"></script>
- <script src="~/Scripts/angular-animate.min.js"></script>
- <script src="~/Scripts/MyScripts/controller.js"></script>
To make your view as the default run you need to make a change in the rout.config like the following:
Image 29.
Now run your application:
![Now run your Application]()
Image 30.
Now provide some search criteria as in the following:
![put some search criteria]()
Image 31.
![select country]()
Image 32.
![manage employee information]()
Image 33.
![result]()
Image 34.
Now click on Add New Employee.
![Country]()
Image 35.
![save employee]()
Image 36.
![message]()
Image 37.
![table]()
Image 38.
Now edit a record:
![Edit Any Record]()
Image 39.
![index]()
Image 40.
![Record]()
Image 41.
Now delete a record:
![Delete Any Record]()
Image 42.