Angular 5, ASP.NET Core CRUD For Inventory Management Using EF And WEB API

In my previous article, I have explained how to get started with Angular5 and ASP.NET Core. Now in this article let's see in depth on how to work with WEB API, EF for performing a CRUD operation for Angular5 and ASP.NET Core application.

ASP.NET Core

If you are new to Angular 5 and ASP.NET Core, then kindly read my previous article.

In my previous article, I have explained how to get started with Angular 5 and ASP.NET Core. Now, in this article, let's see in depth how to work with WEB API, EF for performing a CRUD operation for Angular 5 and ASP.NET Core application.

For performing CRUD operation here, I have taken the concept as Inventory Management priority based Reorder level system.

Inventory management and Reorder Level

Inventory management is very important to maintain and manage the stocks in small shops or in a big warehouse. For example, let’s consider a small shop where they sell items like Tooth Paste, Soap, Shampoo, etc. For all the items in the shop, they will make a list with the item name and they will add the total available stock quantity for each item, Total Sales quantity per item and Total Quantity received to be added in the stock. If the items are small in number, it will be easy to find the items needed to be purchased again to maintain the stock quantity, consider if it’s a big factory and needs to maintain all the stock quantity. Using Inventory Management software, the end user can easily track Inventory items, Total available Stock Quantity, Total Arrived and Issued Stock Quantity, Reorder Level for each item, Reorder Date for each Item and priority based reorder for the items which need to be stocked immediately.

Reorder Level

In Inventory Management Reorder Level or Point is an important part which will be used to indicate the customer as this item Stock is going to be finished and need to be purchased or produced for sales or delivered to the end user.

Inventory Management has many processes to be taken care of to maintain the software. In this article, we will make a simple project to display the web based Monitoring system for indicating the priority based Inventory Management Reorder System.

For Reorder Level there has to be some formula for displaying the indicator on each item which needs to be stocked. In our simple project, I have used the type of Priority based indication to the customer as manual and automatic.

Note: In Inventory Management we will be having Item Master table, Item Detail Table, Inventory tables and etc. For this simple project, I have used only one table as Inventory table and added the Item Name directly with Stock Qty, Reorder Qty and Priority Status, Using the Priority status user can add or edit the priority for each item which needs to be stocked immediately. Here priority Status is to manually add or edit the priority for each item which needs to be stocked.

Manual Priority setting for Reorder Level

When customer adds /edits each Inventory item they can set manually priority for reordering the stock. If you ask me what this means, Consider a case where we have 1000 stock in Samsung S7 Edge Item and let's consider this stock can be used for two months. But there is a high demand for the Samsung S7 Edge phone and the price will be increased after a week and now we need to immediately increase the stock quantity .We have many branches around the world by editing the stock priority all our branches can view this from our web application and add the stock to the priority item; or let’s consider one more example as we have 1000 quantity stock and our customer makes a phone call and asks us to deliver 5000 stock quantity within a couple of days, in this case the manual priority will be more useful. Like this, in many scenarios it's good to have a manual priority for maintaining each item stock. This is a simple demo project so I didn’t add many field examples. We can also add the field like reorder date until we need to increase the stock quantity and notes for priority etc.

Now let’s add a new Item to our Inventory and click Save. Here we didn’t check the Priority Status while adding new Item, which means this item has good stock Quantity and it's not required for now to add more stock quantity.

ASP.NET Core

We can see as the Priority Status Column with red and green color with Checked and un checked boxes. When we add or edit the item if we have checked the Priority Status and saved then the item column will be displayed in green along with checkbox image. If the Item is not saved with priority status then it displays with red color. Here we used the green color to indicate the customer as this item has high priority and stock item needs to be increased with new Reorder Qty.

ASP.NET Core

Now let’s edit the item and set the item with priority status.

ASP.NET Core

When we save the same item with Priority Status checked, we can see the indicator with Green Color along with Checked image.

ASP.NET Core

Automatic Priority Status for Reorder Level

As I already told you, for reorder level we will be using the formula to indicate the customer for increasing the stock quantity with new reorder quantities.

Here we can see the Required Priority Status Column with Sky Blue and Light Green Color. Light Blue color indicates the Item is not needed for reorder and the Green color indicates the Item needs to be reordered.

For the automatic display I have set the formula as if StockQty>ReorderQty+50 then set the Required Priority Status as Light Blue Color which indicates the customer as the item is not needed for reorder and the if StockQty<=ReorderQty+50 then set the Required Priority Status as Light Green Color which indicates the customer as the item needed for immediate stock maintenance.

ASP.NET Core

From the above image for the newly added Samsung S7 Edge Item Stock Qty is 750 and Reorder Qty is 50 so the Required Priority Status is displayed as blue color.

Now let’s edit the item like below and set the Reorder Quantity as 700 and save to check the changes.

ASP.NET Core

Here now we can see the Required Priority Status color has been automatically changed as the Stock Qty is 750 and the Reorder Qty is 700.


Now let's see how to create this web application using Angular5,ASP.NET Core using EF and Web API.

Prerequisites

Make sure you have installed all the prerequisites in your computer. If not, then download and install all, one by one.

  1. First, download and install Visual Studio 2017 from this link.
  2. Download and install .NET Core 2.0
  3. Download and install Node.js v9.0 or above. I have installed V9.1.0 (Download link).

Code Part

Now, it’s time to create our first Angular5 and ASP.NET Core application.

Step 1 - Create a database and a table

We will be using our SQL Server database for our WEB API and EF. First, we create a database named InventoryPDB and a table as InventoryMaster. Here is the SQL script to create a database table and sample record insert query in our table. Run the query given below in your local SQL Server to create a database and a table to be used in our project. 

  1. USE MASTER       
  2. GO       
  3.        
  4. -- 1) Check for the Database Exists .If the database is exist then drop and create new DB       
  5. IF EXISTS (SELECT [nameFROM sys.databases WHERE [name] = 'InventoryPDB' )       
  6. DROP DATABASE InventoryPDB       
  7. GO       
  8.        
  9. CREATE DATABASE InventoryPDB       
  10. GO       
  11.        
  12. USE InventoryPDB       
  13. GO       
  14.        
  15.        
  16. -- 1) //////////// StudentMasters       
  17.        
  18. IF EXISTS ( SELECT [nameFROM sys.tables WHERE [name] = 'InventoryMaster' )       
  19. DROP TABLE InventoryMaster       
  20. GO       
  21.        
  22. CREATE TABLE [dbo].[InventoryMaster](       
  23.         [InventoryID] INT IDENTITY PRIMARY KEY,       
  24.         [ItemName] [varchar](100) NOT NULL,          
  25.         [StockQty]  int NOT NULL,          
  26.         [ReorderQty] int NOT NULL,          
  27.         [PriorityStatus] int NOT NULL     -- 0 for low and 1 for High   
  28. )       
  29.        
  30. -- insert sample data to Student Master table       
  31. INSERT INTO [InventoryMaster]   ([ItemName],[StockQty],[ReorderQty],[PriorityStatus])       
  32.      VALUES ('HardDisk',500,300,0)       
  33.        
  34. INSERT INTO [InventoryMaster]   ([ItemName],[StockQty],[ReorderQty],[PriorityStatus])       
  35.      VALUES ('Mouse',600,550,1)    
  36.   
  37.      INSERT INTO [InventoryMaster]   ([ItemName],[StockQty],[ReorderQty],[PriorityStatus])       
  38.      VALUES ('USB',3500,3000,0)    
  39.             
  40.             
  41.      select * from InventoryMaster    

Step 2- Create Angular5TemplateCore

After installing all the prerequisites listed above and Angular5TemplateCore, click Start >> Programs >> Visual Studio 2017 >> Visual Studio 2017, on your desktop.

Click New >> Project. Select Visual C# >> Select Angular5Core2. Enter your project name and click OK.

ASP.NET Core 

Once our project is created we can see in solution explorer with Angular5 sample components, html and app in ClientApp Folder along with Asp.NET Core Controllers and view folder.

ASP.NET Core


Here these files and folders are very similar to our ASP.NET Core Template Pack for Angular2.

Package.json File

If we open the package.Json file we can see all the dependencies needed for Angular5 and Angular cli has been already added by default. 

ASP.NET Core 


Adding Webpack in Package.json

In order to run our Angular5 Application we need to install webpack in our application. If the wepack is by default not added in our package.json file then we need to add it manually.Webpack is an open-source JavaScript module bundler. Webpack takes modules with dependencies and generates static assets representing those modules.to know more about Webpack click here.

Open our package.json file and add the below line under scripts

"postinstall": "webpack --config webpack.config.vendor.js"

ASP.NET Core 

Step 3 – Working with Model and Context Class

Adding connection string

To add the connection string with our SQL connection, open the “appsettings.json” file .Yes this is a JSON file and this file looks as shown below.

ASP.NET Core



In this appsettings.json file, I have added the connection string 
  1. "ConnectionStrings": {  
  2.     "DefaultConnection""Server=SQLSERVERNAME;Database=InventoryPDB;user id=SQLID;password=SQLPWD;Trusted_Connection=True;MultipleActiveResultSets=true;"  
  3.   }  

Note

Change SQL connection string, as per your local connection.



Next step is to create a folder named Data to create our model and DBContext class. 
ASP.NET Core

Creating Model class for Inventory

We can create a model by adding a new class file in our Data folder. Right Click Data folder and click Add>Click Class. Enter the class name as InventoryMasters and click Add. Now, in this class, we first create a property variable, add InventoryMaster. We will be using this in our WEB API controller.Note that here we will be adding the filed name same as our Database table column names.

ASP.NET Core 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Angular5Core2.Data  
  8. {  
  9.     public class InventoryMaster  
  10.     {  
  11.         [Key]  
  12.         public int InventoryID { get; set; }  
  13.   
  14.         [Required]  
  15.         [Display(Name = "ItemName")]  
  16.         public string ItemName { get; set; }  
  17.   
  18.         [Required]  
  19.         [Display(Name = "StockQty")]  
  20.         public int StockQty { get; set; }  
  21.   
  22.         [Required]  
  23.         [Display(Name = "ReorderQty")]  
  24.         public int ReorderQty { get; set; }  
  25.   
  26.         public int PriorityStatus { get; set; }  
  27.     }  
  28. }  

Creating Database Context

DBContext is Entity Framework class to establish a connection to the database

We can create a DBContext class by adding a new class file in our Data folder. Right click Data folder and click Add>Click Class. Enter the class name as InventroyContext and click Add. In this class, we inherit DbContext and created Dbset for our students table.

  1. using Microsoft.EntityFrameworkCore;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Angular5Core2.Data  
  8. {  
  9.     public class InventoryContext : DbContext  
  10.     {  
  11.         public InventoryContext(DbContextOptions<InventoryContext> options)    
  12.             :base(options) { }  
  13.         public InventoryContext() { }  
  14.         public DbSet<InventoryMaster> InventoryMaster { get; set; }  
  15.     }  
  16. }  

Startup.CS

Now, we need to add our database connection string and provider as SQL SERVER. To add this, we add the code given below in Startup.cs file under ConfigureServices method. 

  1. public void ConfigureServices(IServiceCollection services)  
  2.         {  
  3.             // Add Entity framework .    
  4.             services.AddDbContext<InventoryContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));  
  5.             services.AddMvc();  
  6.         }  

Step 4 - Creating Web API for CRUD operation

To create our WEB API Controller, right click Controllers folder. Click Add >> Click Controller. Select API Controller Empty and click on Add button to create our Web API.

ASP.NET Core

Enter the name as “InventoryMasterAPI.cs” and click Add.

As we all know, Web API is a simple and easy way to build HTTP Services for the Browsers and Mobiles.

Web API has four methods given below as Get/Post/Put and Delete.

  • Get is to request for the data. (Select)
  • Post is to create a data. (Insert)
  • Put is to update the data.
  • Delete is to delete data.

First we create the object for DBContext in our Web API class.

  1. [Produces("application/json")]  
  2.     [Route("api/InventoryMasterAPI")]  
  3.     public class InventoryMasterAPIController : Controller  
  4.     {  
  5.         private readonly InventoryContext _context;  
  6.   
  7.         public InventoryMasterAPIController(InventoryContext context)  
  8.         {  
  9.             _context = context;  
  10.         }  

Get Method (Select Operation)

Get Method is to request single item or list of items from our selected database. Here, we will get all Inventory information from InventoryMasters table.              

  1. // GET: api/InventoryMasterAPI  
  2.   
  3.     [HttpGet]  
  4.     [Route("Inventory")]  
  5.     public IEnumerable<InventoryMaster> GetInventoryMaster()  
  6.     {  
  7.         return _context.InventoryMaster;  
  8.   
  9.     }  

Post Method (Insert Operation)

Post Method will be used to insert the data to our database. In Post Method, we will also check if Inventory Id is already created and return the message. We will pass all inventory Master Column parameters to be inserted in to the Inventory Master table. 

  1. // POST: api/InventoryMasterAPI  
  2. [HttpPost]  
  3. public async Task<IActionResult> PostInventoryMaster([FromBody] InventoryMaster InventoryMaster)  
  4. {  
  5.     if (!ModelState.IsValid)  
  6.     {  
  7.         return BadRequest(ModelState);  
  8.     }  
  9.   
  10.     _context.InventoryMaster.Add(InventoryMaster);  
  11.     try  
  12.     {  
  13.         await _context.SaveChangesAsync();  
  14.     }  
  15.     catch (DbUpdateException)  
  16.     {  
  17.         if (InventoryMasterExists(InventoryMaster.InventoryID))  
  18.         {  
  19.             return new StatusCodeResult(StatusCodes.Status409Conflict);  
  20.         }  
  21.         else  
  22.         {  
  23.             throw;  
  24.         }  
  25.     }  
  26.   
  27.     return CreatedAtAction("GetInventoryMaster"new { id = InventoryMaster.InventoryID }, InventoryMaster);  
  28. }  
  29. private bool InventoryMasterExists(int id)  
  30. {  
  31.     return _context.InventoryMaster.Any(e => e.InventoryID == id);  
  32. }  

Put Method (Update Operation)

Put Method will be used to update the selected Inventory data to our database. In Put Method, we will pass InventoryID along with all other parameters for update. We pass the InventoryID to update the InventoryMaster Table by InventoryID. 

  1. // PUT: api/InventoryMasterAPI/2  
  2. [HttpPut("{id}")]  
  3. public async Task<IActionResult> PutInventoryMaster([FromRoute] int id, [FromBody] InventoryMaster InventoryMaster)  
  4. {  
  5.     if (!ModelState.IsValid)  
  6.     {  
  7.         return BadRequest(ModelState);  
  8.     }  
  9.   
  10.     if (id != InventoryMaster.InventoryID)  
  11.     {  
  12.         return BadRequest();  
  13.     }  
  14.   
  15.     _context.Entry(InventoryMaster).State = EntityState.Modified;  
  16.   
  17.     try  
  18.     {  
  19.         await _context.SaveChangesAsync();  
  20.     }  
  21.     catch (DbUpdateConcurrencyException)  
  22.     {  
  23.         if (!InventoryMasterExists(id))  
  24.         {  
  25.             return NotFound();  
  26.         }  
  27.         else  
  28.         {  
  29.             throw;  
  30.         }  
  31.     }  
  32.   
  33.     return NoContent();  
  34. }  

Delete Method (Delete Operation)

Delete Method will be used to delete the selected inventory data from our database. In Delete Method, we will pass InventoryID to delete the record.

  1. // DELETE: api/InventoryMasterAPI/2  
  2. [HttpDelete("{id}")]  
  3. public async Task<IActionResult> DeleteInventoryMaster([FromRoute] int id)  
  4. {  
  5.     if (!ModelState.IsValid)  
  6.     {  
  7.         return BadRequest(ModelState);  
  8.     }  
  9.   
  10.     InventoryMaster InventoryMaster = await _context.InventoryMaster.SingleOrDefaultAsync(m => m.InventoryID == id);  
  11.     if (InventoryMaster == null)  
  12.     {  
  13.         return NotFound();  
  14.     }  
  15.   
  16.     _context.InventoryMaster.Remove(InventoryMaster);  
  17.     await _context.SaveChangesAsync();  
  18.   
  19.     return Ok(InventoryMaster);  
  20. }  

To test Get Method, we can run our project and copy the GET method API path. Here, we can see our API path to get api/InventoryMasterAPI/Inventory/

Run the program and paste API path to test our output.

ASP.NET Core

Step 5: Working with Angular5

We create all Angular5 related Apps, Modules, Services, Components and HTML templates under ClientApp/App folder.

Here I’m using the existing Home Component and html for performing our Inventory CRUD operations. It will be always good to create a separate Services, Component and html file for each action. For demo project here, I’m using the existing home component and html.

Home.component

From the existing home component Type Script file,I have added all the functions to perform our CRUD operation for Inventory management.

First in the import section we have added the HTTP and FormsModule for working with WEB API Get/Post/Put/Delete and Forms to get and set input from the users.

In HomeComponent Class I have declared all the variables needed and created the separate function for Get/Post/put and Delete method. In get method I have passed the WEB API URL to get all the data from database and result JSON data to be bound in our html page. In Post and Put method I passed all the arguments by the user entered in the form to the WEB API to perform Update and Insert in database. Same like this we also created the delete method for deleting the inventory list from database by passing the Inventory ID as parameter to our Web API.

  1. import { Component, Input, Inject } from '@angular/core';  
  2. import { Http,Response, Headers, RequestOptions } from '@angular/http';  
  3. import { FormsModule } from '@angular/forms';  
  4.   
  5.   
  6. @Component({  
  7.     selector: 'home',  
  8.     templateUrl: './home.component.html'  
  9. })  
  10. export class HomeComponent {  
  11.     // to get the Student Details  
  12.     public Inventory: InventoryMaster[] = [];  
  13.     // to hide and Show Insert/Edit   
  14.     AddTable: Boolean = false;  
  15.     // To stored Student Informations for insert/Update and Delete  
  16.     public sInventoryID : number = 0;  
  17.     public sItemName = "";  
  18.     public sStockQty : number  = 0;  
  19.     public sReorderQty : number = 0;  
  20.     public sPriorityStatus: boolean = false;  
  21.   
  22.     //For display Edit and Delete Images  
  23.     public imgchk = require("./Images/chk.png");  
  24.     public imgunChk =  require("./Images/unchk.png");  
  25.     public bseUrl: string = "";  
  26.       
  27.     public schkName: string = "";  
  28.     myName: string;   
  29.     constructor(public http: Http, @Inject('BASE_URL')  baseUrl: string) {  
  30.         this.myName = "Shanu";  
  31.         this.AddTable = false;  
  32.         this.bseUrl = baseUrl;   
  33.         this.getData();  
  34.     }  
  35.   
  36.   
  37.     //to get all the Inventory data from Web API  
  38.     getData() {  
  39.   
  40.         this.http.get(this.bseUrl + 'api/InventoryMasterAPI/Inventory').subscribe(result => {  
  41.             this.Inventory = result.json();  
  42.         }, error => console.error(error));   
  43.            
  44.     }  
  45.   
  46.     // to show form for add new Student Information  
  47.     AddInventory() {   
  48.         this.AddTable = true;  
  49.         // To stored Student Informations for insert/Update and Delete  
  50.         this.sInventoryID = 0;  
  51.         this.sItemName = "";  
  52.         this.sStockQty = 50;  
  53.         this.sReorderQty = 50;  
  54.         this.sPriorityStatus = false;  
  55.     }  
  56.   
  57.     // to show form for edit Inventory Information  
  58.     editInventoryDetails(inventoryIDs : number, itemNames : string, stockQtys : number, reorderQtys : number , priorityStatus : number) {   
  59.         this.AddTable = true;  
  60.         this.sInventoryID = inventoryIDs;  
  61.         this.sItemName = itemNames;  
  62.         this.sStockQty = stockQtys;  
  63.         this.sReorderQty = reorderQtys;  
  64.         if (priorityStatus == 0)  
  65.         {  
  66.             this.sPriorityStatus = false;  
  67.         }  
  68.         else {  
  69.             this.sPriorityStatus = true;  
  70.         }  
  71.          
  72.     }  
  73.   
  74.     // If the InventoryId is 0 then insert the Inventory infromation using post and if the Inventory id is greater than 0 then edit using put mehod  
  75.     addInventoryDetails(inventoryIDs: number, itemNames: string, stockQtys: number, reorderQtys: number, priorityStatus: boolean) {  
  76.         var pStatus: number = 0;  
  77.           
  78.         this.schkName = priorityStatus.toString();  
  79.         if (this.schkName == "true") {  
  80.             pStatus = 1;  
  81.         }  
  82.         var headers = new Headers();  
  83.         headers.append('Content-Type''application/json; charset=utf-8');  
  84.         if (inventoryIDs == 0) {  
  85.             this.http.post(this.bseUrl + 'api/InventoryMasterAPI/', JSON.stringify({ InventoryID: inventoryIDs, ItemName: itemNames, StockQty: stockQtys, ReorderQty: reorderQtys, PriorityStatus: pStatus }),  
  86.                 { headers: headers }).subscribe(  
  87.                 response => {  
  88.                     this.getData();  
  89.   
  90.                 }, error => {  
  91.                 }  
  92.                 );   
  93.               
  94.         }  
  95.         else {  
  96.             this.http.put(this.bseUrl + 'api/InventoryMasterAPI/' + inventoryIDs, JSON.stringify({ InventoryID: inventoryIDs, ItemName: itemNames, StockQty: stockQtys, ReorderQty: reorderQtys, PriorityStatus: pStatus }), { headers: headers })  
  97.                 .subscribe(response => {  
  98.                     this.getData();  
  99.   
  100.                 }, error => {  
  101.                 }  
  102.                 );   
  103.              
  104.         }  
  105.         this.AddTable = false;  
  106.         //  
  107.         //  
  108.         //this.http.get(this.bseUrl + 'api/InventoryMasterAPI/Inventory').subscribe(result => {  
  109.         //    this.Inventory = result.json();  
  110.         //}, error => console.error(error));   
  111.     }  
  112.   
  113.     //to Delete the selected Inventory detail from database.  
  114.     deleteinventoryDetails(inventoryIDs: number) {  
  115.         var headers = new Headers();  
  116.         headers.append('Content-Type''application/json; charset=utf-8');  
  117.         this.http.delete(this.bseUrl + 'api/InventoryMasterAPI/' + inventoryIDs, { headers: headers }).subscribe(response => {  
  118.             this.getData();  
  119.   
  120.         }, error => {  
  121.         }  
  122.         );   
  123.   
  124.         //this.http.get(this.bseUrl + 'api/InventoryMasterAPI/Inventory').subscribe(result => {  
  125.         //    this.Inventory = result.json();  
  126.         //}, error => console.error(error));   
  127.     }  
  128.   
  129.     closeEdits() {  
  130.         this.AddTable = false;  
  131.         // To stored Student Informations for insert/Update and Delete  
  132.         this.sInventoryID = 0;  
  133.         this.sItemName = "";  
  134.         this.sStockQty = 50;  
  135.         this.sReorderQty = 50;  
  136.         this.sPriorityStatus = false;  
  137.     }  
  138. }  
  139.   
  140. export interface InventoryMaster {  
  141.     inventoryID: number;  
  142.     itemName: string;  
  143.     stockQty: number;  
  144.     reorderQty: number;  
  145.     priorityStatus: number;  
  146. }   

HTML Template file

Here we are using the home.component.html file to perform our CRUD operation for Inventory Management.

  1. <div align="center">  
  2.     <h1> ASP.NET Core,Angular5 CRUD for Invetory Management Priority based Reorder Level System, WEB API and EF   </h1>  
  3. </div>  
  4. <div class="column">  
  5.     <h2>Created by : {{myName}}</h2>  
  6.       
  7. </div>  
  8. <hr style="height: 1px;color: #123455;background-color: #d55500;border: none;color: #d55500;" />  
  9. <p *ngIf="!Inventory"><em>Loading Inventory Details please Wait ! ...</em></p>  
  10. <table id="tblContainer" style='width: 99%;table-layout:fixed;'>  
  11.     <tr>  
  12.         <td>  
  13.             <table style="background-color:#FFFFFF; border: dashed 3px #FFFFFF; padding: 5px;width: 99%;table-layout:fixed;" cellpadding="2"  
  14.                    cellspacing="2">  
  15.                 <tr style="height: 30px;  color:#123455 ;border: solid 1px #659EC7;">  
  16.                     <td width="40px"> </td>  
  17.                     <td width="50%">  
  18.                         <h1> Add New Inventory Information <strong style="color:#0094ff"> </strong></h1>  
  19.   
  20.                     </td>  
  21.                     <td align="right">  
  22.                         <button (click)=AddInventory() style="background-color:#f83500;color:#FFFFFF;font-size:large;width:260px;height:50px;  
  23.                               border-color:#a2aabe;border-style:dashed;border-width:2px;">  
  24.                             Add New Inventory Info  
  25.                         </button>  
  26.                            
  27.                     </td>  
  28.                 </tr>  
  29.             </table>  
  30.   
  31.         </td>  
  32.     </tr>  
  33.     <tr>  
  34.         <td>  
  35.             <hr style="height: 1px;color: #123455;background-color: #d55500;border: none;color: #d55500;" />  
  36.         </td>  
  37.     </tr>  
  38.     <tr *ngIf="AddTable">  
  39.         <td >  
  40.             <table>  
  41.                 <tr>  
  42.                     <td>  
  43.                         <table style="background-color:#FFFFFF; border: dashed 3px #6D7B8D; padding :5px;width :99%;table-layout:fixed;" cellpadding="2" cellspacing="2">  
  44.                             <tr style="height: 30px; background-color:#336699 ; color:#FFFFFF ;border: solid 1px #659EC7;">  
  45.                                 <td width="40">  
  46.                                        
  47.                                 </td>  
  48.                                 <td>  
  49.                                     <h2>Insert/Edit Inventory Details : </h2>  
  50.                                 </td>  
  51.                             </tr>  
  52.                             <tr>  
  53.                                 <td width="100">  
  54.                                        
  55.                                 </td>  
  56.                                 <td>  
  57.                                     <table style="color:#9F000F;font-size:large; padding :5px;" cellpadding="12" cellspacing="16">  
  58.                                         <tr>  
  59.                                             <td><b>Inventory ID: </b> </td>  
  60.                                             <td>  
  61.                                                 <input type="number" #InventoryID (ngModel)="sInventoryID" value="{{sInventoryID}}" style="background-color:tan" readonly>  
  62.                                             </td>  
  63.                                             <td width="20"> </td>  
  64.                                             <td><b>Item Name: </b> </td>  
  65.                                             <td>  
  66.                                                 <input type="text" #ItemName (ngModel)="sItemName" value="{{sItemName}}" required>  
  67.                                             </td>  
  68.                                             <td></td>  
  69.                                         </tr>  
  70.                                         <tr>  
  71.                                             <td><b>Stock Quantity: </b> </td>  
  72.                                             <td>  
  73.                                                 <input type="number" #StockQty (ngModel)="sStockQty" value="{{sStockQty}}" min="50" required>  
  74.                                             </td>  
  75.                                             <td width="20"> </td>  
  76.                                             <td><b>Reorder Quantity: </b> </td>  
  77.                                             <td>  
  78.                                                 <input type="number" #ReorderQty (ngModel)="sReorderQty" value="{{sReorderQty}}" min="50" required>  
  79.                                             </td>  
  80.                                             <td></td>  
  81.                                         </tr>  
  82.                                         <tr>  
  83.                                             <td><b>Priority Status: </b> </td>  
  84.                                             <td>  
  85.                                                 <input type="checkbox" #PriorityStatus (ngModel)="sPriorityStatus" value="{{sPriorityStatus}}" [checked]="sPriorityStatus"  
  86.                                                        (change)="sPriorityStatus = !sPriorityStatus" >  
  87.                                                 <!--<input type="text" #chkName (ngModel)="schkName" value="{{schkName}}">-->  
  88.                                             </td>  
  89.                                             <td width="20"> </td>  
  90.                                             <td align="right" colspan="2">  
  91.                                                 <button (click)=addInventoryDetails(InventoryID.value,ItemName.value,StockQty.value,ReorderQty.value,PriorityStatus.value) style="background-color:#428d28;color:#FFFFFF;font-size:large;width:220px;  
  92.                               border-color:#a2aabe;border-style:dashed;border-width:2px;">  
  93.                                                     Save  
  94.                                                 </button>  
  95.                                             </td>  
  96.                                             <td>  
  97.                                                      
  98.                                                 <button (click)=closeEdits() style="background-color:#334668;color:#FFFFFF;font-size:large;width:180px;  
  99.                               border-color:#a2aabe;border-style:dashed;border-width:2px;">  
  100.                                                     Close  
  101.                                                 </button>  
  102.                                             </td>  
  103.                                         </tr>  
  104.                                     </table>  
  105.                                 </td>  
  106.                             </tr>  
  107.                         </table>  
  108.                     </td>  
  109.                 </tr>  
  110.                 <tr>  
  111.                     <td colspan="2">  
  112.                         <hr style="height: 1px;color: #123455;background-color: #d55500;border: none;color: #d55500;" />  
  113.                     </td>  
  114.                 </tr>  
  115.             </table>  
  116.         </td>  
  117.     </tr>  
  118.     <tr>  
  119.         <td>  
  120.   
  121.             <table class='table' style="background-color:#FFFFFF; border:2px #6D7B8D; padding:5px;width:99%;table-layout:fixed;" cellpadding="2" cellspacing="2" *ngIf="Inventory">  
  122.                 <tr style="height: 30px; background-color:#336699 ; color:#FFFFFF ;border: solid 1px #659EC7;">  
  123.                     <td width="70" align="center">Edit</td>  
  124.                     <td width="70" align="center">Delete</td>  
  125.                     <td width="70" align="center">Inventory ID</td>  
  126.                     <td width="120" align="center">Item Name</td>  
  127.                     <td width="120" align="center">StockQty</td>  
  128.                     <td width="120" align="center">ReorderQty</td>  
  129.                     <td width="120" align="center">Priority Status</td>  
  130.                     <td width="120" align="center">Required Priority Status</td>  
  131.                 </tr>  
  132.                 <tbody *ngFor="let INVY  of Inventory">  
  133.                     <tr>  
  134.                         <td align="center" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">  
  135.                             <span style="color:#9F000F">  
  136.   
  137.                                 <button (click)=editInventoryDetails(INVY.inventoryID,INVY.itemName,INVY.stockQty,INVY.reorderQty,INVY.priorityStatus)  
  138.                                         style="background-color:#0d254f;color:#FFFFFF;font-size:large;width:80px;  
  139.                               border-color:#a2aabe;border-style:dashed;border-width:2px;">  
  140.                                     Edit  
  141.                                 </button>  
  142.                                 <!-- <img src="{{imgEdit}}"  style="height:32px;width:32px" (click)=editStudentsDetails(StudentMasters.stdID,StudentMasters.stdName,StudentMasters.email,StudentMasters.phone,StudentMasters.address)>-->  
  143.                             </span>  
  144.                         </td>  
  145.                         <td align="center" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">  
  146.                             <span style="color:#9F000F">  
  147.                                 <button (click)=deleteinventoryDetails(INVY.inventoryID)  
  148.                                         style="background-color:#0d254f;color:#FFFFFF;font-size:large;width:80px;  
  149.                               border-color:#a2aabe;border-style:dashed;border-width:2px;">  
  150.                                     Delete  
  151.                                 </button>  
  152.                                 <!-- <img src="{{imgDelete}}" style="height:32px;width:32px" (click)=deleteStudentsDetails(StudentMasters.stdID)>-->  
  153.                             </span>  
  154.                         </td>  
  155.                         <td align="center" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">  
  156.                             <span style="color:#9F000F">{{INVY.inventoryID}}</span>  
  157.                         </td>  
  158.                         <td align="left" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">  
  159.                             <span style="color:#9F000F">{{INVY.itemName}}</span>  
  160.                         </td>  
  161.   
  162.                         <td align="right" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">  
  163.                             <span style="color:#9F000F">{{INVY.stockQty}}</span>  
  164.                         </td>  
  165.   
  166.                         <td align="right" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">  
  167.                             <span style="color:#9F000F">{{INVY.reorderQty}}</span>  
  168.                         </td>  
  169.   
  170.                         <td align="center" [ngStyle]="INVY.priorityStatus==1 && {'background-color': 'green'}  || INVY.priorityStatus==0 && {'background-color': 'red'} " style="height:42px;width:42px">  
  171.                             <!--<span style="color:#FFFFFF">{{INVY.priorityStatus}}</span>-->    
  172.                             <div  [ngStyle]="INVY.priorityStatus==1 && {'background-image': 'url(' + imgchk + ')'}  || INVY.priorityStatus==0 && {'background-image': 'url(' + imgunChk + ')'} " style="background-repeat: no-repeat;height:38px;width:38px" >  
  173.                                    
  174.                             </div>  
  175.                                
  176.                         </td>  
  177.   
  178.                         <td align="left" [ngStyle]="INVY.stockQty>INVY.reorderQty+50 && {'background-color': '#0094ff'}  || INVY.stockQty<=INVY.reorderQty+50  && {'background-color': '#e5e800'} " style="border: solid 1px #ffffff; padding: 5px;table-layout:fixed;">  
  179.                             <!--<span style="color:#FFFFFF">{{INVY.priorityStatus}}</span>-->  
  180.                         </td>  
  181.                     </tr>  
  182.                 </tbody>  
  183.             </table>  
  184.         </td>  
  185.     </tr>  
  186. </table>  

Manual Priority setting for Reorder Level logic

When user adds or edits the Inventory Master and if the Priority Status is checked then I will insert the staus as 1 in database and if the Priority Status is unchecked then I will save the status as 0. In our html design I will check for this PriorityStatus value and if the value is returned as 1 then I will set the table td cell background color as Green and set the image as Checked image.

  1. <td align="center" [ngStyle]="INVY.priorityStatus==1 && {'background-color': 'green'}  || INVY.priorityStatus==0 && {'background-color': 'red'} " style="height:42px;width:42px">  
  2.                             <!--<span style="color:#FFFFFF">{{INVY.priorityStatus}}</span>-->    
  3.                             <div  [ngStyle]="INVY.priorityStatus==1 && {'background-image': 'url(' + imgchk + ')'}  || INVY.priorityStatus==0 && {'background-image': 'url(' + imgunChk + ')'} " style="background-repeat: no-repeat;height:38px;width:38px" >                                   
  4.                             </div>  
  5.                                
  6.                         </td>  

Automatic Priority Status for Reorder Level logic

In the Required Priority Status column, I checked for the stockQty>reorderQty+50 and if it’s true then I set the td background color as LightBlue else set the color as LightGreen.

  1. <td align="left" [ngStyle]="INVY.stockQty>INVY.reorderQty+50 && {'background-color': '#0094ff'}  || INVY.stockQty<=INVY.reorderQty+50  && {'background-color': '#e5e800'} " style="border: solid 1px #ffffff; padding: 5px;table-layout:fixed;">  
  2.                             <!--<span style="color:#FFFFFF">{{INVY.priorityStatus}}</span>-->  
  3.                         </td>  
ASP.NET Core

Build and run the application

Build and run the application and you can see the Home page with Inventory Management CRUD page.

Note

First, create a database and table in your SQL Server. You can run use the above table creation script to create Database and tables with sample Insert. Don’t forget to change the database connection string from “appsettings.json”.