Thursday, February 3, 2011

Interface

An Interface is not a class. It is an entity that is defined by the word Interface . An Interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to abstract class it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one Interface but can only inherit from oneabstract class. Since C# doesn't support multiple inheritance, Interface are used to implement multiple inheritance.

Abstract Class

An absrract class is a special kind of class that cannot be instantiated. So the question is why we need a class   that cannot be instantiated? An absrract class  is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

How can exception be handled with out the use of try catch?

using Exception Management application block
or
Page_error
Application_error objects

Caching

In a very simple term caching is process of storing data in memory, frequently used data which is very costly to reproduce, is kept in a memory and re used. One of the ways to improve the throughput is to use caching in web pages. Caching is a tried and tested technique for performance improvement. HTML pages have been cached to improve the speed. An indirect example of caching could be connection pooling. 

ASP.Net supports caching the Asp.net response page that is generated by requests and also storing individual objects in memory. 

Three type of Caching in Asp.net
  • Page Output Caching ·

  • Page Fragment Caching

  • Data Caching


  • Delete Items from CacheItems are deleted from cache when the expiration is set or when the dependency file changes or when the server needs to free memory.


     Cache.Remove("oString");

    Monday, January 31, 2011

    3-tier architecture.

    We generally split our application into 3-Layers1)Presentation Layer ( Where we keep all web forms Master Pages and User Controls).
    2)Business Layer (Where we keep business logic). e.g Code related to manipulating data Custom Exception classes Custom Control classes Login related code if any etc. etc.
    3)Data Access Layer (Where we keep code used to interact with DB). e.g. We can have the methods which are using SQL Helper (Application Block).

    Events occur when a page is loaded

    Below are the events occures during page load.
    1) Page_PreInit
    2) Page_Init
    3) Page_InitComplete
    4) Page_PreLoad 

    Life cycle of an ASP .NET page

    Following are the events occur during ASP.NET Page Life Cycle:

    1)Page_PreInit
    2)Page_Init
    3)Page_InitComplete
    4)Page_PreLoad
    5)Page_Load
    6)Control Events
    7)Page_LoadComplete
    8)Page_PreRender
    9)SaveViewState
    10)Page_Render
    11)Page_Unload

    SQL Standard date formats

    ANSI Standard - YYYY.MM.DD
    SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]

    ISO Standard - YYYYMMDD
    SELECT CONVERT(VARCHAR(8), GETDATE(), 112) AS [YYYYMMDD]

    USA Standard - MM/DD/YYYY
    SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY]

    British/French Standard - DD/MM/YYYY
    SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY]

    German Standard  - DD.MM.YYYY
    SELECT CONVERT(VARCHAR(10), GETDATE(), 104) AS [DD.MM.YYYY]

    Italian Standard  - DD-MM-YYYY
    SELECT CONVERT(VARCHAR(10), GETDATE(), 105) AS [DD-MM-YYYY]

    Avoid Duplicate record insert on page refresh using ASP.NET

     One of most common issue which many of the web developers face in their web applications, is that the duplicate records are inserted to the Database on page refresh. If the web page contains some text box and a button to submit the textbox data to the database. In that case when the user insert some data to the textbox and click on the submit button, it will save the record to the Database and then if the user refresh the web page immediately then the same record is again saved to the database as there is no unique keys that can be used to verify the existence of the data, so as to prevent the multiple insertion.

    From this behavior we can definitely know that, on the page fresh the  button click event is fired.
    To avoid this problem we can try this method as discuss below.

    On page load event save the date/time stamp in a session variable, when the page is first loaded, a Session variable is populated with the current date/time as follows:

    C# Code:

    void Page_Load(Object sender, EventArgs e)
    {
    if(!IsPostBack)
          {
          Session["update"] =  Server.UrlEncode(System.DateTime.Now.ToString());
    }
    }

    On the page's PreRender event, a ViewState variable is set to the value of the Session variable as follows:

        void Page_PreRender(object obj,EventArgs e)
        {
            ViewState["update"] = Session["update"];
        }

    Then these two values are compared to each other immediately before the database INSERT command is run. 
    If they are equal, then the command is permitted to execute and the Session variable is updated with the current date/time, otherwise the command is bypassed