Wednesday, September 19, 2012

What are the advantage of Asp.net

ASP.NET provides the following advantages:
  • Enables you to access information from data sources, such as back-end databases and text files that are stored on a Web server.
  • Provides enriched tool support in the form of Visual Studio .NET integrated development environment (VS .NET IDE).
  • Enables you to develop your application in any .NET language.
  • Enables you to build user interfaces that separate application logic from presentation content.
  • Enables you to manage Web applications by storing the configuration information in an XML file.
  • Helps improve developer productivity and provides facilities for improving the performance, reliability, and scalability of Web applications.

Tuesday, September 18, 2012

What is the main difference between an ASP.NET Web application and a traditional Windows application

ASP.NET Web applications runs under common language runtime using managed code where as Unmanaged Windows application runs under Windows using unmanaged code.

List the four major differences between Web and Windows applications.

  • Web forms cannot use the standard Windows controls. Instead, they use server controls, HTML controls, user controls, or custom controls created specially for Web forms.
  • Web applications are displayed in a browser. Windows applications display their own windows and have more control over how those windows are displayed.
  • Web forms are instantiated on the server, sent to the browser, and destroyed immediately. Windows forms are instantiated, exist for as long as needed, and are destroyed.
  • Web applications run on a server and are displayed remotely on clients. Windows applications run on the same machine they are displayed on.

Explain the life cycle of an ASP .NET page

ASP.NET 2.0 Page Life Cycle - The lifetime of an ASP.NET page is filled with events. A .NET technical interview might begin with this question. A series of processing steps takes place during this page life cycle. Following tasks are performed:
  • Initialization
  • Instantiation of controls
  • Restoration & Maintainance of State
  • Running Event Handlers
  • Rendering of data to the browser
The life cycle may be broken down into Stages and Events. The stages reflect the broad spectrum of tasks performed. The following stages take place
  1. Page Request - This is the first stage, before the page life cycle starts. Whenever a page is requested, ASP.NET detects whether the page is to be requested, parsed and compiled or whether the page can be cached from the system.
  2. Start - In this stage, properties such as Request and Response are set. Its also determined at this stage whether the request is a new request or old, and thus it sets the IsPostBack property in the Start stage of the page life cycle.
  3. Page Initialization - Each control of the page is assigned a unique identification ID. If there are themes, they are applied. Note that during the Page Initialization stage, neither postback data is loaded, nor any viewstate data is retrieved.
  4. Load - If current request is a postback, then control values are retrieved from their viewstate.
  5. Validation - The validate method of the validation controls is invoked. This sets the IsValid property of the validation control.
  6. PostBack Event Handling - Event handlers are invoked, in case the request is a postback.
  7. Rendering - Viewstate for the page is saved. Then render method for each control is called. A textwriter writes the output of the rendering stage to the output stream of the page's Response property.
  8. Unload - This is the last stage in the page life cycle stages. It is invoked when the page is completely rendered. Page properties like Respone and Request are unloaded.

what is application state in asp.net

ASP.NET allows you to save values using application state, a global storage mechanism that is accessible from all pages in the Web application. Application state is stored in the Application key/value dictionary. Once you add your application-specific information to application state, the server manages it, and it is never exposed to the client. Application state is a great place to store information that is not user-specific. By storing it in the application state, all pages can access data from a single location in memory, rather than keeping separate copies of the data. Data stored in the Application object is not permanent and is lost any time the application is restarted.
ASP.NET provides three events that enable you to initialize Application variables (free resources when the application shuts down) and respond to Application errors:

a.Application_Start: Raised when the application starts. This is the perfect place to initialize Application variables.

b.Application_End: Raised when an application shuts down. Use this to free application resources and perform logging.

c.Application_Error: Raised when an unhandled error occurs. Use this to perform error logging.

Example :

// This sets the value of the Application Variable



Application["Name"] = txtName.Text; 

Response.Redirect("WebForm5.aspx"); 



// This is how we retrieve the value of the Application Variable



if( Application["Name"] != null ) 

    Label3.Text = Application["Name"].ToString();

What is session state in asp.net

A session is defined as the period of time that a unique user interacts with a Web application.
Session state is a collection of objects, tied to a session are stored on a server.
Session State information is available to all pages opened by a user during a single visit

ASP.NET allows you to save values using session state, a storage mechanism that is accessible from all pages requested by a single Web browser session. Therefore, you can use session state to store user-specific information. Session state is similar to application state, except that it is scoped to the current browser session. If different users are using your application, each user session has a different session state. In addition, if a user leaves your application and then returns later after the session timeout period, session state information is lost and a new session is created for the user. Session state is stored in the Session key/value dictionary.

You can use session state to accomplish the following tasks:
i.Uniquely identify browser or client-device requests and map them to individual session instances on the server. This allows you to track which pages a user saw on your site during a specific visit.

ii.Store session-specific data on the server for use across multiple browser or client-device requests during the same session. This is perfect for storing shopping cart information.

iii.Raise appropriate session management events. In addition, you can write application code leveraging these events.

ASP.NET session state supports several different storage options for session data:

a.InProc Stores session state in memory on the Web server. This is the default, and it offers much better performance than using the ASP.NET state service or storing state information in a database server. InProc is fine for simple applications, but robust applications that use multiple Web servers or must persist session data between application restarts should use State Server or SQLServer.

b.StateServer Stores session state in a service called the ASP.NET State Service. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm. ASP.NET State Service is included with any computer set up to run ASP.NET Web applications; however, the service is set up to start manually by default. Therefore, when configuring the ASP.NET State Service, you must set the startup type to Automatic.

c.SQLServer Stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm. On the same hardware, the ASP.NET State Service outperforms SQLServer. However, a SQL Server database offers more robust data integrity and reporting capabilities.

d.Custom Enables you to specify a custom storage provider. You also need to implement the custom storage provider.

e.Off Disables session state. You should disable session state if you are not using it to improve performance.

Example:Sessions are created as soon as the first response is being sent from the client to the server, and session ends when the user closes his browser window or some abnormal operation takes place. Here is how you can use session variables fortransferring values. Below you can see a Session is created for the user and "Name" is the key, also known as the Session key, which is assigned the TextBox value.


// Session Created



Session["Name"] = txtName.Text; 

Response.Redirect("WebForm5.aspx");



// The code below shows how to get the session value.



// This code must be placed in other page.



if(Session["Name"] != null) 

    Label3.Text = Session["Name"].ToString();

What is view state

ViewState: Its nothing but it's a hidden data which is kept by asp.net pages in "_VIEWSTATE" as hidden form field. They track the changes to a web site during post backs.
The ViewState of a page can be seen if you view the source code of the running page(Right Click --> View Source). It will be listed under the following tags:
< input id="__VIEWSTATE" name="__VIEWSTATE" type="hidden" value=""  >
Having a large ViewState will cause a page to download slowly from the users side.

source view

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE” value="/wEPDwUKMTIxNDIyOTM0Mg9kFgICAw9kFgICAQ8PFgIeBFRleHQFEzQvNS8yMDA2IDE6Mzc6MTEgUE1kZGROWHn/rt75XF/pMGnqjqHlH66cdw==" />

Encrypting of the View State: You can enable view state encryption to make it more difficult for attackers and malicious users to directly read view state information. Though this adds processing overhead to the Web server, it supports in storing confidential information in view state. To configure view state encryption for an application does the following:

<Configuration>
   <system.web>
                  <pages viewStateEncryptionMode="Always"/>
  </system.web>
</configuration>

Alternatively, you can enable view state encryption for a specific page by setting the value in the page directive, as the following sample demonstrates:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ViewStateEncryptionMode="Always"%>

View State is enabled by default, but if you can disable it by setting the EnableViewState property for each web control to false. This reduces the server processing time and decreases page size. 

Error cannot access a closed file – FileUpload in ASP.NET


This was very interesting error, i got during development of file upload control in ASP.NET.
On my local system, every thing was just fine. but when i deployed my application on development server, my control was able to upload only small size files.
whenever i tried to upload large size file i got error “cannot access a closed file”
After going through MSDN i found that there is one new element added in ASP.NET 2.0 known as httpRuntime.
So i just added this new Tag in my web.config and every thing was right.


<system.web>
<httpRuntime  maxRequestLength="15360" requestLengthDiskThreshold="15360"/>
</system.web>

Note :

requestLengthDiskThreshold should not exceed the maxRequestLengthattribute.

To read more about this, please visit http://msdn.microsoft.com/en-us/library/e1f13641(VS.80).aspx