Articles in my blogs aims to share knowledge in terms of how can we engineer healthier systems for enterprise in today’s life.
Tuesday, September 30, 2025
Tuesday, July 30, 2013
Dynamically Create tables Based on Config Table Data TSQL
USE [SQL2012] GO IF EXISTS ( SELECT * FROM sys.tables WHERE NAME = 'tableconfig' ) BEGIN DROP TABLE tableconfig; END GO CREATE TABLE TableConfig ( TableName VARCHAR(100) ,ColumnName VARCHAR(100) ,ColumnDataType VARCHAR(100) ) GO INSERT INTO TableConfig VALUES ( 'Table 1' ,'Column 1' ,'Varchar(100)' ) ,( 'Table 1' ,'Column 2' ,'Varchar(100)' ) ,( 'Table 1' ,'Column 3' ,'Varchar(100)' ) ,( 'Table 2' ,'Column 1' ,'Varchar(100)' ) ,( 'Table 3' ,'Column 1' ,'Varchar(100)' ) ,( 'Table 3' ,'Column 2' ,'Varchar(100)' ) GO /*SELECT * FROM TableConfig GO */ DECLARE @sqlquery NVARCHAR(4000) = '' DECLARE @startRow INT = 1; DECLARE @endRow INT = 1; DECLARE @startcolumn INT = 1; DECLARE @endcolumn INT = 1; DECLARE @tableToCreate VARCHAR(100) = ''; DECLARE @columnName VARCHAR(100) = ''; DECLARE @DataType VARCHAR(100) = ''; WITH MyTables AS ( SELECT DISTINCT TableName FROM TableConfig ) SELECT @endRow = count(*) FROM MyTables; /*PRINT @endRow*/ WHILE (@startRow <= @endRow) BEGIN WITH AllTables AS ( SELECT ROW_NUMBER() OVER ( ORDER BY tablename ) AS Rownum ,TableName FROM TableConfig GROUP BY TableName ) SELECT @tableToCreate = TableName FROM Alltables WHERE Rownum = @startRow SET @sqlquery = ''; SET @sqlquery = @sqlquery + 'IF EXISTS ( SELECT * FROM sys.tables WHERE NAME = ' + '''' + @tableToCreate + '''' + ' ) BEGIN DROP TABLE ' + '[' + @tableToCreate + ']' + ';' + CHAR(13) + CHAR(10) + 'END' + CHAR(13) + CHAR(10) + 'GO' + CHAR(13) + CHAR(10) + 'CREATE TABLE ' + '[' + @tableToCreate + ']' + '(' /* PRINT @tableToCreate; PRINT @endcolumn */ SET @startcolumn = 1; SELECT @endcolumn = count(*) FROM TableConfig WHERE TableName = @tableToCreate; WHILE (@startcolumn <= @endcolumn) BEGIN WITH Allcolumns AS ( SELECT ROW_NUMBER() OVER ( ORDER BY columnname ) AS Rownum ,ColumnName ,ColumnDataType FROM TableConfig WHERE TableName = @tableToCreate ) SELECT @columnName = ColumnName ,@DataType = ColumnDataType FROM Allcolumns WHERE Rownum = @startcolumn /* PRINT @columnName PRINT @DataType */ SET @sqlquery = @sqlquery + '' + '[' + @columnName + ']' + ' ' + @DataType IF @startcolumn < @endcolumn SET @sqlquery = @sqlquery + ','; SET @startcolumn = @startcolumn + 1; END /*New Line */ SET @sqlquery = @sqlquery + ');' + CHAR(13) + CHAR(10) + 'GO'; PRINT @sqlquery SET @startRow = @startRow + 1; END IF EXISTS ( SELECT * FROM sys.tables WHERE NAME = 'tableconfig' ) BEGIN DROP TABLE tableconfig; END GO
Output
------------------------
IF EXISTS ( SELECT * FROM sys.tables WHERE NAME = 'Table 1' ) BEGIN DROP TABLE [Table 1]; END GO CREATE TABLE [Table 1] ( [Column 1] VARCHAR(100) ,[Column 2] VARCHAR(100) ,[Column 3] VARCHAR(100) ); GO IF EXISTS ( SELECT * FROM sys.tables WHERE NAME = 'Table 2' ) BEGIN DROP TABLE [Table 2]; END GO CREATE TABLE [Table 2] ([Column 1] VARCHAR(100)); GO IF EXISTS ( SELECT * FROM sys.tables WHERE NAME = 'Table 3' ) BEGIN DROP TABLE [Table 3]; END GO CREATE TABLE [Table 3] ( [Column 1] VARCHAR(100) ,[Column 2] VARCHAR(100) ); GO
--------------------------------
Thanks,
Prajesh
Any comments or feedback will be highly appreciated.
Monday, July 29, 2013
I have to write a SQL statement to create a trigger called "ValidOrder" checking the value of a new order inserted into the OrderDetails table is greated that$100. If the value is less that$100, the entry needs to be rolled back.
CREATE TABLE OrderDetails
(
OrderId INT identity(1, 1),
OrderName VARCHAR(50),
OrderValue MONEY
) GO CREATE TRIGGER dbo.ValidOrder ON dbo.OrderDetails AFTER INSERT, UPDATE AS BEGIN SET NOCOUNT ON; BEGIN TRANSACTION DECLARE @OrderValue FLOAT SELECT @OrderValue = OrderValue FROM inserted IF @OrderValue > 100 BEGIN COMMIT TRANSACTION PRINT 'Record inserted' END ELSE BEGIN ROLLBACK TRANSACTION PRINT 'Record rolled back as order value is less than $100' END END GO INSERT INTO OrderDetails (OrderName, OrderValue) VALUES ('Order1', 55) /* Record rolled back as order value is less than $100 Msg 3609, Level 16, State 1, Line 1 The transaction ended in the trigger. The batch has been aborted. */ INSERT INTO OrderDetails (OrderName, OrderValue) VALUES ('Order1', 500) /* Record inserted (1 row(s) affected) */
Thursday, July 25, 2013
Wednesday, July 24, 2013
Get the Central European Time in SQL Server
Here it the function I created to which return the same and it manage the day light saving internally.
CREATE FUNCTION ufs_Datetime_GetCETTime RETURNS DATETIME AS BEGIN DECLARE @DstStart DATETIME DECLARE @DstEnd DATETIME DECLARE @CETDateTime DATETIME DECLARE @UTCDateTime DATETIME SET @UTCDateTime = GETUTCDATE() SELECT @DstStart = DATEADD(hour, 1, DATEADD(day, DATEDIFF(day, 0, '31/Mar' + CAST(YEAR(@UTCDateTime) AS VARCHAR)) - (DATEDIFF(day, 6, '31/Mar' + CAST(YEAR(@UTCDateTime) AS VARCHAR)) % 7), 0)) ,@DstEnd = DATEADD(hour, 1, DATEADD(day, DATEDIFF(day, 0, '31/Oct' + CAST(YEAR(@UTCDateTime) AS VARCHAR)) - (DATEDIFF(day, 6, '31/Oct' + CAST(YEAR(@UTCDateTime) AS VARCHAR)) % 7), 0)) SELECT @CETDateTime = CASE WHEN @UTCD ateTime & lt;= @DstEnd AND @UTCDateTime & gt;= @DstStart THEN DATEADD(hour, + 2, @UTCDateTime) ELSE DATEADD(hour, + 1, @UTCDateTime) END RETURN @CETDateTime END
Comments awaited.. Thanks, Prajesh
Column Length Distribution profile in SSIS Profiler
E.g the below example
WITH cte AS ( SELECT LEN(NULL) AS [Column Length] UNION ALL SELECT LEN(100) AS [Column Length] UNION ALL SELECT LEN(101) AS [Column Length] UNION ALL SELECT LEN(102) AS [Column Length] UNION ALL SELECT LEN(103) AS [Column Length] ) SELECT max([Column Length]) AS [Max Column Length] ,MIN([Column Length]) AS [MIN Column Length] FROM cte OUTPUT OF the above query will be [Max Column Length] = 3 ,[MIN Column Length] = 3
So if the column value in your table is NULL, the lenght for NULL is always NULL.
Compress the Large XML Files using 7zip C# API
You need 2 external files to create a project/solution to zip files using this SevenZip C# Code SevenZipSharp.dll ,
get this from http://sevenzipsharp.codeplex.com/ the above file is a .net assembly and cab be directly referenced in C# code 7-zip.dll, get it from http://www.7-zip.org/
This is not a .net assembly but a COM Use the below code to build the solution, this code is compressing around 6.5 GB files to 1 MB in 2 minutes.
class Program { static void Main(string[] args) { Console.WriteLine("Enter the 7zip dll location :"); string zipfilenametozip = Console.ReadLine(); SevenZip.SevenZipBase.SetLibraryPath(zipfilenametozip); MemoryStream ms = new MemoryStream(); MemoryStream compressedStream = new MemoryStream(); SevenZipSevenZipCompressor compressor = new SevenZipSevenZipCompressor(); compressor.CompressionMethod = SevenZip.CompressionMethod.Lzma2; compressor.CompressionLevel = SevenZip.CompressionLevel.Low; compressor.CompressStream(ms, compressedStream); compressedStream.Position = 0; Console.WriteLine("Enter the uncompressed File Name with location to Compress (Zip) :"); string filenametozip = Console.ReadLine(); Console.WriteLine("Enter the File Name with location for the Compressed zip File :"); string filenamezipped = Console.ReadLine(); Console.WriteLine("Zipping started for file Name : " + filenametozip + " at " + DateTime.Now.ToString()); string[] myfiles = { filenametozip }; compressor.CompressFiles(filenamezipped, myfiles); Console.WriteLine("Zipping completed : " + filenamezipped + " at " + DateTime.Now.ToString()); } }
Please comment if you face any issues in using this code... Thanks, Prajesh Jha
Bulk update of image data in MSSQL
/* create a folder C:\Images\ with all the compressed images */
USE GO CREATE TABLE dbo.tblImages ( id INT identity(1, 1) PRIMARY KEY ,FlowerName VARCHAR(100) NULL ,FlowerImage IMAGE ) GO INSERT INTO dbo.tblImages ( FlowerName ,FlowerImage ) SELECT 'Flower1' ,BulkColumn FROM Openrowset(BULK 'C:\Images\1.jpg', Single_Blob) AS img GO INSERT INTO dbo.tblImages ( FlowerName ,FlowerImage ) SELECT 'Flower2' ,BulkColumn FROM Openrowset(BULK 'C:\Images\2.jpg', Single_Blob) AS img GO INSERT INTO dbo.tblImages ( FlowerName ,FlowerImage ) SELECT 'Flower3' ,BulkColumn FROM Openrowset(BULK 'C:\Images\2.jpg', Single_Blob) AS img GO
Microsoft ODBC Driver for SQL Server on Linux
Below link can help you to download the Microsoft ODBC Driver for SQL Server on Linux
http://www.easysoft.com/products/data_access/odbc-sql-server-driver/index.html
Thursday, May 24, 2007
ASP.NET 2.0 Provider Model
The ASP.NET 2.0 provider model was designed with the following goals in mind:
- To make ASP.NET state storage both flexible and extensible
- To insulate application-level code and code in the ASP.NET run-time from the physical storage media where state is stored, and to isolate the changes required to use alternative media types to a single well-defined layer with minimal surface area
- To make writing custom providers as simple as possible by providing a robust and well-documented set of base classes from which developers can derive provider classes of their own
It is expected that developers who wish to pair ASP.NET 2.0 with data sources for which off-the-shelf providers are not available can, with a reasonable amount of effort, write custom providers to do the job
For more click here http://msdn2.microsoft.com/en-us/library/aa479030.aspx
cheeers
Visual Studio Orcas
http://msdn2.microsoft.com/en-us/vstudio/aa700830.aspx
LINQ Project in C# 3.0
Go through these links to know more about LINQ Project isn C# 3.0
http://msdn2.microsoft.com/en-us/library/aa479865.aspx
http://download.microsoft.com/download/6/2/e/62e1f196-54e5-485f-a31d-c7b384428564/AndersLinqFunctional.zip
More than just a Web server
With IIS7 in Longhorn Server Beta 3 we can
- Reduce attack surface, footprint and patching with fully customizable install
- Enjoy greater reliability and security with automatic sandboxing of new sites
- XCopy deploy config beside code and content with new config system
- Share configuration across a Web farm by sharing configuration
- Administer the server easily and efficiently using powerful new admin tools
- Minimize downtime through detailed diagnostics and troubleshooting tools
cheeers.....
Tuesday, January 16, 2007
Love Story of a Software Engineer
Friday night…he slept very late, I guess around 2 a.m. He has planned dating with his beautiful girlfriend. He has talked her on phone till 2 planning for tomorrow.
Saturday morning, He got up very early regardless of the fact that He slept so late, around 6 a.m. I estimate.
He was very joyful about her meeting. He loves her very much; He can do anything for her. She also loves her too much.
He got organized around 8 AM and about to run off for the meeting his sweetie.
“When will you come back in evening”? I asked him
“I will be back at 8 PM”...He answered me
As soon as he finished his last word. His phone ringed.
“Hello…”...He spoke.
“Can you come to the office today till 10 AM, there is a crisis delivery today, and since we got call from US last night around 2 AM (IST), I could not tell you, yesterday”…His Boss told...
“Sure, I will be there in another 2 hours, don’t worry” … He replied
I am not sure, how much his heart broken that day. But I was very sad that day for my buddy. I guess this is the climax of a Software Engineer’s dating…that day…
Better for enterprise but what about human emotions.
Comments would be highly appreciated
Sunday, December 24, 2006
SOME LOW LEVEL CONCEPTS
Let’s start with a very simple “Hello world” Program here. What we do, open a notepad, write the program and save it to some location. (People who use Visual studio.net can use the templates provide by Microsoft for creating simple console application).
Let’s say I have written a program “HelloWorld.cs” and saved it to “C: Drive”. Let me tell you the steps what happens one by one. After saving the program the next step is to compile the program using some compiler, in our case the program is written in C# so we will use a C# compiler. It does make any difference in which language we write program for compiler, e.g. if we write program in C#, we have to use C# complier and same for VB, J#, Python etc.
After compilation process, regardless of which compiler we use, the result is a managed module. A managed module is a standard 32 bit Microsoft Windows Portable Executable (PE32) or a standard 64 bit windows portable executable (PE32+) file. This means that if we are targeting 32 bit platforms then the result will be PE32 file and PE32+ for 64 bit platforms like Windows XP 64 bit operating systems.
Now after compilation process, we got the managed module. I must tell you, what we have in these PE files.
The first stuff that PE (Managed Module) contains is the PE header, which can be PE32 (if file is targeted to run both on Windows 32 bit or Windows 64 bit versions) or PE32+( if file is targeted to run only on 64 bit versions of windows). This header also includes the type of file, which can be GUI (Graphical User Interface), CUI(Character User Interface) or DLL(Dynamic Link Library) and also a timestamp which tell us that when the file was built.
The second stuff is the CLR Header, before telling you about CLR header, let me clear you the fundamentals of CLR, CLR stands for Common Language Runtime, as the name suggests it is the runtime that is used by different and varied programming language, CLR has no idea which programming language the developer used for the source code. Fair enough about CLR, now come to the point, i.e. concept of CLR header. It contains the information that makes the managed module a managed module.
The CLR header includes.
(a) Version of the CLR required
(b) Some Flags
(c) Main Method
(d) Location and size of the
a. Managed Module metadata
b. Resources
c. Strong Name
d. Some Flags
e. And other less interesting stuffs
The third stuffs that Managed module contains are the METADATA.
There are two main types of tables in.
(a) Tables that describe the types and members defined in “HelloWorld.cs”
(b) Tables that describe the types and members referenced by our source code i.e. “HelloWorld.cs”
The fourth stuff that we have in Managed module is INTERMEDIATE LANGUAGE CODE also called MSIL or shortly IL Code or sometimes MANAGED CODE because CLR manages its execution. This is the code that compiler produces as it compile the source code.
One or more managed modules with Resource files (Optional) are converted to assemblies which also contains MENIFEST other than these stuffs. It is ASSEMBLY that is targeted by CLR for execution.
Hope you are bit clear with the fundamentals of MANAGED MODULES. Comments will be highly appreciated.
Tuesday, December 19, 2006
Ye KT kya hai bhai..(The power of Knowlwdge Transfer Session)

I got a call from him that he has to cancel his flight.
I asked “Why so?”
He told me that since some new bugs has come this morning so he has to stay in the office to keep track of bug fixing and of course to help his team members to engineer the same.
He was quite upset because of the cancellation of the flight.
I asked him the fundamental reasons why you need to stay there as there is lot of other team member are there who can happily handle these bugs.
He replied “I am the only people who have complete idea of functionality as well as technology”
I surprised and told him why didn’t you given any Knowledge Transfer Sessions to your team members, when ever you were having no any bugs.
He replied “I think you are quite correct, but I never got time to do that.”
Now a day we give more time to achieve the deadlines rather than creating better quality software, I mean healthier systems. People have less time to enhance quality than engineer the basic system, which is the reason I think support projects are more in number these days.
What happen to this project if he will leave the company after a period?
Now a day where attrition rate is so high, the concept of KT (Knowledge Transfer Sessions) is very much required. Even if no team member is supposed to leave the company or project, we should create a habit of knowledge sharing which enhance our product knowledge as well as our interest in the project. Working in project does mean that if you are working on module A then you must have some high-level view of the module B as well as high level functionality of the project (Low level functionality is better).
Sunday, December 17, 2006
Which is better to do J2EE or MS.NET from Industry Prospective?
I met one of old friend of mine on MCA examination center in New Delhi. He has completed BIT from IGNOU with me only in 2004.
I reached the examination center very early, I guess around 12:30 PM (IST) (Exam was supposed to start at 2 PM), so I was having too much time to relax before the examination, I was just scrambling around the MCA examination center aiming to find any of my old friend so that we can have a chat, of course on past life at PCTI, Pitampura, it was my Learning center for my BIT course.
Finally around 1:00 PM (IST) I met the guy I have just talked. He was just surprised to see me that I have also come for the examination. After a little bit old gossips. He came to the point and asked me the same question that my batch mates ask me.
“Which is better to do J2EE or MS.NET from Industry Prospective?”
I took few seconds to tell him “Go for .NET technologies”
And as normal phenomenon, he asked me again “Why so…why not J2EE…”
After that we just talked about the benefits of .NET over J2EE.
The first point I have discussed that Ease of Use and easy to Learn
Microsoft .NET offers a better integrated lower cost, easier to use, and more manageable environment for software development than the Java J2EE platform. It also offers a much better way to take advantage of low-cost Intel-based servers for enterprise-scale applications.
Relative to .NET, enterprise software development on the J2EE platform is like trying to count a herd of sheep by counting the legs and dividing by four. Sure, it can be done, but it takes longer, costs more, and is harder to change in response to future business challenges and opportunities.
A few years ago J2EE was a more competitive option because the Windows/Intel server platform was still not quite ready for very large-scale enterprise application deployment. That is no longer so. With Windows Server 2003, increasingly capable Intel-based server hardware, and the rapidly maturing Window/Intel server applications from Microsoft and other software vendors, Microsoft .NET is now able to meet even very large-scale enterprise applications requirements.
He asked again “What circumstances favor .NET over J2EE?”
The most important advantage for .NET over J2EE is in circumstances where minimization of total cost is a high priority. Notwithstanding "figures don't lie but . . ." claims to the contrary, Windows on Intel servers delivers much more bang for the buck than the mainframe or UNIX-based platforms typically used for J2EE deployment. If total cost of ownership (TCO) really matters then .NET is the obvious choice over J2EE.
A second important advantage for .NET over J2EE is anywhere Windows-based server infrastructure and related development and deployment skills are already in place. An organization with skilled BASIC, C, C++, and COBOL programmers already in place, that is already familiar with the Windows development environment, will get much further much faster and at much lower cost using .NET than by trying to turn everyone into Java programmers.
Third, projects aiming to take advantage of the opportunities created by the new Web services standards favor .NET over J2EE as well. Although the Java world is working hard to catch up, crucial Web services standards like XML, SOAP, and WSDL are built into .NET by design while they are still only, in effect, 'bolted on' to J2EE. Development and deployment of Web services applications is significantly easier, faster, and less costly on the .NET platform than it is on J2EE.
Overall, .NET is the better choice.
But I guess some of my friends who are working with J2EE technologies convinced him that J2EE is better and since we have also learned Java better in BIT course J2EE will be better option for you.
So he asked again “Are there any circumstances that favor J2EE over .NET?”
The obvious one is when applications, for one reason or another, absolutely must run on something other than Windows on Intel systems. If an application really must run on, say, IBM mainframes or Sun Solaris boxes, then Java/J2EE may be the only option (albeit a costly one).
The second is simply prior commitment to the Java platform and associated ready availability of Java/J2EE development, deployment and administration skills in-house. In effect, the more an organization or some organizational sub-unit is already using Java/J2EE, the more circumstances are likely to favor continuing to do so. But despite promises of easy, rapid development, the J2EE platform is a daunting one with a steep and difficult learning curve. So J2EE is a plausible option only for organizations that have already climbed that slope and paid the price of learning the platform -- not for those that have not already done so.Third, J2EE is a somewhat more appealing choice for organizations where there is a large proportion of existing UNIX-based IT infrastructure (e.g. Solaris, HP/UX, AIX and so forth) already in place. As a rough rule of thumb, the greater the proportion of UNIX-based servers already in place, the greater the relative advantage for J2EE versus .NET.
Then I told him a story to explain him that how big organizations are using .NET for building Web applications.
MIT have chosen .NET over J2EE as the toolkit for developing Internet applications. MIT comes from the open-source and UNIX world (and a lot of people there do anything they can to avoid anything Microsoft-related), so it would make sense for them to choose a more vendor-neutral platform (e.g., Apache/Tomcat/Java) and all the other open development tools available.
Would that be a fashion statement as well? What are the particular advantages of .NET over J2EE that made it the superior choice? In my mind, I would look at a solution that didn't require paying Microsoft's excessive licensing fees, and that used as much open-source software as possible.
And what about security?
A poorly-configured Apache server can be as bad as a poorly configured IIS server, but isn't it apparent that .NET servers will be less secure than their open-source counterparts (given the security track record of Microsoft products --at least in the short term)?
MIT as a whole is pretty wealthy but individual IT budgets tend to be modest. With only $1 or $2 million to spend per system MIT needs to put its money into capabilities that are valued by end-users. A true J2EE system would involve EJB and container-managed persistence. All of that automatically generated SQL code from the application server can be 100X slower than hand-authored SQL, which means MIT would have to buy 100 times as much computing hardware to support the same application.
Not to mention that projects that built on top of J2EE are famously unproductive, expensive, behind-schedule, and inflexible. If you had a $20 million, 3-year budget to build something simple like photo.net, you could certainly do it with J2EE (or raw C with the Oracle C library for that matter). But MIT needs to produce things sort of like photo.net with a few undergrads hacking away over a summer. And they need to be able to tear down 20 percent of it and add another 50 percent the next summer as ideas evolve.
J2EE also looks like a classical IT dead end. There are dozens of different application servers and execution environments for Java. All have subtle differences so that an application built for, say, WebLogic won't run under Tomcat or Websphere. Maybe the application be ported by changing only 1 percent of the code but it isn't obvious which 1 percent. This is the same situation that Unix presented in the early 1990s. An AIX application wouldn't just work on SunOS or HP-UX. Close but not compatible. Faced with all of these choices, most application developers chose to develop only for Windows. In the amount of time it would take MIT to evaluate and select the right Java application server, V1.0 of a system could be built and launched using the Microsoft tools.
Anyway, it wasn't my decision. So if MIT wants something that can be maintained and extended by its students and graduates.
Microsoft .NET is a logical choice for that reason as well.
Please explain the life cycle of the ASP.net pages from start to end
One of the common question, I have been asked in almost interviews related with MS.Net is “Please explain the life cycle of the ASP.net pages; please explain me from start to end”. People
generally tell the interviewer the most common steps. So be preparing for complete
discussion on that now onwards.
Each request for a Microsoft® ASP.NET page that hits Microsoft® Internet Information Services (IIS) is handed over to the ASP.NET HTTP pipeline. The HTTP pipeline is a chain of managed objects that sequentially process the request and make the transition from a URL to plain HTML text happen. The entry point of the HTTP pipeline is the HttpRuntime class. The ASP.NET infrastructure creates one instance of this class per each AppDomain hosted within the worker process (remember that the worker process maintains one distinct AppDomain
per each ASP.NET application currently running).
The HttpRuntime class picks up an HttpApplication object from an internal pool and sets it to work on the request. The main task accomplished by the HTTP application manager is finding out the class that will
actually handle the request. When the request is for an .aspx resource, the handler is a page handler—namely, an instance of a class that inherits from Page. The association between types of resources and types of handlers is stored in the configuration file of the application. More exactly, the default set of mappings is defined in the <httpHandlers> section of the machine.config file. However, the application can customize the list of its own HTTP handlers in the local web.config file. The line below illustrates the code that defines the HTTP handler for .aspx resources.
<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory"/> An extension can be associated with a handler class, or more in general, with a handler factory class. In all cases, the HttpApplication object in charge for the request gets an object that implements the IHttpHandler interface. If the association resource/class is resolved in terms of a HTTP handler, then the returned class will implement the interface directly. If the resource is bound to a handler factory, an extra step is necessary. A handler
factory class implements the IHttpHandlerFactory interface whose GetHandler method will return
an IHttpHandler-based object.
How can the HTTP run time close the circle and process the page request? The IHttpHandler interface features the ProcessRequest method. By calling this method on the object that represents the requested page, the ASP.NET infrastructure starts the process that will generate the output for the browser.
The Real Page Class
The type of the HTTP handler for a particular page depends on the URL. The first time
the URL is invoked, a new class is composed and dynamically compiled to an assembly. The source code of the class is the outcome of a parsing process that examines the .aspx sources. The class is defined as part of the namespace ASP and is given a name that mimics the original URL. For example, if the URL endpoint is page.aspx, the name of the class is ASP.Page_aspx. The class name, though, can be programmatically controlled by setting the ClassName attribute in the @Page directive.
The base class for the HTTP handler is Page. This class defines the minimum set of methods and properties shared by all page handlers. The Page class implements the IHttpHandler interface.
Under a couple of circumstances, the base class for the actual handler is not Page but a different class. This happens, for example, if code-behind is used. Code-behind is a development technique that insulates the code necessary to a page into a separate C# or Microsoft Visual Basic® .NET class. The code of a page is the set of event handlers and helper methods that actually create the behavior of the page. This code can be defined inline using the <script runat=server> tag or placed in an external class—the code-behind class. A code-behind class is a class that inherits from Page and specializes it with extra methods. When specified, the code-behind class is used as the base class for the HTTP handler.
The other situation in which the HTTP handler is not based on Page is when the configuration file of the application contains a redefinition for the PageBaseType attribute in the <pages>
section.
<pages PageBaseType="Classes.MyPage, mypage" /> The PageBaseType attribute indicates the type and the assembly that contains the base class for page handlers. Derived from Page,
this class can automatically endow handlers with a custom and extended set of methods and properties.
The Page Lifecycle
Once the HTTP page handler class is fully identified, the ASP.NET run time calls the
handler's ProcessRequest method to process the
request. Normally, there is no need to change the implementation of the method
as it is provided by the Page class.
This implementation begins by calling the method FrameworkInitialize, which builds the controls tree for the page. The method is a protected and virtual member of the TemplateControl class—the class from which Page itself
derives. Any dynamically generated handler for an .aspx resource overrides FrameworkInitialize. In
this method, the whole control tree for the page is built.
Next, ProcessRequest makes the page transit various phases: initialization, loading of view state information and postback data, loading of the page's user code and execution of postback server-side events. After that,
the page enters in rendering mode: the updated view state is collected; the HTML code is generated and then sent to the output console. Finally, the page is unloaded and the request is considered completely served.
During the various phases, the page fires a few events that Web controls and user-defined code can intercept and handle. Some of these events are specific for embedded controls and subsequently can't be handled at the level of the .aspx code.
A page that wants to handle a certain event should explicitly register an appropriate handler. However, for backward compatibility with the earlier Visual Basic programming style, ASP.NET also supports a form of implicit event hooking. By default, the page tries to match special method names with events; if a match is found, the method is considered a handler for the event. ASP.NET provides special recognition of six method names. They are Page_Init,
Page_Load, Page_DataBind,
Page_PreRender, and Page_Unload.
These methods are treated as handlers for the corresponding events exposed by
the Page class. The HTTP run time will automatically bind these methods
to page events saving developers from having to write the necessary glue code.
For example, the method named Page_Load is
wired to the page's Load event as if the following code was written.
this.Load += new EventHandler(this.Page_Load); The automatic recognition of special names is a behavior under the control of the AutoEventWireup attribute of the @Page directive. If the attribute is set to false, any applications that wish to handle an event need to connect explicitly to the page event. Pages that don't use automatic event wire-up will get a slight performance boost by not having to do the extra work of matching names and events. You should note that all Microsoft Visual Studio® .NET projects are created with the AutoEventWireup attribute disabled. However, the default setting for the attribute is true, meaning that methods such as Page_Load are recognized and bound to the associated event.
The execution of a page consists of a sequence of stages listed in the following
table and is characterized by application-level events and/or protected, overridable methods.
Table 1. Key events in the life of an ASP.NET page
Stage | Page Event | Overridable method |
Page | Init | |
View state | LoadViewState | |
Postback data processing | LoadPostData method in any control that implements | |
Page loading | Load | |
Postback change notification | RaisePostDataChangedEvent method in any | |
Postback event handling | Any postback event defined by controls | RaisePostBackEvent method in any control that implements |
Page | PreRender | |
View state | SaveViewState | |
Page rendering | Render | |
Page unloading | Unload |
Some of the stages listed above are not visible at the page level and affect only
authors of server controls and developers who happen to create a class derived from Page. Init, Load, PreRender,
Unload, plus all postback events defined by embedded controls are the only signals of life that a page sends to the
external world.
Stages of Execution
The first stage in the page lifecycle is the initialization. This stage is characterized by the Init event, which fires to the application after the page's control tree has been successfully created. In other words, when the
Init event arrives, all the controls statically declared in the .aspx source file have been instantiated and hold their
default values. Controls can hook up the Init event to initialize any settings that will be needed during the lifetime of the incoming Web request. For example, at this time controls can load external template files or set up
the handler for the events. You should notice that no view state information is available for use yet Immediately
after initialization, the page framework loads the view state for the page. The view state is a collection of name/value pairs, where controls and the page itself store any information that must be persistent across Web requests. The
view state represents the call context of the page. Typically, it contains the state of the controls the last time the page was processed on the server. The view state is empty the first time the page is requested in the session. By
default, the view state is stored in a hidden field silently added to the page. The name of this field is __VIEWSTATE. By overriding the LoadViewState method—a protected overridable method on the Control class—component developers can control how the view state is restored and how its contents are mapped to the internal state.
Methods like LoadPageStateFromPersistenceMedium and its counterpart SavePageStateToPersistenceMedium can be used to load and save the view state to an alternative storage
medium—for example, Session, databases, or a server-side file. Unlike LoadViewState, the aforementioned methods are available only in classes derived from Page.
Once the view state has been restored, the controls in the page tree are in the same state they were the last time the page was rendered to the browser. The next step consists of updating their state to incorporate client-side changes. The postback data-processing stage gives controls a chance to update their state so that it accurately reflects the state of the corresponding HTML element on the client. For example, a server TextBox control has its HTML counterpart in an <input type=text> element. In the postback data stage, the TextBox control will retrieve the
current value of <input> tag and use it to refresh its internal state. Each control is responsible for extracting values from posted data and updating some of its properties. The TextBox control
will update its Text property whereas the CheckBox control will refresh its Checked property. The
match between a server control and a HTML element is found on the ID of both.
At the end of the postback data processing stage, all controls in the page reflect the previous state updated with changes entered on the client. At this point, the Load event is fired to the page.
There might be controls in the page that need to accomplish certain tasks if a sensitive property is modified across two different requests. For example, if the text of a textbox control is modified on the client, the control fires the TextChanged event. Each control can take the decision to fire an appropriate event if one or more of its properties are modified with the values coming from the client. Controls for which these changes are critical implement the IPostBackDataHandler interface, whose LoadPostData method is invoked immediately after the Load event. By coding the LoadPostData method, a control verifies if any critical change has occurred since last request and fires its own change event.
The key event in the lifecycle of a page is when it is called to execute the server-side code associated with an event triggered on the client. When the user clicks a button, the page posts back. The collection of posted values contains the ID of the button that started the whole operation. If the control is known to implement the IPostBackEventHandler interface (buttons and link buttons will do), the page framework calls the RaisePostBackEvent method. What this method does depends on the type of the control. With regard to buttons and link buttons, the method looks up for a Click event handler and runs the associated
delegate.
After handling the postback event, the page prepares for rendering. This stage is signaled by the PreRender
event. This is a good time for controls to perform any last minute update operations that need to take place immediately before the view state is saved and the output rendered. The next state is SaveViewState,
in which all controls and the page itself are invited to flush the contents of their own ViewState collection. The resultant view state is then serialized, hashed, Base64 encoded, and associated with the
__VIEWSTATE hidden field.
The rendering mechanism of individual controls can be altered by overriding the Render method. The method takes an HTML writer object and uses it to accumulate all HTML text to be generated for the control. The default implementation of the Render method for the Page class consists of a recursive call to all
constituent controls. For each control the page calls the Render method and caches the HTML output.
The final sign of life of a page is the Unload event that arrives just before the page object is dismissed. In this event you should release any critical resource you might have (for example, files, graphical objects,
database connections).
Finally, after this event the browser receives the HTTP response packet and displays the page.
To conclude, The ASP.NET page object model is particularly innovative because of the eventing mechanism. A Web page is composed of controls that both produce a rich HTML-based user interface and interact with the user through events. Setting up an eventing model in the context of Web applications is challenging. It's amazing to see that client-side generated events are resolved with server-side code, and the output of this is visible as the same
HTML page, only properly modified.
To make sense of this model it is important to understand the various stages in the
page lifecycle and how the page object is instantiated and used by the HTTP run
time.
