How To Get Data Using Angular in ASP.NET

Introduction

In this post we will discuss how to get data from the SQL server database using Angular JS in asp.net. We will learn some Angular directives in this blog.

AngularJS has a set of built-in directives which offers functionality to your applications.

  • ng-app: This directive initializes an AngularJS application
  • ng-module: This directive binds the value of HTML controls (input, select, textarea) to application data.
  • ng-controller: Defines the controller object for an application
  • ng-repeat: Defines a template for each data in a collection.
  • ng-src: Specifies the src attribute for the <img> element.
  • Filter: Selects a subset of items from array and returns it as a new array
  • Currency filter: Format a number to a currency format.

Step 1

Create a database in the SQL server of your choice.

  1. CREATE TABLE [dbo].[Employee](  
  2.     [ID] [int] IDENTITY(1,1) NOT NULL,  
  3.     [Name] [nvarchar](50) NULL,  
  4.     [Position] [nvarchar](50) NULL,  
  5.     [Office] [nvarchar](50) NULL,  
  6.     [Salary] [int] NULL,  
  7.     [Profile_Image] [nvarchar](50) NULL,  
  8.  CONSTRAINT [PK__Employee] PRIMARY KEY CLUSTERED   
  9. (  
  10.     [ID] ASC  
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]  
  12. )  
  13.   
  14. CREATE procedure [dbo].[spGetAllEmployee]  
  15. as  
  16. begin  
  17. select ID,Name,Position,Office,Salary,Profile_Image from [dbo].[Employee]  
  18. end  

Step 2

Create an empty project in visual studio of your choice. Add connection in webconfig file. Change data source and database.

  1. <connectionStrings>  
  2.     <add name="DBCS" connectionString="data source=FARHAN\SQLEXPRESS; database=SampleDB; integrated security=true;"/>  
  3.   </connectionStrings>  

Add the below script in webconfig file to work with get request and avoid internal server error.

  1. <system.web>  
  2.     <webServices>  
  3.       <protocols>  
  4.         <add name="HttpGet"/>  
  5.       </protocols>  
  6.     </webServices>  
  7.   </system.web>  

Step 3

Create class -- right click, select new item, choose class, and  give the name employees

  1. public class Employees  
  2.     {  
  3.         public int ID { get; set; }  
  4.         public string Name { get; set; }  
  5.         public string Position { get; set; }  
  6.         public string Office { get; set; }  
  7.         public int Salary { get; set; }  
  8.         public string Profile_Image { get; set; }  
  9.     }  

Step 4

Create web service -- right click, select new item, choose web service asmx, and give it a meaningful name

Add namespace

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Serialization;

Create web method to call data 

  1. [WebMethod]  
  2.         public void GetAllEmployees()  
  3.         {  
  4.             List<Employees> employeeList = new List<Employees>();  
  5.             string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
  6.             using (SqlConnection con = new SqlConnection(CS))  
  7.             {  
  8.                 SqlCommand cmd = new SqlCommand("spGetAllEmployee", con);  
  9.                 cmd.CommandType = CommandType.StoredProcedure;  
  10.                 con.Open();  
  11.                 SqlDataReader rdr = cmd.ExecuteReader();  
  12.   
  13.                 while (rdr.Read())  
  14.                 {  
  15.                     Employees emp = new Employees();  
  16.                     emp.ID = Convert.ToInt32(rdr["ID"]);  
  17.                     emp.Name = rdr["Name"].ToString();  
  18.                     emp.Position= rdr["Position"].ToString();  
  19.                     emp.Office= rdr["Office"].ToString();  
  20.                     emp.Salary= Convert.ToInt32(rdr["Salary"]);  
  21.                     emp.Profile_Image= rdr["Profile_Image"].ToString();  
  22.   
  23.                     employeeList.Add(emp);  
  24.                 }  
  25.             }  
  26.   
  27.             JavaScriptSerializer js = new JavaScriptSerializer();  
  28.             Context.Response.Write(js.Serialize(employeeList));  
  29.         }  

Step 5

Create web form with meaningful name and an images folder to add some images.

Add script and style in head section.

  1. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">  
  2. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">  
  3. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>  
  4. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>  

Write Angular script to call web service.

  1. <script type="text/javascript">  
  2.         var app = angular  
  3.         .module("myModule", [])  
  4.         .controller("myController", function ($scope, $http) {  
  5.   
  6.             $http.get("EmployeeService.asmx/GetAllEmployees")  
  7.                  .then(function (response) {  
  8.                      $scope.employees = response.data;  
  9.                  });  
  10.         })  
  11. </script>  

Design html

  1. <body ng-app="myModule">  
  2.     <form id="form1" runat="server">  
  3.         <div class="container" ng-controller="myController">  
  4.             <h3 class="text-center">How to GetData using Angular JS in ASP.NET</h3>  
  5.             <div class="row">  
  6.                 <div class="col-md-6">  
  7.                     <div class="form-group">  
  8.                         <label>Search:</label>  
  9.                         <div class="input-group">  
  10.                             <div class="input-group-prepend">  
  11.                                 <span class="input-group-text"><i class="fa fa-search"></i></span>  
  12.                             </div>  
  13.                             <input type="text" class="form-control " placeholder="Search employees" ng-model="searchText" />  
  14.                         </div>  
  15.                     </div>  
  16.                 </div>  
  17.             </div>  
  18.             <table class="table table-bordered table-striped">  
  19.                 <thead class="bg-dark text-white text-uppercase">  
  20.                     <tr>  
  21.                         <th>ID</th>  
  22.                         <th>Name</th>  
  23.                         <th>Position</th>  
  24.                         <th>Office</th>  
  25.                         <th>Salary</th>  
  26.                         <th>Profile Picture</th>  
  27.                     </tr>  
  28.                 </thead>  
  29.                 <tbody>  
  30.                     <tr ng-repeat="emplopye in employees|filter:searchText">  
  31.                         <td>{{emplopye.ID}}</td>  
  32.                         <td>{{emplopye.Name}}</td>  
  33.                         <td>{{emplopye.Position}}</td>  
  34.                         <td>{{emplopye.Office}}</td>  
  35.                         <td>{{emplopye.Salary| currency}}</td>  
  36.                         <td>  
  37.                             <img ng-src="{{emplopye.Profile_Image}}" class="img-thumbnail" style="width: 50px; height: 50px;" />  
  38.                         </td>  
  39.                     </tr>  
  40.                 </tbody>  
  41.             </table>  
  42.         </div>  
  43.     </form>  
  44. </body>  

Complete web page code

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4. <head runat="server">  
  5.     <title>Employee</title>  
  6.     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">  
  7.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">  
  8.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>  
  9.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>  
  10.     <script type="text/javascript">  
  11.         var app = angular  
  12.         .module("myModule", [])  
  13.         .controller("myController", function ($scope, $http) {  
  14.   
  15.             $http.get("EmployeeService.asmx/GetAllEmployees")  
  16.                  .then(function (response) {  
  17.                      $scope.employees = response.data;  
  18.                  });  
  19.         })  
  20.     </script>  
  21. </head>  
  22. <body ng-app="myModule">  
  23.     <form id="form1" runat="server">  
  24.         <div class="container" ng-controller="myController">  
  25.             <h3 class="text-center">How to GetData using Angular JS in ASP.NET</h3>  
  26.             <div class="row">  
  27.                 <div class="col-md-6">  
  28.                     <div class="form-group">  
  29.                         <label>Search:</label>  
  30.                         <div class="input-group">  
  31.                             <div class="input-group-prepend">  
  32.                                 <span class="input-group-text"><i class="fa fa-search"></i></span>  
  33.                             </div>  
  34.                             <input type="text" class="form-control " placeholder="Search employees" ng-model="searchText" />  
  35.                         </div>  
  36.                     </div>  
  37.                 </div>  
  38.             </div>  
  39.             <table class="table table-bordered table-striped">  
  40.                 <thead class="bg-dark text-white text-uppercase">  
  41.                     <tr>  
  42.                         <th>ID</th>  
  43.                         <th>Name</th>  
  44.                         <th>Position</th>  
  45.                         <th>Office</th>  
  46.                         <th>Salary</th>  
  47.                         <th>Profile Picture</th>  
  48.                     </tr>  
  49.                 </thead>  
  50.                 <tbody>  
  51.                     <tr ng-repeat="emplopye in employees|filter:searchText">  
  52.                         <td>{{emplopye.ID}}</td>  
  53.                         <td>{{emplopye.Name}}</td>  
  54.                         <td>{{emplopye.Position}}</td>  
  55.                         <td>{{emplopye.Office}}</td>  
  56.                         <td>{{emplopye.Salary| currency}}</td>  
  57.                         <td>  
  58.                             <img ng-src="{{emplopye.Profile_Image}}" class="img-thumbnail" style="width: 50px; height: 50px;" />  
  59.                         </td>  
  60.                     </tr>  
  61.                 </tbody>  
  62.             </table>  
  63.         </div>  
  64.     </form>  
  65. </body>  
  66. </html>  

Step 5

Run project ctr+F5

Final output on bowser:

output