Motive
In this blog, we will understand the concepts of Angular 2, Web API, and SQL Server.
Introduction - Angular 2
- Angular (commonly referred to as "Angular 2+" or "Angular v2 and above") is a TypeScript-based product of Google and created by a community of individuals and corporations.
- This is not an MVC framework.
- It is an open-source front-end web application platform.
WebAPI
- A server-side web API is a programmatic interface consisting of one or more publicly exposed endpoints to a defined request–response message system, typically expressed in JSON or XML, which is exposed via the web—most commonly by means of an HTTP-based web server
OverView
This is a step-by-step solution to set up Angular 2 with your Visual Studio 2017. In this project, I will create a simple web application for beginners. This application is used to bind the drop-down. We are just five steps away from Angular 2 application.
This is a step-by-step solution to set up Angular 2 with your Visual Studio 2017. In this project, I will create a simple web application for beginners. This application is used to bind the drop-down. We are just five steps away from Angular 2 application.
- Step 1 - Create a database (SQL Server).
- Step 2 - Create an API (web API)
- Step 3 - Create a connection with SQL and get data from SQL using Web API.
- Step 4 - Setup angular2 with Web API.
- Step 5 - Bind drop-down using Angular 2.
Needs
- Node JS (node version is 4.6.x or greater)
- NPM (Node Package Manager)( version 3.x.x or greater)
- Visual Studio 2017.
- TypeScript (2.2 or greater).
Now, set up Angular 2 files with Visual Studio.
Step 1
- Create table Country(CountryId int Identity (1,1),CountryName varchar(50))
- insert intoCityvalues(India);
- insert intoCityvalues('Canada');
- insert intoCityvalues(Australia);

Step 2 - CREATE WEB API
- Open Visual Studio 2017 and create a new project.
- Go to File=>New=> Project=>Select Web from Right Side =>Select ASP .Net Web Application (.NET FrameWork).
- Give a name to the project and select the directory where you want to create the project.

- Now, you will see a new window. Please select MVC project.

Step 3 - Create a connection with SQL and get data from SQL using Web API.
You can do it with only three steps.
- SqlConnetion
- Model
- Buisnesslogic
The following code is used to GET the data from database.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using System.Data;
- using System.Data.SqlClient;
- namespace WebApplication1.api {
- [RoutePrefix("api/DropDownBind")]
- publicclassDropDownBindController: ApiController {
- /// Method for calling getcountrylist and return list of country to service of angular
- [HttpGet]
- public IHttpActionResult GetCountry() {
- var objlist = GetCountryList();
- return Ok(objlist.ToList());
- }
- /// Method to get from Database
- public List < CountryModel > GetCountryList() {
- SqlConnection con = new SqlConnection("Data Source=ServerName;Initial Catalog=DatabaseName;Persist Security Info=True;User ID=sa;Password=123");
- con.Open();
- string sql = null;
- SqlCommand cmd = new SqlCommand(sql, con);
- SqlDataAdapter sd = new SqlDataAdapter(cmd);
- List < CountryModel > listdetail = new List < CountryModel > ();
- try {
- cmd.CommandText = "Select * from Country";
- sd.SelectCommand = cmd;
- DataTable dt = new DataTable();
- sd.Fill(dt);
- for (int i = 0; i < dt.Rows.Count; i++) {
- CountryModel md1 = new CountryModel();
- md1.CountryName = dt.Rows[i]["CountryName"].ToString();
- md1.CountryId = Convert.ToInt16(dt.Rows[i]["CountryId"].ToString());
- listdetail.Add(md1);
- }
- } catch (Exception ex) {
- Console.WriteLine(ex);
- }
- return listdetail;
- }
- // Model or Properties for Country
- publicclassCountryModel {
- publicint CountryId {
- get;
- set;
- }
- publicstring CountryName {
- get;
- set;
- }
- }
- }
- }
Please change your connection string.
Please check it in the following images.










dd ddPosted Jul 23, 2019, 3:42 PM
I am connect my publish API, but data not displayed on the UI. Please help me.. how to connect api from out side..
dd ddPosted Jul 23, 2019, 3:40 PM
How to debug Angular2 application??