C#

Features ›› Wealth ›› getFXForwardContracts ›› Sample Code ››
Parent Previous Next

Get FX Forward Contracts


1. getFXForwardContracts.cs


These five packages are required to be used.




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





2. GetFXForwardContracts.aspx.cs


These 15 packages are required to be used.




Code in Common

(1). Check if user id and password is inside session. When the user id and password is inside user session, it will automatically populate into the user id and pin textbox, so that the user need not manually type his user id and password repeatedly.




Step 2. Store details entered by the user. If user id, password, and OTP exist in session, user need not enter again (refer to common stuff).




Step 3. 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, only header 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 ‘getFXForwardContracts’ class.




Step 5. Obtain error code. If error code is ‘010000’ which means invocation successful, populate attributes of the ‘getFXForwardContracts’ class into the Label and Gridview. If error code is '010041', it means OTP has expired, and we will use the newOTP() function to prompt the user for the OTP again. Else, we will display the ErrorText and ErrorDetails.


The newOTP() function can be referred to under Common Stuff Section




Overview of getFXForwardContracts.cs


using Newtonsoft.Json;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace Demo

{

   public class getFXForwardContractsFXForwardContract

   {

       public string open_date { get; set; }

       public string period { get; set; }

       public string amount { get; set; }

       public string maturity_date { get; set; }

       public string spot_rate { get; set; }

       public string quoteCurrencyAccountID { get; set; }

       public string baseCurrencyAccountID { get; set; }

       public string baseCurrency { get; set; }

       public string baseCurrencyInterestRate { get; set; }

       public string quoteCurrency { get; set; }

       public string referenceNumber { get; set; }

       public string customerID { get; set; }

       public string forward_rate { get; set; }

       public string quoteCurrencyInterestRate { get; set; }

       public string FX_ForwardID { get; set; }

       public string status { get; set; }

   }


   public class getFXForwardContractsFXForwardContractList

   {

       [JsonConverter(typeof(ArrayConverter<getFXForwardContractsFXForwardContract>))]

       public List<getFXForwardContractsFXForwardContract> FXForwardContract { get; set; }

   }


   public class getFXForwardContractsServiceRespHeader

   {

       public string ErrorText { get; set; }

       public object ErrorDetails { get; set; }

       public string GlobalErrorID { get; set; }

   }


   public class getFXForwardContractsServiceResponse

   {

       public getFXForwardContractsFXForwardContractList FXForwardContractList { get; set; }

       public getFXForwardContractsServiceRespHeader ServiceRespHeader { get; set; }

   }


   public class getFXForwardContractsContent

   {

       public getFXForwardContractsServiceResponse ServiceResponse { get; set; }

   }


   public class getFXForwardContractsRootObject

   {

       public getFXForwardContractsContent Content { get; set; }

   }

}


Download



Overview of GetFXForwardContracts.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 Newtonsoft.Json.Linq;

using System.Collections.Specialized;

using System.Configuration;

using System.Net;

using System.IO;

using System.Windows.Forms;

using System.Data;


namespace Demo

{

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

   {

       protected void Page_Load(object sender, EventArgs e)

       {

           if (Page.IsPostBack == false)

           {

               if (Session["userid"] != null)

               {

                   txtUserID.Text = (string)Session["userid"];

                   txtPassword.Text = (string)Session["pin"];

               }

           }

       }


       protected void btnSend_Click(object sender, EventArgs e)

       {

           if (Page.IsValid)

           {

               gvFXForwardContracts.DataSource = null;

               gvFXForwardContracts.DataBind();


               try

               {

                   string userID = txtUserID.Text;

                   Session["userid"] = userID;

                   string pin = txtPassword.Text;

                   Session["pin"] = pin;


                   string otp;

                   if (Session["otp"] == null)

                   {

                       otp = "0";

                   }

                   else

                   {

                       otp = Session["otp"].ToString();

                   }


                   var header = new HeaderJson();

                   header.userID = userID;

                   header.PIN = pin;

                   header.serviceName = "getFXForwardContracts";

                   header.OTP = otp;

                   var headerJson = new HeaderJsonObject();

                   headerJson.Header = header;

                   string strHeader = JsonConvert.SerializeObject(headerJson);


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


                   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();

                   }


                   getFXForwardContractsRootObject FX = new getFXForwardContractsRootObject();

                   FX = JsonConvert.DeserializeObject<getFXForwardContractsRootObject>(result);


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


                   if (globalErrorID == "010000")

                   {

                       int counter = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract.Count();

                       DataTable dt = new DataTable();

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

                       DataColumn col2 = new DataColumn("Open Date", typeof(string));

                       DataColumn col3 = new DataColumn("Period", typeof(string));

                       DataColumn col4 = new DataColumn("Amount", typeof(string));

                       DataColumn col5 = new DataColumn("Maturity Date", typeof(string));

                       DataColumn col6 = new DataColumn("Spot Rate", typeof(string));

                       DataColumn col7 = new DataColumn("Quote Currency Account ID", typeof(string));

                       DataColumn col8 = new DataColumn("Base Currency Account ID", typeof(string));

                       DataColumn col9 = new DataColumn("Base Currency Country's Name", typeof(string));

                       DataColumn col10 = new DataColumn("Base Currency", typeof(string));

                       DataColumn col11 = new DataColumn("Base Currency Interest Rate", typeof(string));

                       DataColumn col12 = new DataColumn("Quote Currency Country's Name", typeof(string));

                       DataColumn col13 = new DataColumn("Quote Currency", typeof(string));

                       DataColumn col14 = new DataColumn("Reference Number", typeof(string));

                       DataColumn col15 = new DataColumn("Customer ID", typeof(string));

                       DataColumn col16 = new DataColumn("Forward Rate", typeof(string));

                       DataColumn col17 = new DataColumn("Quote Currency Interest Rate", typeof(string));

                       DataColumn col18 = new DataColumn("FX Forward ID", typeof(string));

                       DataColumn col19 = new DataColumn("Status", typeof(string));


                       dt.Columns.Add(col1);

                       dt.Columns.Add(col2);

                       dt.Columns.Add(col3);

                       dt.Columns.Add(col4);

                       dt.Columns.Add(col5);

                       dt.Columns.Add(col6);

                       dt.Columns.Add(col7);

                       dt.Columns.Add(col8);

                       dt.Columns.Add(col9);

                       dt.Columns.Add(col10);

                       dt.Columns.Add(col11);

                       dt.Columns.Add(col12);

                       dt.Columns.Add(col13);

                       dt.Columns.Add(col14);

                       dt.Columns.Add(col15);

                       dt.Columns.Add(col16);

                       dt.Columns.Add(col17);

                       dt.Columns.Add(col18);

                       dt.Columns.Add(col19);


                       // 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] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].open_date?.ToString() ?? " ";

                           dt.Rows[i][col3] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].period?.ToString() ?? " ";

                           dt.Rows[i][col4] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].amount?.ToString() ?? " ";

                           dt.Rows[i][col5] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].maturity_date?.ToString() ?? " ";

                           dt.Rows[i][col6] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].spot_rate?.ToString() ?? " ";

                           dt.Rows[i][col7] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].quoteCurrencyAccountID?.ToString() ?? " ";

                           dt.Rows[i][col8] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].baseCurrencyAccountID?.ToString() ?? " ";


                           string basecurrencyname = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].baseCurrency?.ToString() ?? " ";

                           var z = utilities.getCurrencyList();

                           for (int x = 0; x < z.Count(); x++)

                           {

                               if (basecurrencyname == z[x].CurrencyCode)

                               {

                                   dt.Rows[i][col9] = z[x].CountryName;

                               }

                           }


                           dt.Rows[i][col10] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].baseCurrency?.ToString() ?? " ";

                           dt.Rows[i][col11] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].baseCurrencyInterestRate?.ToString() ?? " ";


                           string quotecurrencyname = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].quoteCurrency?.ToString() ?? " ";

                           for (int y = 0; y < z.Count(); y++)

                           {

                               if (quotecurrencyname == z[y].CurrencyCode)

                               {

                                   dt.Rows[i][col12] = z[y].CountryName;

                               }

                           }


                           dt.Rows[i][col13] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].quoteCurrency?.ToString() ?? " ";

                           dt.Rows[i][col14] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].referenceNumber?.ToString() ?? " ";

                           dt.Rows[i][col15] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].customerID?.ToString() ?? " ";

                           dt.Rows[i][col16] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].forward_rate?.ToString() ?? " ";

                           dt.Rows[i][col17] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].quoteCurrencyInterestRate?.ToString() ?? " ";

                           dt.Rows[i][col18] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].FX_ForwardID?.ToString() ?? " ";

                           dt.Rows[i][col19] = FX.Content.ServiceResponse.FXForwardContractList.FXForwardContract[i].status?.ToString() ?? " ";

                       }

                       gvFXForwardContracts.DataSource = dt;

                       gvFXForwardContracts.DataBind();

                   }

                   else if (globalErrorID == "010041")

                   {

                       otp = utilities.newOTP();

                       Session["otp"] = otp;

                   }

                   else

                   {

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

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

                       lblErrorMessage.Text = errorMessage;

                   }

               }

               catch (Exception ex)

               {

                   lblExceptionMsg.Text = ex.ToString();

               }

           }

       }

   }

}


Download

Created with the Personal Edition of HelpNDoc: Free HTML Help documentation generator