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