Thursday, August 13, 2015


;WITH cte
                                       ORDER BY ( SELECT 0)) RN
         FROM   #MyTable)

nth max salary in sql

    SELECT EmpID,EmpName,EmpSalar,
           RN = ROW_NUMBER() OVER (ORDER BY EmpSalary DESC)
    FROM dbo.Salary
SELECT EmpID,EmpName,EmpSalar
WHERE RN = @NthRow

Monday, August 10, 2015

interface with same method name in c#

interface IPrinter
        void print(string a);

    interface IPrinter1
        void print(string a);

class Program
        static void Main(string[] args)
            IPrinter p = new Printer();

            Printer p1 = new Printer();

    class Printer : IPrinter, IPrinter1
         void IPrinter.print(string a)
            Console.Write(a + "Printer");

         void IPrinter1.print(string a)

Thursday, July 9, 2015

difference between save and insert in mongodb

If the document contains an _id field, then the save() method performs an upsert querying the collection on the _id field:
If a document does not exist with the specified _id value, the save() method performs an insert with the specified fields in the document.
If a document exists with the specified _id value, the save() method performs an update, replacing all field in the existing record with the fields from the document.

Tuesday, May 26, 2015

Child Actions in ASP.NET MVC

Child Actions are the action methods which can be invoked within the view. This is used to work with the data in the view, which are not related to the main action method. For example, if you want to create a data driven widget in the view using data that is not related to the main action method, you will need to use the child action method.
In ASP.NET MVC any action can be used as a child action. However, to use an action only as a child action and attribute it with the ChildActionOnly. It will make sure the action is not called by any user request and will only be used in the view. A child action can be created as shown below:
This child action returns a partial view with the current time as the data in the partial view. The partial view is created as below:
In the main view, the child action can be used as shown below:
Above we are using child action from the same controller in which the main action is defined. To use child actions from a different controller, you have the option of passing the controller name as well. You can also pass input parameters to the child action. To demonstrate this, let us create the child action with an input parameter as follows:
In the view, a parameter can be passed to the child action as follow:
You may consider using the child action method in the following scenarios,
  • To use data in the view which is not related to the main controller
  • To create data driven widgets to be used on different views

Custom Action Filter in ASP.NET MVC 5

ASP.NET MVC 5 provides five different kinds of Filters. They are as follows:
  1. Authentication [Introduced in MVC5]
  2. Authorization
  3. Action
  4. Result
  5. Exception
Filters are used to inject logic at the different levels of request processing. Let us understand where at the various stages of request processing different filters get applied.
  • Authentication filter runs before any other filter or action method
  • Authorization filter runs after Authentication filter and before any other filter or action method
  • Action filter runs before and after any action method
  • Result filter runs before and after execution of any action result
  • Exception filter runs only if action methods, filters or action results throw an exception
I have tried to show the filter execution timing in context of request processing in the below diagram:
Action Filter
An action filter consists of codes that run either before or after an action runs. It can be used for tasks like logging, privileged based authorization, authentication, caching etc.
Creating a custom action filter is very easy. It can be created in four simple steps:
  1. Create a class
  2. Inherit ActionFilterAttribute class
  3. Override the OnActionExecuting method to run logic before the action method
  4. Override the OnActionExecuted method to run logic after the action method
Let us see how we can create a custom action filter. The purpose of the action filter is to find whether a logged in user belongs to a particular privileges or not. On the basis of the result, a user will access a particular action or navigate to the login action. To do this, I have created a class and extended the ActionFilterAttribute class.
As you see in the above code, the OnActionExecuting method is overridden because we want the code to execute before the action method gets executed. Once the action filter is created, it can be used in three ways:
  1. As Global filter
  2. As Controller
  3. As Action
By adding a filter to the global filter in App_Start\FilterConfig it will be available globally to the entire application.
By adding a filter to a particular Controller it will also be available to the all actions of that particular controller.
By adding a filter to a particular action it will be available to the particular action.
        public ActionResult About()

Custom HTML Helper for MVC Application

The solution to avoid this is to build a custom HTML helper to encapsulate everything and make the repetitive task less cumbersome.
Let's say we want to avoid doing this many times inside your views:
<img src="@myModel.MyObject.ImageSource", alt ="@myModel.MyObject.Imagename",
title ="@myModel.MyObject.ImageDescription" /> 
And instead, you want something Razor like that may take care of some logic or validations to avoid error because in the previous snippet, the ImageSource or Imagename or again the description may be null:
@myModel.MyObject.Imagename, @myModel.MyObject.ImageDescription) 
namespace MyNamespace 
    public static class MyHeleprs
        public static MvcHtmlString Image(this HtmlHelper htmlHelper, 
        	string source, string alternativeText)
            //declare the html helper 
            var builder = new TagBuilder("image"); 
            //hook the properties and add any required logic
            builder.MergeAttribute("src", source);
            builder.MergeAttribute("alt", alternativeText);
            //create the helper with a self closing capability
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
To make this helper available in your view, add its namespace as follows:
@namespace MyNamespace 
But if you want it available in all your views, you have to add the namespace not to a specific view but to the collection of namespaces in views' web.config:
<add namespace="MyNamespace" /> 
Now, the Image helper is available everywhere in your views.