About Me

My photo
I am a senior MEAN/MERN stack consultant for United Nations. With 7 years of experience.

Monday, April 12, 2021

How to use a api response object...


this is an angular code, 
where it calls a certain service, 
and read the value in 3 different ways. 

lets see..

the code ...

    this.lookupService.getLookupData('Session').subscribe(
      data => {
        let sessionLookupData1 = data;
        console.log('sessionLookupData1'sessionLookupData1._body);

        let sessionLookupData2 = JSON.stringify(data);
        console.log('sessionLookupData2'sessionLookupData2._body);

        let sessionLookupData3 = JSON.parse(JSON.stringify(data));
        console.log('sessionLookupData3'sessionLookupData3._body);
      }
    );

1st sessionLookupData1 will print the the response of the api call, so you want be able to access the _body 

2nd sessionLookupData2 will print the response (Converts a JavaScript value to a JavaScript Object Notation (JSON) string.),  so you wont be able to get the objects as well. 

3rd is the correct way. Converts a JavaScript Object Notation (JSON) string into an object.

Now you can access the object values just as accessing a normal js object. 

Thanks you :)
happy coding. JavaScript rules ...

Monday, January 20, 2020

using SOAP service in c#



client app

consuming soap service , console application 


using System;
using System.IO;
using System.Net;
using System.Xml;

namespace UsingSOAPRequest
{
    class Program
    {
        static void Main(string[] args)
        {
            //creating object of program class to access methods 
            Program obj = new Program();
            Console.WriteLine("Please Enter Input values..");
            //Reading input values from console 
            int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());
            //Calling InvokeService method 
            obj.InvokeService(a, b);
        }
        public void InvokeService(int a, int b)
        {
            //Calling CreateSOAPWebRequest method 
            HttpWebRequest request = CreateSOAPWebRequest();

            XmlDocument SOAPReqBody = new XmlDocument();
            //SOAP Body Request 
            SOAPReqBody.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?> 
            <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-   instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> 
             <soap:Body> 
                <Addition xmlns=""http://tempuri.org/""> 
                  <a>" + a + @"</a> 
                  <b>" + b + @"</b> 
                </Addition> 
              </soap:Body> 
            </soap:Envelope>");


            using (Stream stream = request.GetRequestStream())
            {
                SOAPReqBody.Save(stream);
            }
            //Geting response from request 
            using (WebResponse Serviceres = request.GetResponse())
            {
                using (StreamReader rd = new StreamReader(Serviceres.GetResponseStream()))
                {
                    //reading stream 
                    var ServiceResult = rd.ReadToEnd();
                    //writting stream result on console 
                    Console.WriteLine(ServiceResult);
                    Console.ReadLine();
                }
            }
        }

        public HttpWebRequest CreateSOAPWebRequest()
        {
            //Making Web Request 
            HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"http://localhost:52168/Service.asmx");
            //SOAPAction 
            Req.Headers.Add(@"SOAPAction:http://tempuri.org/subtract");
            //Content_type 
            Req.ContentType = "text/xml;charset=\"utf-8\"";
            Req.Accept = "text/xml";
            //HTTP method 
            Req.Method = "POST";
            //return HttpWebRequest 
            return Req;
        }
    }
}




server side 
dot net 3.5  web site -> asp.net web service 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]

public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    
    public string sum(int a, int b)
    {
        return (a + b).ToString();
    }
    [WebMethod]
    public int subtract(int a, int b)
    {
        return a - b;
    }
    [WebMethod]
    public int multiply(int a, int b)
    {
        return a * b;
    }
    [WebMethod]
    public int divide(int a, int b)
    {
        return a / b;
    }

}

ASP.net forms - connect to a ms sql db and use CRUD operations.


Create a table called customer to store customer information.


USE [cypad]
GO

/****** Object:  Table [dbo].[customer]    Script Date: 20-Jan-20 3:03:53 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[customer](
[customerid] [numeric](18, 0) NULL,
[name] [nvarchar](50) NULL,
[country] [nvarchar](50) NULL
) ON [PRIMARY]
GO


insert few records to it. 

USE [cypad]
GO

INSERT INTO [dbo].[customer]
           ([customerid]
           ,[name]
           ,[country])
     VALUES
           (<customerid, numeric(18,0),>
           ,<name, nvarchar(50),>
           ,<country, nvarchar(50),>)
GO


add a asp.net web form 


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace cypad
{
    public partial class customerList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.IsPostBack)
                {
                    this.loadCustomerList();
                }
            }catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }

        private void loadCustomerList()
        {
            try
            {
                string consting = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
                string query = "SELECT * FROM customer";
                using (SqlConnection con = new SqlConnection(consting))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            gvCustomerList.DataSource = dt;
                            gvCustomerList.DataBind();
                        }
                    }
                }
            }
            catch
            {
                throw;
            }
        }
    }
}


Conections string should be stored in web.config file 

  <connectionStrings>
    <add name ="MyConnection"
    connectionString ="Data Source=DENISP\SQLEXPRESS;Initial Catalog=cypad;Integrated Security=true"/>
  </connectionStrings>


add the newly created form to site master.  


use the page notation to add the created form to the master page 

<%@ Page Title="Customer List" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" 
    CodeBehind="customerList.aspx.cs" Inherits="cypad.customerList" %>


<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

    <asp:GridView ID="gvCustomerList" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="customerid" HeaderText="Customer ID" />
            <asp:BoundField DataField="name" HeaderText="Customer Name" />
            <asp:BoundField DataField="country" HeaderText="Country" />
        </Columns>
    </asp:GridView>

</asp:Content>





        private void loadCustomerList()
        {
            try
            {
                DBconnections DBconnet = new DBconnections();
                string consting = DBconnet.getConnectionString().consting;
                string query = string.Concat( "insert into customer (name,country) values('{0}',{1})",txt;
                using (SqlConnection con = new SqlConnection(consting))
                {
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    sql = 
                        connection.Open();
                        adapter.InsertCommand = new SqlCommand(sql, connection);
                        adapter.InsertCommand.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
            catch
            {
                throw;
            }
        }

















Monday, August 3, 2015

Oracle Collections (Index-By table)

collection is an ordered group of elements, all of the same type. It is a general concept that encompasses lists, arrays, and other familiar datatypes. Each element has a unique subscript that determines its position in the collection.

types of collections

  • Index-by table (associative arrays)
  • Nested tables
  • Varrays ( variable size arrays)


If you already have code or business logic that uses some other language, you can usually translate that language's array and set types directly to PL/SQL collection types.
  • Arrays in other languages become VARRAYs in PL/SQL.
  • Sets and bags in other languages become nested tables in PL/SQL.
  • Hash tables and other kinds of unordered lookup tables in other languages become associative arrays in PL/SQL.

Index-by table


DECLARE
    i            VARCHAR (50);
    TYPE arraylike IS TABLE OF VARCHAR2 (50)
        INDEX BY VARCHAR2 (50);
    datearray    arraylike;
    cutidarray   arraylike;
BEGIN
    datearray (1) := '02-08-2015';
    datearray (2) := '02-08-2015';
    datearray (3) := '06-08-2015';
    datearray (4) := '06-08-2015';
    datearray (5) := '04-08-2015';
    datearray (6) := '04-08-2015';
    datearray (7) := '02-08-2015';
    datearray (8) := '04-08-2015';
    datearray (9) := '06-08-2015';
    datearray (10) := '02-08-2015';
    datearray (11) := '06-08-2015';
    datearray (12) := '04-08-2015';
    datearray (13) := '02-08-2015';
    datearray (14) := '06-08-2015';
    datearray (15) := '02-08-2015';
    cutidarray (1) := '11684129';
    cutidarray (2) := '11192994';
    cutidarray (3) := '11300838';
    cutidarray (4) := '11281815';
    cutidarray (5) := '11228291';
    cutidarray (6) := '11129267';
    cutidarray (7) := '11114680';
    cutidarray (8) := '11183177';
    cutidarray (9) := '11242009';
    cutidarray (10) := '11200434';
    cutidarray (11) := '11328917';
    cutidarray (12) := '11130167';
    cutidarray (13) := '11172261';
    cutidarray (14) := '11401292';
    cutidarray (15) := '11893237';

    FOR i IN 1 .. 15
    LOOP
        UPDATE m01_cma_complient_details cma
           SET cma.is_secondery_update = 1,
               cma.m01_other_next_review =
                   TO_DATE (datearray (i), 'dd-mm-yyyy')
         WHERE cma.m01_customer_id IN
                   (SELECT m01.m01_customer_id
                      FROM m01_customer m01
                     WHERE m01.m01_c1_customer_id = cutidarray (i));
    /*DBMS_OUTPUT.put_line (
        datearray (i) || '-----------' || cutidarray (i)); */
    END LOOP;
END;






Thursday, July 23, 2015

Nested and Correlated Subqueries

Nested - The subquery executed first and the result is inserted to the main query.

SELECT *
  FROM t11_executed_orders t11
 WHERE t11.t11_symbol IN
           (SELECT aa.t11_symbol
              FROM t11_executed_orders aa
             WHERE aa.t11_value = (SELECT bb.t11_price * bb.t11_filled_volume
                                     FROM t11_executed_orders bb
                                    WHERE bb.t11_exec_id = '123'));

the inner most query will executed first and then the nest subquery and after that the main query.


Correlated - The main query executed first and the subquery is executed against each and every row returned by the main query.

UPDATE t11_executed_orders t11
   SET t11.t11_value =
           (SELECT a.t11_price * a.t11_filled_volume
              FROM t11_executed_orders a
             WHERE     t11.t11_exec_id = a.t11_exec_id
                   AND t11.t11_exectime_int = a.t11_exectime_int
                   AND t11.t11_side = a.t11_side
                   AND t11.t11_exchange = a.t11_exchange)
 WHERE t11.t11_value <>
           (SELECT a.t11_price * a.t11_filled_volume
              FROM t11_executed_orders a
             WHERE     t11.t11_exec_id = a.t11_exec_id
                   AND t11.t11_exectime_int = a.t11_exectime_int
                   AND t11.t11_side = a.t11_side
                   AND t11.t11_exchange = a.t11_exchange);



the same update can be written in PL-SQL as well . like this.

BEGIN
    FOR i IN (SELECT *
                FROM (SELECT t11_exec_id,
                             t11_exchange,
                             t11_side,
                             t11_datetime,
                             t11_symbol,
                             t11_routingac,
                             t11_price * t11_filled_volume
                                 AS calculated_t11_value,
                             t11_value
                        FROM mubasher_oms.t11_executed_orders)
               WHERE calculated_t11_value <> t11_value)
    LOOP
        UPDATE mubasher_oms.t11_executed_orders
           SET t11_value = i.calculated_t11_value
         WHERE     t11_exec_id = i.t11_exec_id
               AND t11_exchange = i.t11_exchange
               AND t11_side = i.t11_side;
    END LOOP;
END;






for more infor

http://sql-plsql.blogspot.com/2011/09/difference-nested-correlated-subquery.html


Saturday, June 20, 2015

Analytical Functions in Oracle

http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions004.htm#SQLRF06174

if i explain this with a simple example .. lets say we have a to take a data set like this..

"select  sum(order_amt) from t01_orders group by order_no "


this will certainly  return the sum of each order amount . but what if want to show other details like customer name and all.. you can not add that column to group by clause.


"select sum(order_amt) over(partition by order_no) , * from t01_orders"


simple like that ... :)

Friday, May 27, 2011

C++ programming language

C++ is often considered to be a superset of C, but this is not strictly true. Most C code can easily be made to compile correctly in C++, but there are a few differences that cause some valid C code to be invalid or behave differently in C++.
One commonly encountered difference is that C allows implicit conversion from void* to other pointer types, but C++ does not. Another common portability issue is that C++ defines many new keywords, such as new and class, that may be used as identifiers (e.g. variable names) in a C program.
Some incompatibilities have been removed by the latest (C99) C standard, which now supports C++ features such as line comments (//), and mixed declarations and code. On the other hand, C99 introduced a number of new features that C++ does not support, such as variable-length arrays, native complex-number types, designated initializers, and compound literals. However, at least some of the new C99 features will likely be included in the next version of the C++ standard