C#

Features ›› Market Data ›› getExchangeRate ›› Sample Code ››
Parent Previous Next

Get Exchange Rate


1.getExchangeRate.cs


These five packages are required to be used.




Step 1. Create class file to store content of the web response.



2.getExchangeRate.aspx.cs


These 13 packages are required to be used.



1). Display the currency code into dropdownlist so that the user can choose easily. Display the list of currency in a gridview. getCurrencyList function can be referred to in the reference data section.




Step 2. Store details entered by the user.



Step 2. Create URL which will be post to the server. User inputs need to be stored in the class created, and serialize into JSON format. In this function, both header and content is needed




Step 4. Post to the server, and store response. URL is sent using web request function, and a response will be send back from the server. The response need to be deserialized into the ‘getExchangeRate’ class.





Step 5. Obtain error code. If error code is ‘010000’ which means invocation successful, populate attributes of the ‘getExchangeRate’ class into the Label. Else, we will display the ErrorText and ErrorDetails.




Overview of getExchangeRate.cs


using Newtonsoft.Json;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace Demo

{

   public class getExchangeRateRootObject

   {

       public getExchangeRateContent Content { get; set; }

   }


   public class getExchangeRateContent

   {

       public getExchangeRateServiceResponse ServiceResponse { get; set; }

   }


   public class getExchangeRateServiceResponse

   {

       [JsonProperty("FX_SpotRate_Read-Response")]

       public FXSpotRateReadResponse FXSpotRate { get; set; }

       public getExchangeRateServiceRespHeader ServiceRespHeader { get; set; }

   }  

   public class FXSpotRateReadResponse

   {

       public string Rate { get; set; }

   }


   public class getExchangeRateServiceRespHeader

   {

       public string ErrorText { get; set; }

       public object ErrorDetails { get; set; }

       public string GlobalErrorID { get; set; }

   }

}


Download



Overview of getExchangeRate.aspx.cs


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using DotNetOpenAuth.OAuth2;

using Newtonsoft.Json;

using System.Collections.Specialized;

using System.Configuration;

using System.Net;

using System.IO;

using System.Windows.Forms;


namespace Demo

{

   public partial class GetExchangeRate : System.Web.UI.Page

   {

       protected void Page_Load(object sender, EventArgs e)

       {

           if (!Page.IsPostBack)

           {

               var p = utilities.getCurrencyList();


               ddlbaseCurrency.Items.Add("Please choose a Base Currency");

               ddlquoteCurrency.Items.Add("Please choose a Quote Currency");


               for (int i = 0; i < p.Count(); i++)

               {

                   ddlbaseCurrency.Items.Add(p[i].CurrencyCode);

                   ddlquoteCurrency.Items.Add(p[i].CurrencyCode);

               }


               int counter = p.Count();

               DataTable dt = new DataTable();

               DataColumn col1 = new DataColumn("  ", typeof(string));

               DataColumn col2 = new DataColumn("Country Name", typeof(string));

               DataColumn col3 = new DataColumn("Currency Code", typeof(string));

               DataColumn col4 = new DataColumn("Currency Name", typeof(string));

               dt.Columns.Add(col1);

               dt.Columns.Add(col2);

               dt.Columns.Add(col3);

               dt.Columns.Add(col4);


               // put data into the data table

               for (int i = 0; i <= counter - 1; i++)

               {

                   DataRow row = dt.NewRow();

                   dt.Rows.Add(row);

                   dt.Rows[i][col1] = i + 1;

                   dt.Rows[i][col2] = p[i].CountryName.ToString();

                   dt.Rows[i][col3] = p[i].CurrencyCode.ToString();

                   dt.Rows[i][col4] = p[i].CurrencyName.ToString();


               }

               gvCurrency.DataSource = dt;

               gvCurrency.DataBind();

           }

       }


       protected void btnSend_Click(object sender, EventArgs e)

       {

           if (Page.IsValid)

           {      

               try

               {

                   string baseCurrency = ddlbaseCurrency.SelectedItem.ToString();

                   string quoteCurrency = ddlquoteCurrency.SelectedItem.ToString();


                   var header = new HeaderJson();

                   header.userID = "";

                   header.PIN = "";

                   header.serviceName = "getExchangeRate";

                   header.OTP = "";

                   var headerJson = new HeaderJsonObject();

                   headerJson.Header = header;

                   string strHeader = JsonConvert.SerializeObject(headerJson);


                   var content = new ContentJson();

                   content.baseCurrency = baseCurrency;

                   content.quoteCurrency = quoteCurrency;                  

                   var contentJson = new ContentJsonObject();

                   contentJson.Content = content;

                   string strContent = JsonConvert.SerializeObject(contentJson);


                   string url = "http://tbankonline.com/SMUtBank_API/Gateway?Header=" + strHeader + "&Content=" + strContent;


                   var webRequest = (HttpWebRequest)WebRequest.Create(url);

                   webRequest.ContentType = "application/json";

                   webRequest.Method = "POST";


                   var result = "";

                   var httpResponse = (HttpWebResponse)webRequest.GetResponse();

                   using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))

                   {

                       result = streamReader.ReadToEnd();

                   }


                   getExchangeRateRootObject rate = new getExchangeRateRootObject();

                   rate = JsonConvert.DeserializeObject<getExchangeRateRootObject>(result);


                   string globalErrorID = rate.Content.ServiceResponse.ServiceRespHeader.GlobalErrorID.ToString();


                   if (globalErrorID == "010000")

                   {

                       lblRate.Text = rate.Content.ServiceResponse.FXSpotRate.Rate.ToString();

                   }


                   else

                   {

                       lblTest.Text = rate.Content.ServiceResponse.ServiceRespHeader.ErrorText.ToString();

                       string errorMessage = rate.Content.ServiceResponse.ServiceRespHeader.ErrorDetails.ToString();

                       lblErrorMessage.Text = errorMessage;

                   }

               }

               catch (Exception ex)

               {

                   lblExceptionMsg.Text = ex.ToString();

               }

           }

       }

   }

}


Download

Created with the Personal Edition of HelpNDoc: Free PDF documentation generator