CRUD Web API Service Call Function In Windows Applications

Introduction

 
The API Web service call function in windows application is highly secured. This is because of no data available on the local computer, so all are services oriented. The records insert and update, delete, select has been [HttpGet], [HttpPut], [HttpPost] and [HttpDelete] oriented functions.
 
Step 1
 
Create a new project.
 
 
Step 2
 
Choose Windows Desktop, and Select the Windows Forms App (.NET Framework)
 
 
Step 3
 
Design the Windows application.
 
 
Step 4 - [HttpGet], [HttpPut], [HttpPost] and [HttpDelete]
 
Declare the Headers and add on dll Reference (System.Net.Http.Formatting, Newtonsoft.json)
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Windows.Forms;  
  5. using System.Net.Http;  
  6. using System.Net.Http.Headers;  
  7. using System.Net;  
  8. using System.IO;  
  9. using Newtonsoft.Json;   
Get all the data in Gridview List: [HttpGet]
  1. private void GetData()    
  2. {    
  3.        
  4.     HttpClient client = new HttpClient();               
  5.     client.BaseAddress = new Uri("http://localhost:2514/");    
  6.     client.DefaultRequestHeaders.Accept.Add(    
  7.         new MediaTypeWithQualityHeaderValue("application/json"));    
  8.   
  9.     HttpResponseMessage response = client.GetAsync("api/values").Result;    
  10.   
  11.     if (response.IsSuccessStatusCode)    
  12.     {    
  13.         var users = response.Content.ReadAsAsync<IEnumerable<Users>>().Result;    
  14.         dataGridView1.DataSource = users;    
  15.     }    
  16.     else    
  17.     {    
  18.         MessageBox.Show("Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase);    
  19.     }    
  20.   
  21. }    
Call the Get data Function in Form1() 
  1. public Form1()  
  2. {  
  3.     InitializeComponent();  
  4.     GetData();  
  5. }   
Refer the below screenshot for thedata grid view list:
 
  
Post the data in Gridview List: [HttpPost]
  1. private void button1_Click(object sender, EventArgs e)  
  2. {       
  3.     Users objProduct = new Users();  
  4.     objProduct.Username = textBox1.Text;  
  5.     objProduct.Mobile = textBox2.Text;  
  6.     string json = JsonConvert.SerializeObject(objProduct);  
  7.   
  8.     var baseAddress = "http://localhost:2514/api/values/GetMobileInsert?Username=" + textBox1.Text + "&Mobile=" + textBox2.Text + "";  
  9.   
  10.     var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));  
  11.     http.Accept = "application/json";  
  12.     http.ContentType = "application/json";  
  13.     http.Method = "POST";  
  14.   
  15.     string parsedContent = json;  
  16.     ASCIIEncoding encoding = new ASCIIEncoding();  
  17.     Byte[] bytes = encoding.GetBytes(parsedContent);  
  18.   
  19.     Stream newStream = http.GetRequestStream();  
  20.     newStream.Write(bytes, 0, bytes.Length);  
  21.     newStream.Close();  
  22.   
  23.     var response = http.GetResponse();  
  24.   
  25.     var stream = response.GetResponseStream();  
  26.   
  27.     GetData();  
  28. }  
Refer to the below screenshot for the data grid view Insert:
 
 
Put the data in Gridview List: [HttpPut]
  1. private void button2_Click(object sender, EventArgs e)  
  2. {  
  3.     Users objProduct = new Users();  
  4.     objProduct.Username = textBox1.Text;  
  5.     objProduct.Mobile = textBox2.Text;  
  6.     string json = JsonConvert.SerializeObject(objProduct);  
  7.   
  8.     var baseAddress = "http://localhost:2514/api/values/GetMobileUpdate?ID="+textBox3.Text+"&Username=" + textBox1.Text + "&Mobile=" + textBox2.Text + "";  
  9.   
  10.     var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));  
  11.     http.Accept = "application/json";  
  12.     http.ContentType = "application/json";  
  13.     http.Method = "PUT";  
  14.   
  15.     string parsedContent = json;  
  16.     ASCIIEncoding encoding = new ASCIIEncoding();  
  17.     Byte[] bytes = encoding.GetBytes(parsedContent);  
  18.   
  19.     Stream newStream = http.GetRequestStream();  
  20.     newStream.Write(bytes, 0, bytes.Length);  
  21.     newStream.Close();  
  22.   
  23.     var response = http.GetResponse();  
  24.   
  25.     var stream = response.GetResponseStream();  
  26.   
  27.     GetData();  
  28. }  
Refer to the below screenshot for data grid view Update:
 
  
Get the Randomly selected value in data GridView:
  1. private void GetselectData()  
  2. {  
  3.     HttpClient client = new HttpClient();  
  4.     client.BaseAddress = new Uri("http://localhost:2514/");  
  5.     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  6.   
  7.     var id = textBox3.Text;  
  8.   
  9.     var url = "api/values/GetMobile?ID=" + id;  
  10.   
  11.     HttpResponseMessage response = client.GetAsync(url).Result;  
  12.   
  13.     if (response.IsSuccessStatusCode)  
  14.     {  
  15.         var result = response.Content.ReadAsStringAsync().Result;  
  16.         var s = Newtonsoft.Json.JsonConvert.DeserializeObject(result);  
  17.         MessageBox.Show(s.ToString());  
  18.         dataGridView1.DataSource = s;  
  19.     }  
  20.     else  
  21.     {  
  22.         MessageBox.Show("Fail");  
  23.     }  
  24.   
  25. }  
 Refer to the below screenshot for a randomly selected value in data GridView:
 
 
Delete the data in Gridview List: [HttpDelete]
  1. private void button4_Click(object sender, EventArgs e)  
  2. {  
  3.     Users objProduct = new Users();  
  4.     objProduct.Username = textBox1.Text;  
  5.     objProduct.Mobile = textBox2.Text;  
  6.     string json = JsonConvert.SerializeObject(objProduct);  
  7.   
  8.     var baseAddress = "http://localhost:2514/api/values?ID=" + textBox3.Text + "";  
  9.   
  10.     var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));  
  11.     http.Accept = "application/json";  
  12.     http.ContentType = "application/json";  
  13.     http.Method = "DELETE";  
  14.   
  15.     string parsedContent = json;  
  16.     ASCIIEncoding encoding = new ASCIIEncoding();  
  17.     Byte[] bytes = encoding.GetBytes(parsedContent);  
  18.   
  19.     Stream newStream = http.GetRequestStream();  
  20.     newStream.Write(bytes, 0, bytes.Length);  
  21.     newStream.Close();  
  22.   
  23.     var response = http.GetResponse();  
  24.   
  25.     var stream = response.GetResponseStream();  
  26.   
  27.     GetData();  
  28. }  
 Refer to the below screenshot for data grid view Delete:
 
 

Summary

 
The web API call function is used in windows applications and is one of the best options for high-level data security. I hope this method helps you to API Web service call in a Windows application.