Consume (Call) postal pin code API using C# and VB.Net in ASP.Net

Last Reply 9 days ago By dharmendr

Posted 9 days ago

Please help me out to consume this API to a Gridview by sending PIN Code in asp.net C# Webforms

https://api.postalpincode.in/pincode/110001

Only this Part to be bind in Gridview

"PostOffice": [
       {
           "Name": "Basudevpur",
           "Description": null,
           "BranchType": "Branch Post Office",
           "DeliveryStatus": "Delivery",
           "Circle": "Odisha",
           "District": "Cuttack",
           "Division": "Cuttack North",
           "Region": "Bhubaneswar HQ",
           "Block": "Salipur",
           "State": "Odisha",
           "Country": "India",
           "Pincode": "754204"
       },

 

Posted 9 days ago Modified on 9 days ago

Hi Amitabha,

Check this example. Now please take its reference and correct your code.

HTML

<asp:TextBox runat="server" ID="txtPin" />
<asp:Button Text="Get PostOffice" runat="server" OnClick="GetDetails" /><hr />
<asp:GridView runat="server" ID="gvDetails"></asp:GridView>

Namespaces

C#

using System.Collections.Generic;
using Newtonsoft.Json;
using RestSharp;

VB.Net

Imports System.Collections.Generic
Imports Newtonsoft.Json
Imports RestSharp

Code

C#

protected void GetDetails(object sender, EventArgs e)
{
    string pin = txtPin.Text.Trim();
    RestClient restClient = new RestClient(string.Format("{0}{1}", "https://api.postalpincode.in/pincode/", pin));
    restClient.Timeout = -1;
    RestRequest request = new RestRequest();
    IRestResponse result = restClient.Execute(request);
    PostOfficeDetails[] pd = JsonConvert.DeserializeObject<PostOfficeDetails[]>(result.Content);
    this.gvDetails.DataSource = pd[0].PostOffice;
    this.gvDetails.DataBind();
}

public class PostOfficeDetails
{
    public string Message { get; set; }
    public string Status { get; set; }
    public List<PostOffice> PostOffice { get; set; }
}

public class PostOffice
{
    public string Name { get; set; }
    public string Description { get; set; }
    public string BranchType { get; set; }
    public string DeliveryStatus { get; set; }
    public string Circle { get; set; }
    public string District { get; set; }
    public string Division { get; set; }
    public string Region { get; set; }
    public string Block { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
    public string Pincode { get; set; }
}

VB.Net

Protected Sub GetDetails(ByVal sender As Object, ByVal e As EventArgs)
    Dim pin As String = txtPin.Text.Trim()
    Dim restClient As RestClient = New RestClient(String.Format("{0}{1}", "https://api.postalpincode.in/pincode/", pin))
    restClient.Timeout = -1
    Dim request As RestRequest = New RestRequest()
    Dim result As IRestResponse = restClient.Execute(request)
    Dim pd As PostOfficeDetails() = JsonConvert.DeserializeObject(Of PostOfficeDetails())(result.Content)
    Me.gvDetails.DataSource = pd(0).PostOffice
    Me.gvDetails.DataBind()
End Sub

Public Class PostOfficeDetails
    Public Property Message As String
    Public Property Status As String
    Public Property PostOffice As List(Of PostOffice)
End Class

Public Class PostOffice
    Public Property Name As String
    Public Property Description As String
    Public Property BranchType As String
    Public Property DeliveryStatus As String
    Public Property Circle As String
    Public Property District As String
    Public Property Division As String
    Public Property Region As String
    Public Property Block As String
    Public Property State As String
    Public Property Country As String
    Public Property Pincode As String
End Class

Screenshot