Sunday, November 20, 2011

ASP.NET page lifecycle

Here are the details about ASP.NET page life cycle. Browser Sends a request to IIS. IIS completes following 2 steps:
 - ASP.Net Environment ( App, Request,Response and context object creation)
 - Process request ( handers,modules and page events).
Environment Creation:
Browser sends Request to IIS. IIS decides who will serve the request. In IIS properties there are ISAPI extensions installed, which actually keep information about dll who is going to take care of which extenstion. For example for .aspx the dll is  aspnet_isapi.dll.
If it is a 1st request then using ApplicationManager IIS creates new application domain, This application domain creates the hosting environment.
Create HttpApplication Object.
Then core objects like HTTPRequest,HTTPResponse and HTTPContext are created and assigned to HttpApplication object. If there are 2nd or 3rd request then this HttpApplication object is reused(or can be new also,depending upon settings). If we have global.asax file in our website, then global.asax object is also created.
Process Request:




There are different stages of processing the request.
Page request:


1) occurs before the page life cycle begins.
2) At this stage ASP.NET determines whether the page needs to be parsed and compiled or whether a cached version of the page can be sent in response without running the page.
Start:
1) page properties such as Request and Response are set.
2) Determine whether the request is a postback or a new request and sets the IsPostBack property. The page also sets the UICulture property.
Initialization:
1) Controls on the page are available and each control's UniqueID property is set.
Load:
1) If the current request is a postback, control properties are loaded with information recovered from view state and control state.
Postback event handling:
1) If the request is a postback, control event handlers are called.
2) The Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page. (There is an exception to this sequence: the handler for the event that caused validation is called after validation.)
Rendering:
1)Before rendering, view state is saved for the page and all controls.
2) During the rendering stage, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream object of the page's Response property.
Unload:
The Unload event is raised after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and cleanup is performed.

LifeCycle event:
Here are the list of events that play their role during asp.net page life cycle:

PreInit:
Raised after the start stage is complete and before the initialization stage begins.
Use this event for the following:
-Check the IsPostBack property to determine whether this is the first time the page is being processed. The IsCallback and IsCrossPagePostBack properties have also been set at this time.
-Create or re-create dynamic controls.Set a master page dynamically.
-Set the Theme property dynamically.Read or set profile property values.
If the request is a postback, the values of the controls have not yet been restored from view state. If you set a control property at this stage, its value might be overwritten in the next event.
Init:
Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page. The Init event for each child control occur before the corresponding event is raised for its container (bottom-up).
Use this event to read or initialize control properties.
InitComplete:
Raised at the end of the page's initialization stage. Only one operation takes place between the Init and InitComplete events: tracking of view state changes is turned on. View state tracking enables controls to persist any values that are programmatically added to the ViewState collection. Until view state tracking is turned on, any values added to view state are lost across postbacks. Controls typically turn on view state tracking immediately after they raise their Init event.
Use this event to make changes to view state that you want to make sure are persisted after the next postback.
PreLoad:
Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance.
Load:
The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page.The Load event for a container occurs before the Load events for its child controls (top-down). Use the OnLoad event method to set properties in controls and to establish database connections.
NoteNote:In a postback request, if the page contains validator controls, check the IsValid property of the Page and of individual validation controls before performing any processing.
LoadComplete:
Raised at the end of the event-handling stage. Use this event for tasks that require that all other controls on the page be loaded.
PreRender:
Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls.The PreRender event of individual controls occurs after the PreRender event of the page. Use the event to make final changes to the contents of the page or its controls before the rendering stage begins.
PreRenderComplete:
Raised after each data bound control whose DataSourceID property is set calls its DataBind method.
SaveStateComplete:
Raised after view state and control state have been saved for the page and for all controls. Any changes to the page or controls at this point affect rendering, but the changes will not be retrieved on the next postback.
Render:
This is not an event; instead, at this stage of processing, the Page object calls this method on each control. All ASP.NET Web server controls have a Render method that writes out the control's markup to send to the browser. A user control (an .ascx file) automatically incorporates rendering, so you do not need to explicitly render the control in code.
Unload:
Raised for each control and then for the page. The Unload event for each child control occur before the corresponding event is raised for its container (bottom-up) In controls, use this event to do final cleanup for specific controls, such as closing control-specific database connections. During the unload stage, the page and its controls have been rendered, so you cannot make further changes to the response stream. If you attempt to call a method such as the Response.Write method, the page will throw an exception.





Reference: http://msdn.microsoft.com/en-us/library/ms178472.aspx

Saturday, November 19, 2011

DataBase Basic1 (Inner and outer join)

Que: What is Referential integrity?
Ans: Referential integrity is database principle which ensure references between data is valid and intact. It prevents user/application to enter invalid data into database.

Que: What is difference between inner join and outer join?
Que: We will try to understand the difference by an example. Suppose we have 2 tables:
Table A              Table B
Id   Name           Id   Name
1    Pankaj          1    Subbu
2    Aman            2    Pankaj
3    Abhay           3    Anil
4    Manish          4    Abhay
Inner Join:It is the intersection of the tables i.e. the rows they have in common.
So the following inner join query will return the result as follows:

SELECT * FROM TableA INNER JOIN TableB
ON TableA.Name = TableB.Name

id   name          id   name
1   Pankaj        2    Pankaj
3   Abhay         4    Abhay

Left Outer Join:
A left outer join will give all rows in A, plus any common rows in B.

SELECT * FROM TableA LEFT OUTER JOIN TableB
ON TableA.Name = TableB.Name

id      name          id        name
1      Pankaj        2         Pankaj
2      Aman          null       null
3      Abhay         4         Abhay
4      Manish        null        null

Full outer join:
A full outer join will give you the union of A and B, i.e. All the rows in A and all the rows in B.

SELECT * FROM TableA FULL OUTER JOIN TableB
ON TableA.name = TableB.name

id      name          id      name
1       Pankaj       2       Pankaj
2       Aman        null    null
3       Abhay       4       Abhay
4      Manish      null     null
null     null           1      Subbu
null     null           3      Anil

Que: Write a query to achieve following result.
1)







Ans:  SELECT * FROM TableA INNER JOIN TableB ON TableA.name = TableB.name

2)








Ans:  SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.name = TableB.name

3)







Ans:  SELECT * FROM TableA LEFT OUTER JOIN TableB ON TableA.name = TableB.name

4)







Ans: SELECT * FROM TableA LEFT OUTER JOIN TableB ON TableA.name = TableB.name
        WHERE TableB.id IS null
5)







Ans: SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.name = TableB.name
         WHERE TableA.id IS null  OR TableB.id IS null