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


Thursday, 29 May 2014

DateTime String Format in C#

Reference:---http://www.csharp-examples.net/string-format-datetime/

Following examples demonstrate how are the format specifiers rewritten to the output.
DateTime date = new DateTime(2004, 9, 5, 21, 3, 2, 123);
----2004-09-05 21:03:02.123---  = 05-Sep-2004 9:03:02 PM
String.Format("{0:y yy yyy yyyy}", date); // "4 04 004 2004" year String.Format("{0:M MM MMM MMMM}", date); // "9 09 Sep September" month String.Format("{0:d dd ddd dddd}", date); // "5 05 Sun Sunday" day String.Format("{0:h hh H HH}", date); // "9 09 21 21" hour 12/24 String.Format("{0:m mm}", date); // "3 03" minute String.Format("{0:s ss}", date); // "2 02" second String.Format("{0:f ff fff ffff}", date); // "1 12 123 1230" sec.fraction String.Format("{0:F FF FFF FFFF}", date); // "1 12 123 123" without zeroes String.Format("{0:t tt}", date); // "P PM" A.M. or P.M. String.Format("{0:z zz zzz}", date); // "-6 -06 -06:00" time zone
String.Format("{0:d/M/yyyy HH:mm:ss}", date); // "5/9/2004 21:03:02" 
String.Format("{0:d/M/yyyy HH:mm:ss}", date); // "5.9.2004 21:03:02" 
String.Format("{0:M/d/yyyy}", date);            // "9/5/2004"
String.Format("{0:MM/dd/yyyy}", date);          // "09/05/2004"
// day/month names
String.Format("{0:ddd, MMM d, yyyy}", date);    // "Sun, Sep 5, 2004"
String.Format("{0:dddd, MMMM d, yyyy}", date);  // "Sunday, September 5, 2004"
// two/four digit year
String.Format("{0:MM/dd/yy}", date);            // "09/05/04"
String.Format("{0:MM/dd/yyyy}", date);          // "09/05/2004"

Following examples show usage of standard format specifiers in String.Format method and the resulting output.
String.Format("{0:t}", date);  // "9:03 PM"                         
String.Format("{0:d}", date);  // "9/5/2004"                        
String.Format("{0:T}", date);  // "9:03:02 PM"                      L
String.Format("{0:D}", date);  // "Sunday, September 05, 2004"      
String.Format("{0:f}", date);  // "Sunday, September 05, 2004 9:03 PM" 
String.Format("{0:F}", date);  // "Sunday, September 05, 2004 9:03:02 PM" 
String.Format("{0:g}", date);  // "9/5/2004 9:03 PM"             
String.Format("{0:G}", date);  // "9/5/2004 9:03:02 PM"          
String.Format("{0:m}", date);  // "September 05"                  
String.Format("{0:y}", date);  // "September, 2004"                  
String.Format("{0:r}", date);  // "Sun, 05 Sep 2014 21:03:02 GMT"   
String.Format("{0:s}", date);  // "2004-09-05T21:03:02"             
String.Format("{0:u}", date);  // "2004-09-05 21:03:02Z"