Tuesday, 24 January 2017

REST Design Principles

    Everything is a Resource

  • Everything that needs to be identified on web should be treated as Resource and be uniquely identified.
  •  Resource is usually Noun and not verb
  •  Each piece of data available on the Internet has a format that could be described by a content type
  • Example: “http://Csharpstar.com/Employee/1234
  • Each resource is identifiable by a unique identifier (URI)

  • The Internet contains so many different resources, they all should be accessible via URIs and should be identified uniquely
  • The URI keeps the data self-descriptive and eases further development on it.
  • The URIs helps you to reduce the risk of logical errors in your programs to a minimum.
  • URIs expose different types of resources in a straightforward manner
  • Example:
    – Images (http://Csharpstar.com/Images)  
    – Videos (http://Csharpstar.com/Videos)  
    – XML documents (http://Csharpstar.com/Archieves)

    Use the standard HTTP methods

       Use standard Http methods to operate on identified resources
        – Get : Request an existing Resource
        – Post :Update an existing Resource
        – Put : Create or Update a Resource
        – Delete:Remove/Delete a Resource

    Allow multiple representations for same Resource

  •  A key feature of a resource is that they may be represented in a different form than the one it is stored. so, it can be requested or posted in different representations.As long as the specified format is supported, the REST-enabled endpoint should use it
  • The resource should be presented to client in multiple formats as the client desire. The format can be anything like XML,JSON,JPG,PDF,HTML etc..
  • Communication should be always stateless   

  • REST services are stateless by nature. So, the clients state can not be stored on the server.However, the client can still maintain state and pass it to the service with each request
  • In a production environment, it is likely that incoming requests are served by a load balance, ensuring availability and high availability. Once exposed via a load balance, the idea of keeping your application state at server side gets compromised. This doesn’t mean that you are not allowed to keep the state of your application. It just means that you should keep it in a RESTful way.

Friday, 30 December 2016

C# 7.0

Visual studio 2017 RC which was released on November 16, 2016. Let us see some of the new features of C# 7.0, which is the default language of this version.

the new C# 7.0 features coming down the pipeline. Although C# 7.0 is still in development, most of the new features noted by Microsoft have been enabled in Visual Studio 15 Preview 5.
According to Mads Torgersen, Microsoft program manager for C#; the biggest features are tuples, which make it easy to have multiple results, and pattern matching, which simplifies code that is conditional on the shape of data.
Current features and beyond can be followed at Roslyn on GitHub.
Some of the new features in C# 7.0 are
  • Out variables
  • Pattern matching
  • Tuples
  • Deconstruction
  • Local functions
  • Ref returns and locals
  • Generalized async return types

Tuesday, 31 May 2016

SQL Query for generating matrix in SQL Server

We have a table like that:
























We need result of query in MATRIX form like this:
















We can use SQL Server's PIVOT operator

;WITH Q AS (
  SELECT  [Vehicle] = 'Scooter' , [Employee] = 'Anas', [Amount] = 80115.50
  UNION ALL SELECT 'Scooter', 'Ross', 36571.85
  UNION ALL SELECT 'Scooter', 'Michelle', 39571.97
  UNION ALL SELECT 'Car', 'Peterson', 82658.23
  UNION ALL SELECT 'Car', 'Ross', 68998.85
  UNION ALL SELECT 'Car', 'Anas', 63598.75
  UNION ALL SELECT 'Car', 'Smith', 58950.53
  UNION ALL SELECT 'Car', 'Andrew', 57890.21
  UNION ALL SELECT 'Van', 'Andrew', 82658.23
  UNION ALL SELECT 'Van', 'Smith', 13400.65
  UNION ALL SELECT 'Van', 'Deniss', 15430.10
  UNION ALL SELECT 'Bus', 'Anas', 95867.55
  UNION ALL SELECT 'Bus', 'Bob', 98222.85
  UNION ALL SELECT 'Bus', 'Ronald', 98547.52
  UNION ALL SELECT 'Bus', 'Eric', 57880.65
  UNION ALL SELECT 'Bus', 'Smith', 6430.35
  UNION ALL SELECT 'Truck', 'Ross', 29560.10
  UNION ALL SELECT 'Truck', 'Andrew', 25780.30
  UNION ALL SELECT 'Truck', 'Taniya', 25054.44
 
)
SELECT  Employee,
        Scooter = ISNULL( Scooter, 0 ),
        Car = ISNULL( Car, 0 ),
        Van = ISNULL( Van, 0 ),
        Bus = ISNULL( Bus, 0 ),
        Truck = ISNULL( Truck, 0 ),
        Total = ISNULL( Scooter, 0 )+ ISNULL( Car, 0 ) + ISNULL( Van, 0 )+ ISNULL( Bus, 0 )  + ISNULL( Truck, 0 )
FROM    (
          SELECT  FROM  Q
        ) AB
PIVOT   ( SUM(Amount ) FOR [Vehicle] IN ([Scooter],[Car], [Van], [Bus], [Truck] )) PVT_table


Monday, 1 June 2015

Automatic Page Refresh in ASP.NET

To refresh a page automatically at a time interval. Doing this is pretty simple, using META tags.

    <meta http-equiv="refresh" content="45">


    <meta http-equiv="refresh" content="45;url=home.aspx">

But if you used Master Pages, the META tag then all pages that use this master page will be refreshed, which is not desired, to accomplish this, add the following c# code in the code behind of, the particular page you want to refresh:
Response.AppendHeader("Refresh", 45 + "; URL=home.aspx");

Where, 45 is the time interval in seconds.

There are many other ways of doing auto refreshing , like using JavaScript and J Queries, but this is simplest way. 

Wednesday, 25 March 2015

SQL:- Multiple rows to a single comma-separated value

Sample Data
DECLARE @MyTable1 TABLE(ID INT, Value Varchar(50))
INSERT INTO @MyTable1 VALUES (1,'John'),(1,'Tom'),(1,'Sajan'),(1,'Ram')
Sql Query
SELECT  ID
       ,STUFF((SELECT ', ' + CAST(Value AS VARCHAR(200)) [text()]
        FROM @MyTable1
         WHERE ID = t.ID
         FOR XML PATH(''), TYPE)
        .value('.','NVARCHAR(MAX)'),1,2,' ') Name
FROM @MyTable1 t
GROUP BY ID
Results
ID Names
1 John, Tom, Sajan, Ram

Monday, 16 March 2015

Implement Remember Me functionality using CheckBox in ASP.Net C#

HTML Markup
I have a simple HTML Form below which has two ASP.Net TextBox controls txt_UserName  and  txt_Password and a CheckBox control chk_RememberMe to allow user specify whether he wants the page to remember the UserName and Password when he visits next time, finally an ASP.Net Button  btn_Login which when clicked will save the entered UserName and Password in the Cookie.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    UserName:
    <asp:TextBox ID="txt_UserName" runat="server"></asp:TextBox><br />
    Password:
    <asp:TextBox ID="txt_Password" TextMode="Password" runat="server"></asp:TextBox><br />
    Remember me:
    <asp:CheckBox ID="chk_RememberMe" runat="server" /><br />
    <asp:Button ID="btn_Login" runat="server" Text="Login" OnClick="btn_Login_Click" />
    </form>
</body>
</html>
Saving the UserName and Password in Cookie
When the Button btn_Login is clicked the following event handler is executed which first checks whether the chk_RememberMe is checked. If it is checked then it save the UserName and Password in the Cookies and sets their expiration date to 30 days in future from the current date. And if it is not checked then it sets the expiration date to 1 day in past so that Cookie is destroyed.
C#
protected void btn_Login_Click(object sender, EventArgs e)
{
    if (chk_RememberMe.Checked)
    {
        Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(30);
        Response.Cookies["Password"].Expires = DateTime.Now.AddDays(30);
    }
    else
    {
        Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
        Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
 
    }
    Response.Cookies["UserName"].Value = txt_UserName.Text.Trim();
    Response.Cookies["Password"].Value = txt_Password.Text.Trim();
}
 Populating the UserName and Password from Cookie and setting it in TextBoxes
Now in Page_Load event we will check if the Cookie exists and if yes then we will set the TextBoxes for UserName and Password with their respective Cookie value.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
      if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null)
        {
         txt_UserName.Text = Request.Cookies["UserName"].Value;
         txt_Password.Attributes["value"] = Request.Cookies["Password"].Value;
        }
    }
}

Wednesday, 2 July 2014

How to delete duplicate entries from table without deleting single entry in SQL

How to delete duplicate entries  from table without deleting single  entry in SQL


Suppose there are 5 duplicate  entries with same values in [MyTable] table, we have to keep only one entry in the table. 
When we  use the given code then 4 entries will be deleted


set rowcount 4
delete from MyTable where Myid =
set rowcount 0