Thursday, May 31, 2018

Show cookie data one time in MVC



@{
    Response.Cookies["userName"].Value = "Himanshu Gautam";
    Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);
    Response.Cookies["val"].Value = "0";
    HttpCookie aCookie = new HttpCookie("lastVisit");
    aCookie.Value = DateTime.Now.ToString();
    aCookie.Expires = DateTime.Now.AddDays(1);
    Response.Cookies.Add(aCookie);

    ViewBag.CookieUsername = Server.HtmlEncode(Request.Cookies["userName"].Value);
    ViewBag.CookieLastvisit = Server.HtmlEncode(Request.Cookies["lastVisit"].Value);
    var x = Convert.ToInt16(Server.HtmlEncode(Request.Cookies["val"].Value));
}



@if (x == 0)
{
    <div id="cookiedata">
        Username: @ViewBag.CookieUsername  <br />
        Last Visit: @ViewBag.CookieLastvisit<span><a class="close" onclick="hidediv();">close</a></span>

    </div>

}

<script>
    function hidediv() {
        $('#cookiedata').hide();
        document.cookie="val=1;";
    }
</script>

Tuesday, May 22, 2018

Show Rows as Columns in SQL Server

SELECT TOP 1000 [ID]
      ,[Name]
      ,[Salary]
      ,[Department]
      ,[Sex]
      ,[UserId]
      ,[ManagerID]
  FROM [TestDatabase].[dbo].[Employees]


  DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(Name)
                    from Employees
                    group by Name, id
                    order by id
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')
        ,1,1,'')

set @query = N'SELECT ' + @cols + N' from
             (
                select ID, Name
                from Employees
            ) x
            pivot
            (
                max(ID)
                for Name in (' + @cols + N')
            ) p '

exec sp_executesql @query;



for Department

set @query = N'SELECT ' + @cols + N' from
             (
                select Name, Department
                from Employees
            ) x
            pivot
            (
                max(Department)
                for Name in (' + @cols + N')
            ) p '