C#

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

Get Stock Orders


1. getStockOrders.cs


These five packages are required to be used.




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





2. GetStockOrders.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 ‘getStockOrders’ class.




Step 5. Obtain error code. If error code is ‘010000’ which means invocation successful, populate attributes of the ‘getStockOrders’ 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 getStockOrders.cs


using Newtonsoft.Json;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace Demo

{

   public class getStockOrdersServiceRespHeader

   {

       public string ErrorText { get; set; }

       public object ErrorDetails { get; set; }

       public string GlobalErrorID { get; set; }

   }


   public class getStockOrdersStockOrder

   {

       public string orderType { get; set; }

       public string quantity { get; set; }

       public string buy_or_sell { get; set; }

       public string orderID { get; set; }

       public string initial_spot_price { get; set; }

       public string maturity_date { get; set; }

       public string settlementAccountID { get; set; }

       public string expiration_type { get; set; }

       public string order_status { get; set; }

       public string order_date { get; set; }

       public string price_at_execution { get; set; }

       public string stockSymbol { get; set; }

       public string customerID { get; set; }

       public string price_at_order { get; set; }

   }


   public class getStockOrdersStockOrderList

   {

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

       public List<getStockOrdersStockOrder> StockOrder { get; set; }

   }


   public class getStockOrdersServiceResponse

   {

       public getStockOrdersServiceRespHeader ServiceRespHeader { get; set; }

       public getStockOrdersStockOrderList StockOrderList { get; set; }

   }


   public class getStockOrdersContent

   {

       public getStockOrdersServiceResponse ServiceResponse { get; set; }

   }


   public class getStockOrdersRootObject

   {

       public getStockOrdersContent Content { get; set; }

   }

}


Download



Overview of GetStockOrders.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 GetStockOrders : 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)

           {

               gvStockOrders.DataSource = null;

               gvStockOrders.DataBind();


               try

               {

                   string userID = txtUserID.Text;

                   Session["userid"] = userID;

                   string pin = txtPassword.Text;

                   Session["pin"] = pin;


                   string otp;

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

                   {

                       otp = "";

                   }

                   else

                   {

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

                   }


                   var header = new HeaderJson();

                   header.userID = userID;

                   header.PIN = pin;

                   header.serviceName = "getStockOrders";

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

                   }


                   getStockOrdersRootObject SO = new getStockOrdersRootObject();

                   SO = JsonConvert.DeserializeObject<getStockOrdersRootObject>(result);


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


                   if (globalErrorID == "010000")

                   {

                       int counter = SO.Content.ServiceResponse.StockOrderList.StockOrder.Count();

                       DataTable dt = new DataTable();

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

                       DataColumn col2 = new DataColumn("Order Type", typeof(string));

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

                       DataColumn col4 = new DataColumn("Buy Or Sell", typeof(string));

                       DataColumn col5 = new DataColumn("Order ID", typeof(string));

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

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

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

                       DataColumn col9 = new DataColumn("Expiration Type", typeof(string));

                       DataColumn col10 = new DataColumn("Order Status", typeof(string));

                       DataColumn col11 = new DataColumn("Order Date", typeof(string));

                       DataColumn col12 = new DataColumn("Price At Execution", typeof(string));

                       DataColumn col13 = new DataColumn("Stock Symbol", typeof(string));

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

                       DataColumn col15 = new DataColumn("Price At Order", 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);


                       // 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] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].orderType?.ToString() ?? " ";

                           dt.Rows[i][col3] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].quantity?.ToString() ?? " ";

                           dt.Rows[i][col4] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].buy_or_sell?.ToString() ?? " ";

                           dt.Rows[i][col5] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].orderID?.ToString() ?? " ";

                           dt.Rows[i][col6] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].initial_spot_price?.ToString() ?? " ";

                           dt.Rows[i][col7] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].maturity_date?.ToString() ?? " ";

                           dt.Rows[i][col8] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].settlementAccountID?.ToString() ?? " ";

                           dt.Rows[i][col9] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].expiration_type?.ToString() ?? " ";

                           dt.Rows[i][col10] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].order_status?.ToString() ?? " ";

                           dt.Rows[i][col11] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].order_date?.ToString() ?? " ";

                           dt.Rows[i][col12] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].price_at_execution?.ToString() ?? " ";


                           //dt.Rows[i][col13] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].stockSymbol?.ToString() ?? " ";

                           string symbolname = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].stockSymbol?.ToString() ?? " ";

                           var z = utilities.getStockSymbols();

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

                           {

                               if (symbolname == z[x].symbol)

                               {

                                   dt.Rows[i][col13] = z[x].company;

                               }

                           }



                           dt.Rows[i][col14] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].customerID?.ToString() ?? " ";

                           dt.Rows[i][col15] = SO.Content.ServiceResponse.StockOrderList.StockOrder[i].price_at_order?.ToString() ?? " ";

                       }

                       gvStockOrders.DataSource = dt;

                       gvStockOrders.DataBind();

                   }


                   else if (globalErrorID == "010041")

                   {

                       otp = utilities.newOTP();

                       Session["otp"] = otp;

                   }


                   else

                   {

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

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

                       lblErrorMessage.Text = errorMessage;

                   }

               }

               catch (Exception ex)

               {

                   lblExceptionMsg.Text = ex.ToString();

               }

           }

       }

   }

}


Download

Created with the Personal Edition of HelpNDoc: Free help authoring tool