Category Archives: Asp.Net

Asp.Net

Http Handler vs Http Module in Asp.Net

Http Handler

  • An http handler returns a response to a request that is identified by a file name extension. i.e; .aspx (page handler) .
  • It uses IHttpHandler Interface.
  • It is used for rss feeds, image server etc.

Http Modules

  • An http module is invoked for all types of requests and responses.
  • It uses IHttpModule Interface.
  • it is used for security, logging and custom headers/footers etc.

State Management in Asp.net.

State Management – State Management techniques are used to persist data between post-backs. State Management techniques can be divided in two categories :

  1. Client State Management
    • View state
    • Control state
    • Hidden fields
    • Cookies
    • Query strings
  2. Server State Management
    • Application state
    • Session state
    • Profile properties
    • Database support

View state

  • Use when you need to store small amounts of information for a page that will post back to itself.
  • Syntax:   ViewState[“Name”]=”I am a Software developer”
    To get value from viewstate need to convert data
    string Name = Viewstate[“Name”].tostring();
  • Viewstate is stored in hidden field as string in encrypted form.
  • Viewstate can be enabled/disabled at page level.

Control state

  • Use when you need to store small amounts of state information for a control between round trips to the server.
  • Control state can not be modified
  • It can not be disabled
  • It can not be directly accessed

Hidden fields

  • Use a hidden field only on pages that are submitted to the server.
  • It can store only single value as string.
  • Hidden fields are standard HTML controls that are easy to use.

Cookies

  • Use when you need to store small amounts of information on the client.
    These are two types :
    Persistent – They store in browser’s sub folder and remain till the manually deleted or automatically deleted based on time defined.
    Non-Persistent – They lost when user close browser.

Query strings

  • Use query strings only if you are requesting the same page, or another page via a link.
  • They are not secure as they are visible in browser and hackers can tempered them.
  • Get value from querystring :
    Request.QueryString[“Text”]

Application state

  • Use when you are storing infrequently changed, global information that is used by many users.
  • It is maintained in below events :
    Application_Start – Code that runs on application startup
    Application_End – Code that runs on application shutdown
    Application_Error – Code that runs when an unhandled error occurs
    Session_Start – Code that runs when a new session is started
    Session_End – Code that runs when a session ends.

Session state

  • Use when you are storing short-lived information that is specific to an individual session
  • There are four modes for session state :
    In Proc mode – It is default mode and is used to store data on the web server.
    State Server mode – It is used to store data in the Asp.Net State service.  Session state does not lost when web application restarted and can be shared between multiple web servers in the web farming.
    SQL Server mode – It is used to store data in the SQL Server.  Session state does not lost when web application restarted and can be shared between multiple web servers in the web farming.
    Custom mode – It is used to store data in custome location.

Profile properties
Use when you are storing user-specific information that needs to be persisted after the user session is expired and needs to be retrieved again on subsequent visits to your application.

Database support
Use when you are storing large amounts of information, managing transactions, or the information must survive application and session restarts.

When to use interface and abstract class in C#.

  • Abstract classes have some implemented functionality so you can use the same functionality among all your implementations. Its the best example of re-usability of code. While Interfaces don’t provide any implementation of code.
  • If you are thinking about multiple version of your component then create abstract class. Abstract classes provide a simple and easy way to version your component. By updating base class, all the inheriting classes are automatically updated. While Interfaces can not be changed once created. Interfaces needs to be recreated.
  • Abstract classes are useful for large scale functionality While interface are useful in Small scale of functionality.

What is difference between overloading and overriding in C#?

Difference between overloading and overriding.

Overloading :

  • It means methods are declared with same name but different parameters (signatures).
  • It is also called compile time and early binding.
  • Example :-

    public class Overloading
      {
        public int CalculateAmt(int a, int b)  //Method with two int type Parameters.
        {
            return a + b;
        }
    
        public int CalculateAmt(int a, int b, int c)  //Same method name with different parameters.
        {
            return a * b * c;
        }
      }
    

Overriding :

  • It means methods are declared with same name and same parameters (signatures).
  • it is also called run time and late binding.
  • Method overriding is only possible in derived class.
  • Method overriding is not possible in the same class.
  • Only virtual and abstract methods in the base class can be overrides in derived class.
  • Example :

    //Base Class
    public class Math
      {
        public virtual int CalculateAmt(int a, int b)
        {
            return a + b;
        }
      }
    
    //Derived Class
    public class Calculator:Math
    {
        public override int CalculateAmt(int a, int b)
        {
           return a * b;
        }
    }
    

What is Data Concurrency and Types of Concurrency Control in ADO.NET.

When multiple users attempt to modify data at the same time, controls need to be established in order to prevent one user’s modifications from adversely affecting modifications from simultaneous users. The system of handling what happens in this situation is called concurrency control.

Types of Concurrency Control
Three common ways to manage concurrency in a database:

  • Pessimistic concurrency control: A row is unavailable to users from the time the record is fetched until it is updated in the database.
  • Optimistic concurrency control: A row is unavailable to other users only while the data is actually being updated. The update examines the row in the database and determines whether any changes have been made. Attempting to update a record that has already been changed results in a concurrency violation.
  • “Last in wins”: A row is unavailable to other users only while the data is actually being updated. However, no effort is made to compare updates against the original record; the record is simply written out, potentially overwriting any changes made by other users since you last refreshed the records.

What are the validation controls available in ASP.NET?

ASP.NET validation controls are:

  • RequiredFieldValidator: This validates controls if controls contain data.
  • CompareValidator: This allows checking if data of one control match with other control.
  • RangeValidator: This verifies if entered data is between two values.
  • RegularExpressionValidator: This checks if entered data matches a specific format.
  • CustomValidator: Validate the data entered using a client-side script or a server-side code.
  • ValidationSummary: This allows developer to display errors in one place.

Explain how a web application works.

A web application resides in the server and serves the clients requests over internet. The client access the web page using browser from his machine. When a client makes a request, it receives the result in the form of HTML which are interpreted and displayed by the browser.

A web application on the server side runs under the management of Microsoft Internet Information Services (IIS). IIS passes the request received from client to the application. The application returns the requested result in the form of HTML to IIS, which in turn, sends the result to the client.