Monday, February 9, 2009

ASP.NET Page Lifecycle

From now and then I forget the life cycle of ASP.NET pages.
In the CodeProject website I found a wonderfull article: http://www.codeproject.com/KB/aspnet/ASPNET_Page_Lifecycle.aspx

If you really don't want to read, here is what you need to know and memorize (SILVER):
S - Start
I - Initialize
L - Load
V - Validate
E - Event Handling
R - Render



Some add "U" (SILVER-U) for "Unload"

And in a more detailed way, the methods are:

  1. Construct
  2. ProcessRequest
  3. FrameworkInitialize
  4. InitializeCulture
  5. If child controls are present:
    1. AddParsedSubObject
    2. CreateControlCollection
    3. AddedControl
    4. ResolveAdapter
  6. DeterminePostBackMode
  7. OnPreInit
  8. OnInit
  9. TrackViewState
  10. OnInitComplete
  11. OnPreLoad
  12. OnLoad
  13. OnLoadComplete
  14. EnsureChildControls
    1. CreateChildControls
  15. OnPreRender
  16. OnPreRenderComplete
  17. SaveViewState
  18. OnSaveStateComplete
  19. CreateHtmlTextWriter
  20. RenderControl
  21. Render
    1. RenderChildren
    2. VerifyRenderingInServerForm
  22. OnUnload
  23. Dispose

Hope it'll help you in any way.... I know it helps me.

Monday, February 2, 2009

SQL Server 2005 CTE and DateTime parsing

CTE - Common Table Expressions
On various occasions I come with a need to use recursive iterations in a stored procedure.
Since server programing is not what I usually do, I search, find and use CTE (Common Table Expressions)
A wonderfull reference I found in an article by Nigel Rivett on:
http://www.simple-talk.com/sql/sql-server-2005/sql-server-2005-common-table-expressions/

A sample for a recursive use with recursion position flag:


with MyCTE (r1, r2, i, x)
as (
select r1 = 1, r2 = 1, i = 0, x = convert(varchar(1000),'hello')
union all
select r1 = r1 + 1, r2 = r2, i = 1, convert(varchar(1000),x + 'a') from MyCTE
where len(x) < 10 and i in (0,1)
union all
select r1 = r1, r2 = r2 + 1, i = 2, convert(varchar(1000),x + 'b') from MyCTE
where len(x) < 10 and i in (0,2))
select r1, r2, x
from MyCTE
order by len(x), x
The output for this is:
1 1 hello
2 1 helloa
1 2 hellob
3 1 helloaa
1 3 hellobb
1 4 hellobbb
1 5 hellobbbb
1 6 hellobbbbb

DateTime parsing
Another issue that parsing DateTime datatype. In SQL 2000 I had to convert the DateTime into varchar/nvarchar (depending on the collation), split (another function) and parse the relevant thing I wanted (day, month, hour etc). In SQL 2005, Microsoft introduced us to a new and usefull function called DatePart. This function does the entire trick.
The way to use it, is just say what you want from the DateTime variable and voila...

DATEPART(datepart, date)
The dateparts can be as follows:
  • year (yy, yyyy)
  • quarter (qq, q)
  • month (mm, m)
  • dayofyear (dy, y)
  • day (dd, d)
  • week (wk, ww)
  • weekday (dw)
  • hour (hh)
  • minute (mi, n)
  • second (ss, s)
  • millisecond (ms)

Samples can be seen on:
http://msdn.microsoft.com/en-us/library/ms174420(SQL.90).aspx (SQL Server 2005)
http://msdn.microsoft.com/en-us/library/ms174420.aspx (SQL Server 2008)