Introduction

In this article, I will demonstrate how to display employee details using AngularJS in ASP.NET. I will upload the employee details using bootstrap 4 modal and insert them into a SQL database table and retrieve those using AngularJS and ASP.NET Web Service.

Step 1:Open SQL Server 2014 and create a database table.

  1. CREATE TABLE [dbo].[EmployeeDetails](
  2. [ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [nvarchar](50) NULL,
  4. [Position] [nvarchar](50) NULL,
  5. [Office] [nvarchar](50) NULL,
  6. [Profile_Picture] [nvarchar](50) NULL,
  7. CONSTRAINT [PK_EmployeeDetails] PRIMARY KEY CLUSTERED
  8. (
  9. [ID] ASC
  10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  11. ) ON [PRIMARY]
  12. GO
  13. CREATE ALTER procedure [dbo].[spAddEmployeeDetails]
  14. (
  15. @Name nvarchar(50),
  16. @Position nvarchar(50),
  17. @Office nvarchar(50),
  18. @Profile_Picture nvarchar(50)
  19. )
  20. as
  21. begin
  22. insert into [dbo].[EmployeeDetails](Name,Position,Office,Profile_Picture)
  23. values(@Name,@Position,@Office,@Profile_Picture)
  24. end
  25. CREATE procedure [dbo].[spGetEmployeeDetails]
  26. as
  27. begin
  28. select ID,Name,Position,Office,Profile_Picture from [dbo].[EmployeeDetails]
  29. end
ASP.NET

Step 2:Open Visual Studio 2015, click on New Project, and create an empty web application project.

Screenshot for creating new project 1

ASP.NET

After clicking on New Project, one window will appear. Select Web from the left panel, choose ASP.NET Web Application, give it a meaningful name for your project, then click on OK as shown in the below screenshot.

Screenshot for creating new project 2

ASP.NET

After clicking on OK, one more window will appear. Choose Empty, check on Web Forms checkbox, and click on OK.

Screenshot for creating new project 3

ASP.NET

Step 3: Double-click on webconfig file to add database connection.

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

Add the following code to get data.

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

Step 4: Right-click on Models folder, select Add, then choose a class and click.

Add C# class screenshot 1

ASP.NET

After that, a window will appear. Choose class as shown in the below image. Give it the name EmployeeDetails and click on Add.

Add C# class screenshot 2

ASP.NET

Write the following class.

  1. namespace DisplayEmployeeDetails_Demo.Models
  2. {
  3. public class EmployeeDetails
  4. {
  5. public int ID { get; set; }
  6. public string Name { get; set; }
  7. public string Position { get; set; }
  8. public string Office { get; set; }
  9. public Nullable<int>Experience{ get; set; }
  10. public string Profile_Picture { get; set; }
  11. }
  12. }

Step 5: Right click on the project, select Add, then choose a new item and click on it.

Screenshot for Web Service 1

ASP.NET

After that, a window will appear. Choose Web Service (ASMX). Give it a name as EmployeeService and click on Add.

Screenshot for Web Service 2

ASP.NET

Add namespace

  1. using DisplayEmployeeDetails_Demo.Models;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Web.Script.Serialization;
  6. using System.Web.Services;
Write the following code in web service
  1. Write following code in web service
  2. [WebMethod]
  3. public void GetEmployeeDetails()
  4. {
  5. List<EmployeeDetails> employeeList = new List<EmployeeDetails>();
  6. string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
  7. using (SqlConnection con = new SqlConnection(CS))
  8. {
  9. SqlCommand cmd = new SqlCommand("spGetEmployeeDetails", con);
  10. cmd.CommandType = CommandType.StoredProcedure;
  11. con.Open();
  12. SqlDataReader rdr = cmd.ExecuteReader();
  13. while (rdr.Read())
  14. {
  15. EmployeeDetails emp = new EmployeeDetails();
  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.Profile_Picture = rdr["Profile_Picture"].ToString();
  21. employeeList.Add(emp);
  22. }
  23. }
  24. JavaScriptSerializer js = new JavaScriptSerializer();
  25. Context.Response.Write(js.Serialize(employeeList));
  26. }

Complete web service code

  1. using DisplayEmployeeDetails_Demo.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Web.Script.Serialization;
  8. using System.Web.Services;
  9. namespace DisplayEmployeeDetails_Demo
  10. {
  11. /// <summary>
  12. /// Summary description for EmployeeService
  13. /// </summary>
  14. [WebService(Namespace = "http://tempuri.org/")]
  15. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  16. [System.ComponentModel.ToolboxItem(false)]
  17. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  18. // [System.Web.Script.Services.ScriptService]
  19. public class EmployeeService : System.Web.Services.WebService
  20. {
  21. [WebMethod]
  22. public void GetEmployeeDetails()
  23. {
  24. List<EmployeeDetails> employeeList = new List<EmployeeDetails>();
  25. string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
  26. using (SqlConnection con = new SqlConnection(CS))
  27. {
  28. SqlCommand cmd = new SqlCommand("spGetEmployeeDetails", con);
  29. cmd.CommandType = CommandType.StoredProcedure;
  30. con.Open();
  31. SqlDataReader rdr = cmd.ExecuteReader();
  32. while (rdr.Read())
  33. {
  34. EmployeeDetails emp = new EmployeeDetails();
  35. emp.ID = Convert.ToInt32(rdr["ID"]);
  36. emp.Name = rdr["Name"].ToString();
  37. emp.Position = rdr["Position"].ToString();
  38. emp.Office = rdr["Office"].ToString();
  39. emp.Profile_Picture = rdr["Profile_Picture"].ToString();
  40. employeeList.Add(emp);
  41. }
  42. }
  43. JavaScriptSerializer js = new JavaScriptSerializer();
  44. Context.Response.Write(js.Serialize(employeeList));
  45. }
  46. }
  47. }

Step 6: Right click on the project, select Add, then choose Web Form and click on it.

Screenshot adding web form 1

ASP.NET

After that, one window will appear. Give it the name EmployeeList and click on OK.

Screenshot adding web form 2

ASP.NET

Add the following CDN link for style and script.

  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://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  4. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
  5. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  6. <link href="EmployeeCard.css" rel="stylesheet" />

Write the script to retrieve data from the database.

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

Design web form

  1. <body ng-controller="myController">
  2. <form id="form1" runat="server">
  3. <div class="container py-4">
  4. <div class="card">
  5. <div class="card-header bg-primary text-white">
  6. <h5 class="card-title text-uppercase">Employee Details</h5>
  7. </div>
  8. <div class="card-body">
  9. <button type="button" class="btn btn-primary rounded-0" data-toggle="modal" data-target="#myModal">
  10. <i class="fa fa-plus-circle"></i>Add New
  11. </button>
  12. <div class="modal" id="myModal">
  13. <div class="modal-dialog modal-lg">
  14. <div class="modal-content">
  15. <div class="modal-header">
  16. <h5 class="modal-title">Employee Details</h5>
  17. <button type="button" class="close" data-dismiss="modal">×</button>
  18. </div>
  19. <div class="modal-body">
  20. <div class="row">
  21. <div class="col-md-6">
  22. <div class="form-group">
  23. <label>Name:</label>
  24. <div class="input-group">
  25. <div class="input-group-prepend">
  26. <span class="input-group-text"><i class="fa fa-user"></i></span>
  27. </div>
  28. <asp:TextBox ID="txtName" CssClass="form-control" runat="server"></asp:TextBox>
  29. </div>
  30. </div>
  31. </div>
  32. <div class="col-md-6">
  33. <div class="form-group">
  34. <label>Position:</label>
  35. <div class="input-group">
  36. <div class="input-group-prepend">
  37. <span class="input-group-text"><i class="fa fa-suitcase"></i></span>
  38. </div>
  39. <asp:TextBox ID="txtPosition" CssClass="form-control" runat="server"></asp:TextBox>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. <div class="row">
  45. <div class="col-md-6">
  46. <div class="form-group">
  47. <label>Office:</label>
  48. <div class="input-group">
  49. <div class="input-group-prepend">
  50. <span class="input-group-text"><i class="fa fa-building"></i></span>
  51. </div>
  52. <asp:TextBox ID="txtOffice" CssClass="form-control" runat="server"></asp:TextBox>
  53. </div>
  54. </div>
  55. </div>
  56. <div class="col-md-6">
  57. <div class="form-group">
  58. <label>Profile Picture:</label>
  59. <div class="custom-file">
  60. <asp:FileUpload ID="ProfileFileUpload" CssClass="custom-file-input" runat="server" />
  61. <label class="custom-file-label"></label>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. <div class="modal-footer">
  68. <asp:Button ID="btnSubmit" CssClass="btn btn-primary rounded-0" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
  69. <button type="button" class="btn btn-danger rounded-0" data-dismiss="modal">Close</button>
  70. </div>
  71. </div>
  72. </div>
  73. </div>
  74. <div class="row">
  75. <div class="col-md-3 col-sm-6" ng-repeat="emplopye in employees">
  76. <div class="our-team">
  77. <div class="pic">
  78. <img ng-src="{{emplopye.Profile_Picture}}" />
  79. </div>
  80. <h3 class="title">{{emplopye.Name}}</h3>
  81. <span class="post">{{emplopye.Position}}</span>
  82. <span class="post">{{emplopye.Office}}</span>
  83. <ul class="social">
  84. <li><a href="#" class="fa fa-facebook"></a></li>
  85. <li><a href="#" class="fa fa-twitter"></a></li>
  86. <li><a href="#" class="fa fa-google-plus"></a></li>
  87. <li><a href="#" class="fa fa-linkedin"></a></li>
  88. </ul>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. </div>
  94. </form>
  95. </body>

Step 7: Right click on project, select add, then choose stylesheet. Give it the name EmployeeCard.

  1. .our-team{
  2. padding: 20px 15px 30px;
  3. background: #fff;
  4. text-align: center;
  5. border: 1px solid #1b1e21;
  6. margin-top: 10px;
  7. margin-bottom: 10px;
  8. box-shadow: 1px 1px 1px #212529;
  9. border-radius: 3px;
  10. }
  11. .our-team .pic{
  12. display: inline-block;
  13. width: 100%;
  14. height: 100%;
  15. background: #fff;
  16. padding: 10px;
  17. margin-bottom: 25px;
  18. transition: all 0.5s ease 0s;
  19. }
  20. .our-team:hover .pic{
  21. background: #17bebb;
  22. border-radius: 50%;
  23. }
  24. .pic img{
  25. width: 100%;
  26. height: auto;
  27. border-radius: 50%;
  28. }
  29. .our-team .title{
  30. display: block;
  31. font-size: 16px;
  32. font-weight: 600;
  33. color: #2e282a;
  34. text-transform: uppercase;
  35. margin: 0 0 7px 0;
  36. }
  37. .our-team .post{
  38. display: block;
  39. font-size: 15px;
  40. color: #17bebb;
  41. text-transform: capitalize;
  42. margin-bottom: 10px;
  43. }
  44. .our-team .social{
  45. padding: 0;
  46. margin: 0;
  47. list-style: none;
  48. }
  49. .our-team .social li{
  50. display: inline-block;
  51. margin-right: 5px;
  52. }
  53. .our-team .social li a{
  54. display: block;
  55. width: 30px;
  56. height: 30px;
  57. line-height: 30px;
  58. border-radius: 50%;
  59. font-size: 15px;
  60. color: #17bebb;
  61. border: 1px solid #17bebb;
  62. transition: all 0.5s ease 0s;
  63. }
  64. .our-team:hover .social li a{
  65. background: #17bebb;
  66. color: #fff;
  67. }
  68. @media only screen and (max-width: 990px){
  69. .our-team{ margin-bottom: 30px; }
  70. }
Step 8: Double-click on the Submit button and write the following C# code.

Add namespace

  1. using System.Data;
  2. using System.Data.SqlClient;
  3. using System.Configuration;
  4. using System.IO;

Complete code for C# backend

  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Configuration;
  5. using System.IO;
  6. namespace DisplayEmployeeDetails_Demo
  7. {
  8. public partial class EmployeesList : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. if (!IsPostBack)
  13. {
  14. ClearTexBox();
  15. }
  16. }
  17. private void ClearTexBox()
  18. {
  19. txtName.Text = string.Empty;
  20. txtPosition.Text = string.Empty;
  21. txtOffice.Text = string.Empty;
  22. }
  23. protected void btnSubmit_Click(object sender, EventArgs e)
  24. {
  25. if (ProfileFileUpload.PostedFile!=null)
  26. {
  27. string FileName = Path.GetFileName(ProfileFileUpload.PostedFile.FileName);
  28. ProfileFileUpload.SaveAs(Server.MapPath("/images" + FileName));
  29. string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
  30. using (SqlConnection con = new SqlConnection(CS))
  31. {
  32. SqlCommand cmd = new SqlCommand("spAddEmployeeDetails",con);
  33. cmd.CommandType = CommandType.StoredProcedure;
  34. con.Open();
  35. cmd.Parameters.AddWithValue("@Name",txtName.Text.Trim());
  36. cmd.Parameters.AddWithValue("@Position",txtPosition.Text.Trim());
  37. cmd.Parameters.AddWithValue("@Office",txtOffice.Text.Trim());
  38. cmd.Parameters.AddWithValue("@Profile_Picture","images" + FileName);
  39. cmd.ExecuteNonQuery();
  40. ClearTexBox();
  41. }
  42. }
  43. }
  44. }
  45. }

Step 9: Run the project by pressing ctrl+F5.

Screenshot 1

ASP.NET

Screenshot 2

ASP.NET

Conclusion

In this article, I have explained how we can display employee list with their respective data. I hope this is useful for you.