Friday, December 2, 2011

this._form is null or not an object, JavaScript Error

While I am developing an ASP.Net application, I am getting the below JavaScript error.

this._form is null or not an object

As the error clearly states that it is a JavaScript error, I started checking all of my JavaScript code.

At last I found the root cause to resolve the issue.

When you are trying to include the JavaScript files in the page header you have to include the closing </Script> tag.

Incorrect / Causing Error:
 <script src="~/scripts/abcd.js" type="text/javascript"/>

Correct:
 <script src="~/scripts/abcd.js" type="text/javascript"></script>

Friday, October 7, 2011

Assembly binding logging is turned off?

Very recently I faced an issue with a web application which is hosted on the Windows 2003 server. While browsing the web application from IIS the following message is giving.

Error:
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].


Reason:
The error is saying that there is a dll file missed which is useful for the Crystal Decisions.

Solution:
To resolve this issue I installed Report Viewer which is a redist package. The problem is solved. :)

Happy Coding...

Thursday, October 6, 2011

A new expression requires () or [] after type compilation error

I came across the following issue when I am trying to implement a project in Visual Studio 2005 C#. Some part of the project is already developed in Visual Studio 2008 C#. The same class files are copied to use it in Visual Studio 2005.

myList = new List<string> {"Data1", "Data2", "Data3"};

This gives me an error while compiling it in Visual Studio 2005 as "A new expression requires () or [] after type".

I had a long search on this and finally I got the following solution. 


Note those {} in error message, which are part of c# 3.0 syntax. This is not related to framework version, but to the version of the language. This is because of a different version of compiler was used.

It is compiled successfully, When I try to install the Microsoft following patch.
Happy Coding...

Wednesday, September 21, 2011

What are Design Patterns & Types.

What are Design Patterns:


             Design patterns are optimized, reusable solutions to the programming problems that we encounter every day. These defined originally by the Gang of Four programmers. Design patterns can be use in any programming language as these are not language specific. 
          A design pattern in architecture and computer science is a format way of documenting a solution to a design problem in a particular field of expertise.


Depending upon the nature of design these are divided into three different groups. 

  • Creational : These patterns provide instantiation mechanisms, making it easier to create objects in a way that suits the situation. 
    • Factory Method
    • Abstract Factory
    • Singleton
    • Builder
    • Prototype
  • Structural : These patterns generally deal with the relationship between entities, making it easier for these entities to work together. 
    • Adapter
    • Bridge
    • Composite
    • Decorator
    • Facade
    • Proxy
    • Flyweight
  • Behavioral : These patterns are used in communications between entities and make it easier and more flexible for these entities to communicate.
    • Chain of Responsibility
    • Command
    • Iterator
    • Interpreter
    • Mediator
    • Memento
    • Observer
    • State
    • Strategy
    • Visitor
    • Template Method
References: Design Pattern

Wednesday, March 2, 2011

ASP.Net Page Life Cycle events

A page in an ASP.NET application consists of several server controls. These are the fundamental building blocks of an ASP.NET application. The Life cycle of an ASP.NET page, depends on whether the page is requested for the first time or it is a postback. Postback is a process by which a page can request for itself.


When the Page is requested for the first time


The Life Cycle of a page when requested for the first time:

Initializing: During this phase, the server creates an instance of the server control

Loading: During this phase, the instance of the control is loaded onto the page object in which it is defined.

PreRendering: During this phase, the control is updated with the changes made to it. This prepares the control for rendering.

Saving: During this phase, the state information of the control is saved. For example, if a value is set for the control during the Load event, it is embedded in the HTML tag that will be returned to the browser.

Rendering: During this phase, the server creates the corresponding HTML tag for the control.

Disposing: During this phase, all cleanup tasks, such as closing files and database connections opened by the control are performed.

Unloading: During this phase, all cleanup tasks, such as destroying the instances of server control are performed. This is the final event in the life cycle of a server control

Life cycle when the page processed during a postback event

The processing sequence in which a page is processed during a postback event is:

Initializing: During this phase, the server creates an instance of the server control

Loading view state: During this phase, the view state of the control posted by the client is reloaded into the new instance of the control.

Loading: During this phase, the instance of the control is loaded onto the page object in which it is defined.

Loading the postback data: During this phase, the server searches any data corresponding to the control that is loaded in the data posted by the client.

PreRendering: During this phase, the control is updated with the changes made to it. This prepares the control for rendering.

Saving state: During this phase, the change in the state of control between the current request and the previous request of the page is saved. For each change, the corresponding event is raised. For example, if the text of a textbox is changed, the new text is saved and a text_change event is raised.

Rendering: During this phase, the server creates the corresponding HTML tag for the control.

Disposing: During this phase, all cleanup tasks, such as closing files and database connections opened by the control are performed.

Unloading: During this phase, all cleanup tasks, such as destroying the instances of server control are performed. This is the final event in the life cycle of a server control

The events associated with the relevant page cycle phases are:
  • Page Initialization: Page_Init
  • View State Loading:LoadViewState
  • Postback data processing: LoadPostData
  • Page Loading: Page_Load
  • PostBack Change Notification: RaisePostDataChangedEvent
  • PostBack Event Handling: RaisePostBackEvent
  • Page Pre Rendering Phase: Page_PreRender
  • View State Saving: SaveViewState
  • Page Rendering: Page_Render
  • Page Unloading: Page_UnLoad





The following are the main ten steps in the ASP.NET page life cycle.


1. Object Initialization


A page's controls (and the page itself) are first initialized in their raw form. By declaring your objects within the constructor of your C# code-behind file, the page knows what types of objects and how many to create. Once you have declared your objects within your constructor, you may then access them from any sub class, method, event, or property. However, if any of your objects are controls specified within your ASPX file, at this point the controls have no attributes or properties. It is dangerous to access them through code, as there is no guarantee of what order the control instances will be created (if they are created at all). The initialization event can be overridden using the OnInitmethod. 

2. Load Viewstate Data


After the Init event, controls can be referenced using their IDs only (no DOM is established yet for relative references). At LoadViewState event, the initialized controls receive their first properties: viewstate information that was persisted back to the server on the last submission. The page viewstate is managed by ASP.NET and is used to persist information over a page roundtrip to the server. Viewstate information is saved as a string of name/value pairs and contains information such as control text or value. The viewstate is held in the value property of a hidden <input> control that is passed from page request to page request. As you can see, this is a giant leap forward from the old ASP 3.0 techniques of maintaining state. This event can be overridden using theLoadViewState method and is commonly used to customize the data received by the control at the time it is populated.  

3. LoadPostData Processes Postback Data

During this phase of the page creation, form data that was posted to the server (termed postback data in ASP.NET) is processed against each control that requires it. When a page submits a form, the framework will implement theIPostBackDataHandler interface on each control that submitted data. The page then fires the LoadPostData event and parses through the page to find each control that implements this interface and updates the control state with the correct postback data. ASP.NET updates the correct control by matching the control's unique ID with the name/value pair in the NameValueCollection. This is one reason that ASP.NET requires unique IDs for each control on any given page. Extra steps are taken by the framework to ensure each ID is unique in situations, such as several custom user controls existing on a single page. After the LoadPostData event triggers, theRaisePostDataChanged event is free to execute (see below). 


4. Object Load 

Objects take true form during the Load event. All object are first arranged in the page DOM (called the Control Tree in ASP.NET) and can be referenced easily through code or relative position (crawling the DOM). Objects are then free to retrieve the client-side properties set in the HTML, such as width, value, or visibility. During Load, coded logic, such as arithmetic, setting control properties programmatically, and using the StringBuilder to assemble a string for output, is also executed. This stage is where the majority of work happens. TheLoad event can be overridden by calling OnLoad. 


5. Raise PostBack Change Events


As stated earlier, this occurs after all controls that implement theIPostBackDataHandler interface have been updated with the correct postback data. During this operation, each control is flagged with a Boolean on whether its data was actually changed or remains the same since the previous submit. ASP.NET then sweeps through the page looking for flags indicating that any object's data has been updated and fires RaisePostDataChanged. TheRaisePostDataChanged event does not fire until all controls are updated and after the Load event has occurred. This ensures data in another control is not manually altered during the RaisePostDataChanged event before it is updated with postback data. 


6. Process Client-Side PostBack Event

After the server-side events fire on data that was changed due to postback updates, the object which caused the postback is handled at theRaisePostBackEvent event. The offending object is usually a control that posted the page back to the server due to a state change (with autopostbackenabled) or a form submit button that was clicked. There is often code that will execute in this event, as this is an ideal location to handle event-driven logic. The RaisePostBackEvent event fires last in the series of postback events due to the accuracy of the data that is rendered to the browser. Controls that are changed during postback should not be updated after the executing function is called due to the consistency factor. That is, data that is changed by an anticipated event should always be reflected in the resulting page. TheRaisePostBackEvent can be trapped by catching RaisePostBackEvent. 


7. Prerender the Objects 

The point at which the objects are prerendered is the last time changes to the objects can be saved or persisted to viewstate. This makes the PreRender step a good place to make final modifications, such as changing properties of controls or changing Control Tree structure, without having to worry about ASP.NET making changes to objects based off of database calls or viewstate updates. After the PreRender phase those changes to objects are locked in and can no longer be saved to the page viewstate. The PreRender step can be overridden using OnPreRender.


8. ViewState Saved


The viewstate is saved after all changes to the page objects have occurred. Object state data is persisted in the hidden <input> object and this is also where object state data is prepared to be rendered to HTML. At the SaveViewStateevent, values can be saved to the ViewState object, but changes to page controls are not. You can override this step by using SaveViewState. 

9. Render To HTML


The Render event commences the building of the page by assembling the HTML for output to the browser. During the Render event, the page calls on the objects to render them into HTML. The page then collects the HTML for delivery. When the Render event is overridden, the developer can write custom HTML to the browser that nullifies all the HTML the page has created thus far. The Render method takes an HtmlTextWriter object as a parameter and uses that to output HTML to be streamed to the browser. Changes can still be made at this point, but they are reflected to the client only.  

10. Disposal

After the page's HTML is rendered, the objects are disposed of. During theDispose event, you should destroy any objects or references you have created in building the page. At this point, all processing has occurred and it is safe to dispose of any remaining objects, including the Page object. You can override Dispose, as well as Render by setting the appropriate selection in the object parameter.

Interview Questions

1. Explain the AutoPostBack feature in ASP.NET?

AutoPostBack allows a control to automatically postback when an event is fired. For eg: If we have a Button control and want the event to be posted to the server for processing, we can set AutoPostBack = True on the button.

2. What are the different code models available in ASP.NET 2.0?
There are 2 code models available in ASP.NET 2.0. One is the single-file page and the other one is the code behind page.
3. Which base class does the web form inherit from?
Page class in the System.Web.UI namespace.

4. Explain the ViewState in ASP.NET?
Http is a stateless protocol. Hence the state of controls is not saved between postbacks. Viewstate is the means of storing the state of server side controls between postbacks. The information is stored in HTML hidden fields. In other words, it is a snapshot of the contents of a page.
You can disable viewstate by a control by setting the EnableViewState property to false.
5. What does the EnableViewState property signify?
EnableViewState saves the state of an object in a page between postbacks. Objects are saved in a Base64 encoded string. If you do not need to store the page, turn it off as it adds to the page size.

6. Explain the Event Life cycle of ASP.NET 2.0?
The events occur in the following sequence. Its best to turn on tracing(<% @Page Trace=”true”%>) and track the flow of events :
PreInit – This event represents the entry point of the page life cycle. If you need to change the Master page or theme programmatically, then this would be the event to do so. Dynamic controls are created in this event.
Init – Each control in the control collection is initialized.
Init Complete* - Page is initialized and the process is completed.
PreLoad* - This event is called before the loading of the page is completed.
Load – This event is raised for the Page and then all child controls. The controls properties and view state can be accessed at this stage. This event indicates that the controls have been fully loaded.
LoadComplete* - This event signals indicates that the page has been loaded in the memory. It also marks the beginning of the rendering stage.
PreRender – If you need to make any final updates to the contents of the controls or the page, then use this event. It first fires for the page and then for all the controls.
PreRenderComplete* - Is called to explicitly state that the PreRender phase is completed.
SaveStateComplete* - In this event, the current state of the control is completely saved to the ViewState.
Unload – This event is typically used for closing files and database connections. At times, it is also used for logging some wrap-up tasks.
The events marked with * have been introduced in ASP.NET 2.0.

Monday, February 28, 2011

Breakpoints are not working in visual studio?

If are you getting the warning "The break point will not currently be hit. No symbols have been loaded for this document." If breakpoints stopped working in the project on vs IDE the breakpoints became hollow circle with a warning message inside both VS 2008 and Vs 2005, below are few points that comes handy when you break points stop working on Visual Studio.







--> First of all check whether you are running on debug or release mode if you are running on release mode change it to debug, if it is in debug try next step.


--> Go to Debug-->Windows-->Modules check whether all the project dll's have symbol status if it says cannot find or open pdb file right click on module select load symbols and browse to the path of your application PDB,if it says says symbol Loaded then try next point.

--> Exit your IDE and delete bin and obj folder go to visual studio command line and run "devenv /resetsettings" NOTE: running this command will reset all your visual studio settings to default so think before running it and use at your own risk.

--> If you are using web development then Clear temporary output folders the path looks something like below

C:\WINDOWS\Microsoft.NET\Framework\{framework version}\Temporary ASP.NET Files\{some name}\{some numbers}\{some numbers} 

Friday, February 25, 2011

Introduction to Visual Studio IDE with features

Visual Studio .NET IDE

Visual Studio .NET IDE (Integrated Development Environment) is the Development Environment for all .NET based applications which comes with rich features. VS .NET IDE provides many options and is packed with many features that simplify application development by handling the complexities. Visual Studio .NET IDE is an enhancement to all previous IDE’s by Microsoft.

Important Features

One IDE for all .NET Projects

Visual Studio .NET IDE provides a single environment for developing all types of .NET applications. Application’s range from single windows applications to complex n-tier applications, rich web applications, Web Services, Windows Services, Mobile applications, Console Applications etc.,


Option to choose from Multiple Programming Languages

You can choose the programming language of your choice to develop applications based on your expertise in that language. The major programming languages supported by the Visual Studio are C#, VB.Net, F#, J#, C++ etc., You can also incorporate multiple programming languages in one .NET solution and edit that with the IDE.


Option to choose from Multiple Frameworks

You can choose a specific framework to develop the project for your specific target platform. Also you can have multiple projects with multiple frameworks under single solution.


IDE is Customizable

You can customize the IDE based on your preferences. The Options under Tools menu settings allow you to do this. With these settings you can set the IDE screen the way you want, the way the keyboard behaves and you can also filter the help files based on the language of your choice. You can select fonts, colors of your coding window.
You can also change the Text Editor properties for various languages. If you want to store your settings you can Import and Export your settings.


Built-in Browser

The IDE comes with a built-in browser that helps you browse the Internet without launching another application. You can look for additional resources, online help files, source codes and much more with this built-in browser feature.
When we open VS .NET from Start->Programs->Microsoft Visual Studio .NET->Microsoft Visual Studio .NET the window that is displayed first is the Start Page which is shown below. The start Page allows us to select from the most recent projects (last four projects) with which we worked or it can be customized based on your preferences.




The Integrated Development Environment (IDE) shown in the image below is what we actually work with. This IDE is shared by all programming languages in Visual Studio. You can view the toolbars towards the left side of the image along with the Solution Explorer window towards the right.





New Project Dialogue Box

The New Project dialogue box like the one in the image below is used to create a new project specifying it's type allowing us to name the project and also specify it's location on the disk where it is saved. The default location on the hard disk where all the projects are saved is C:\DocumentsandSettings\Administrator\MyDocuments\VisualStudioProjects.




Following are different templates under Project Types and their use:

Windows Application: This template allows you to create standard windows based applications.
Class Library: Class libraries are those that provide functionality similar to Active X and DLL by creating classes that access other applications.
Windows Control Library: This allows you to create our own windows controls. Also called as User Controls, where you group some controls, add it to the toolbox and make it available to other projects.
ASP .NET Web Application: This allows you to create web-based applications using IIS. We can create web pages, rich web applications and web services.
ASP .NET Web Service: Allows you to create XML Web Services.
Web Control Library: Allows you to create User-defined controls for the Web. Similar to user defined windows controls but these are used for Web.
Console Application: A new kind of application in Visual Studio .NET. They are command line based applications.
Windows Service: These run continuously regardless of the user interaction. They are designed for special purpose and once written, will keep running and come to an end only when the system is shut down.
Other: This template is to develop other kinds of applications like enterprise applications, database applications, Setup and Deployment etc.



Solution Explorer Window

Solution Explorer provides you with an organized view of your projects and their files as well as ready access to the commands that pertain to them. A toolbar associated with this window offers commonly used commands for the item you highlight in the list. To access Solution Explorer, select Solution Explorer on the View menu. An image of the Solution Explorer window is shown below.





Server Explorer Window

The Server Explorer window is a great tool that provides "drag and drop" feature and helps us work with databases in an easy graphical environment. For example, if we drag and drop a database table onto a form, VB .NET automatically creates connection and command objects that are needed to access that table. The image below displays Server Explorer window.





Class View

Class View displays the symbols defined, referenced, or called in the application you are developing. You can open Class View from the View menu. There are two panes: an upper Objects pane and a lower Members pane. The Objects pane contains an expandable tree of symbols whose top-level nodes represent projects. To expand a node selected in the tree, click its plus (+) sign or press the plus (+) key on the keypad.





Intellisense

Intellisense is what that is responsible for the boxes that open as we type the code. IntelliSense provides a list of options that make language references easily accessible and helps us to find the information we need. They also complete the typing for us. The image below displays that.
IntelliSense provides an array of options that make language references easily accessible. When coding, you do not need to leave the Code Editor or the Immediate Mode command window to perform searches on language elements. You can keep your context, find the information you need, insert language elements directly into your code, and even have IntelliSense complete your typing for you.

IntelliSense comprises the following options:
  • List Members
  • Parameter Info
  • Quick Info
  • Complete Word
  • Automatic Brace Matching



Properties Window

The properties window allows us to set properties for various objects at design time. For example, if you want to change the font, font size, backcolor, name, text that appears on a button, textbox etc, you can do that in this window. Below is the image of properties window. You can view the properties window by selecting
View->Properties Window from the main menu or by pressing F4 on the keyboard.





Code Definition Window

The Code Definition window is a read-only editor view that displays the definition of a symbol in a code file stored in, or referenced by, the active project. To display this window, select Code Definition Window from the View menu. When the window first opens, it displays the definition for the last symbol selected. If no definition is available, No definition selected is displayed. This window can be docked along any convenient edge of the integrated development environment (IDE). By default, it is displayed beneath the Code Editor.





Command Window

The Command window is used to execute commands or aliases directly in the Visual Studio integrated development environment (IDE). You can execute both menu commands and commands that do not appear on any menu. To display the Command window, choose Other Windows from the View menu, and select Command Window.

You can view the command window by selecting 
View->Other Windows->Command Window from the main menu. The command window in the image displays all possible commands with File.




Output Window
This window can display status messages for various features in the integrated development environment (IDE). It displays the results of building and running applications. To display the Output window, select Output from the View menu. To close the Output window and shift focus back to the Editor, press the Escape (ESC) key.




Object Browser

The Object Browser allows you to select and examine the symbols available for use in projects. You can open the Object Browser from the View menu, or by clicking the Object Browser button on the main toolbar.
There are three panes: an Objects pane on the left, a Members pane on the upper right, and a Description pane on the lower right. If you resize the Object Browser into a single column, the Objects pane moves to the top, the Members pane to the middle, and the Description pane to the bottom.
In the Objects pane, icons identify hierarchical structures such as .NET Framework and COM components, namespaces, type libraries, interfaces, enums, and classes. You can expand these structures to reveal ordered lists of their members. Properties, methods, events, variables, constants, and other contained items are listed in the Members pane. Details on the item selected in the Objects or Members pane appear in the Description pane.
You can view the object explorer window by selecting View->Other Windows-> Object Browser from the main menu.





Error List Window

The Error List helps you speed application development. In the Error List window, you can:
  • Display the Errors, Warnings, and Messages produced as you edit and compile code.
  • Find syntax errors noted by IntelliSense.
  • Find deployment errors, certain Static Analysis errors, and errors detected while applying Enterprise Template policies.
  • Double-click any error message entry to open the file where the problem occurs, and move to the error location.
  • Filter which entries are displayed, and which columns of information appear for each entry.
To display the Error List, on the View menu choose Error List.
Use the Errors, Warnings, and Messages buttons to select which entries to display.





Call Stack Window

By using the Call Stack window, you can view the function or procedure calls that are currently on the stack.
The Call Stack window displays the name of each function and the programming language it is written in. The function or procedure name may be accompanied by optional information, such as module name, line number, byte offset, and parameter names, types, and values. The display of this optional information can be turned on or off.




Watch Window
You can use the Watch window to evaluate variables and expressions and keep the results. You can also use the Watch window to edit the value of a variable or register.
To open the Watch window, the debugger must be running or in break mode. From the Debug menu, choose Windows, then Watch, and click on Watch1, Watch2, Watch3, or Watch4.





Task List Window

The Task List helps you create and manage a list of programming tasks. In the Task List window, you can:
  • Enter notes on work to be done as User Tasks. For more information.
  • Display Task Comments that link to lines in your code files where work must be done. For more information
  • Display custom Categories of task messages.
To display the Task List, on the View menu, click Task List. Use the Categories drop-down list to select which entries to display.