Tuesday, April 24, 2018

What in Func in c#

    Func is a generic delegate that encapsulates a method that can accept parameters and return some value

//Annonymous function
Func MyFun1 = delegate(int[] a)
    {
        string tempString = "";
        foreach (int num in a) {
            tempString += num.ToString() + ",";
        }       
        return tempString;
    };




//Generic or Arrow function
    Func MyFun2 = (a) =>
    {
        string tempString = "";
        foreach (float num in a)
        {
            tempString += num.ToString() + ",";
        }
        return tempString;
    };


    private T[] Sort(T[] inputArray)
    {
        //Sort array
        //and return sorted array
        return inputArray;
    }

   int[] numArray = { 10, 15, 26, 98, 68 };
   float[] floatArray = { 10.9F, 15.5F, 26, 98.8F, 68 };
   lblMessage.Text = "Num : " + MyFun1.Invoke(Sort(numArray));
   lblMessage.Text += "Float : " + MyFun2.Invoke(Sort(floatArray));

Friday, April 13, 2018

Compare two dates in javascript

var yearDiff = 100;   
var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth();
    var day = d.getDate();
    var dtToday = new Date(year - yearDiff , month, day)

    mydate = new Date(DOBDate);
    year = mydate.getFullYear();
    month = mydate.getMonth();
    day = mydate.getDate();
    mydate = new Date(year, month, day);

    if (mydate > dtToday) {
      alert("Age can not be greater than 100 years.");
    }
    else {
        alert("Prev Smaller");
    }

Tuesday, March 27, 2018

Get Publish page url and title in sitefinity



SELECT sf_page_node.Title_,
  _url_parent =
    CASE WHEN (sf_page_node_2.url_name_ = N'Pages')   THEN '/'
              ELSE sf_page_node_2.url_name_ +'/'
    END,
  sf_page_node.url_name_ AS url_page
FROM
  sf_page_node LEFT OUTER JOIN
  sf_page_node AS sf_page_node_2
  ON sf_page_node.parent_id = sf_page_node_2.id
WHERE (sf_page_node.root_id =
    (
      SELECT id FROM sf_page_node AS sf_page_node_1 WHERE (nme = 'FrontendSiteMap')
     )
   ) AND
  (sf_page_node.show_in_navigation = 1) AND
  (sf_page_node.render_as_link = 1) AND
  (sf_page_node.node_type = 0) AND
  (sf_page_node.approval_workflow_state_ = N'Published')

Monday, November 27, 2017

Find a specific text in a SQL Server Stored Procedure, Function, View

Declare @SearchString  varchar(50) = 'kotak_CMS..sf_subscriber'

Get Sp name with type and defination from sys.sql_modules and sys.objects tables
SELECT [Scehma]=schema_name(o.schema_id), o.Name, o.type, m.definition
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON o.object_id = m.object_id
WHERE m.definition like '%'+@SearchString+'%'

Get Sp name from syscomment and sysobjects table
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%'+@SearchString+'%'

Get Sp name from sys.procedures table
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%'+@SearchString+'%'
GO

Wednesday, November 1, 2017

Below Query for fetch most trafic consuming query list. Most expensive, time consuming queries

SELECT TOP 100
    qs.total_elapsed_time / qs.execution_count / 1000000.0 AS average_seconds,
    qs.total_elapsed_time / 1000000.0 AS total_seconds,
    qs.execution_count,
    SUBSTRING (qt.text,qs.statement_start_offset/2,
    (CASE WHEN qs.statement_end_offset = -1
    THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2
    ELSE qs.statement_end_offset END - qs.statement_start_offset)/2) AS individual_query,
    o.name AS object_name,
    DB_NAME(qt.dbid) AS database_name
FROM sys.dm_exec_query_stats qs
    CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as qt
    LEFT OUTER JOIN sys.objects o ON qt.objectid = o.object_id
Where qt.dbid = DB_ID()
ORDER BY average_seconds DESC;

Tuesday, August 8, 2017

Removing X-Powered-By ASP.Net and other version headers

Removing X-AspNet-Version

In web.config add this:
<system.web>
<httpRuntime enableVersionHeader="false"/>


Removing X-AspNetMvc-Version

In Global.asax.cs add this line:

protected void Application_Start()
{
             MvcHandler.DisableMvcResponseHeader = true;
}

Removing or changing Server

In Global.asax.cs add this:

protected void Application_PreSendRequestHeaders()
{
           //Response.Headers.Remove("Server");
          Response.Headers.Set("Server","Server");
          Response.Headers.Remove("X-AspNet-Version"); //alternative to above solution
          Response.Headers.Remove("X-AspNetMvc-Version"); //alternative to above solution
}

Removing or changing X-Powered-By

IIS 7 - You can also remove the X-Powered-By header by including these lines to the element:






or manually go to IIS7 Management Console and open HTTP Response Headers

Thursday, March 16, 2017

Get Nth Row From SQL, Database

To Get the nth row from your table to the following steps
  • Find all the distinct values count
  • Write a self query that test the grater then condition
  • Now you will get the same row that you requested
For Ex:

Create table Employee(Id int identity(1,1),Name varchar(10),Salary int);

Insert into Employee values ('A',10000)
Insert into Employee values ('B',10001)
Insert into Employee values ('C',10002)
Insert into Employee values ('D',10003)
Insert into Employee values ('E',10004)
Insert into Employee values ('F',10005)
Insert into Employee values ('G',10006)
Insert into Employee values ('H',10007)
Insert into Employee values ('I',10008)
Insert into Employee values ('J',10009)


Declare @N int = 9
SELECT * FROM Employee E1
WHERE (@N-1) =
(SELECT COUNT(DISTINCT(E2.Salary)) FROM Employee E2 WHERE E2.Salary < E1.Salary)