Performance Improvement of a Web Page With Data Grids (ASP.NET)

Introduction

Often we come across code written in older versions of .NET (for example, 1.0 and so on) where the performance of the application is very slow. Most of the developers are in need of improving the performance of the existing code even without migrating to the latest versions.

This article has a few tips to handle such scenarios.

Places to look For:

When you want to work on performance improvement of a web page, the key areas to look for are:

  1. Code
  2. Database

Code

In aspx pages or ascx pages you can look for the following improvements.

  1. Server roundtrips

    Look for unnecessary round trips to the server, rather replace the same with client code (for example, JavaScript or jQuery).

  2. DataControls

    By default view state is enabled for all data controls. Disable the viewstate by setting EnableViewState = false;

  3. Check for paging

    If paging is not implemented in your data control, handle that first.

  4. Sorting and Filtering

    Move all the Filters and search criteria to DB. Sorting and filtering can be faster in DB rather than in memory.

  5. Minimize the use of View State

  6. Refine the search for data

    Rather than blindly getting all the data from the database to the grid, you should have at least one filter criteria, so that data retrieval is faster.

  7. Stored Procedures vs Table data

    Use Stored Procedure calls rather than direct table hits. This will improve performance in a great way.

  8. Caching

    Avoid getting too many records from the database and holding them in the cache. Over a period of time, the cache will be too huge and performance will degrade. Instead, cache small chunks of data.

  9. Exception handling

    Avoid having too much exception handling. Exceptions degrade the performance of the application. When you already know that an exception will occur, why not avoid it, rather than making a try..catch block to handle it.
    Don't use:
    1. Try  
    2.    {  
    3.       int div = a/b;  
    4.    }  

    5. Catch(Exception e)  
    6.    {
    7.   
    8.    }  
    Instead use:
    1. If (b>0)  
    2.    {  
    3.       int div = a/b;  
    4.    }  
Database
  1. Pagination

    Have paging incorporated in the Stored Procedures, For example In a SQL Stored Procedure, you can:
    1. CREATE PROCEDURE [dbo].[GetPolicies]   
    2. ( @PolicyID INT = NULL,  
    3.   @UserID INT = NULL,   
    4.   @PageNumber INT = 1,  
    5.   @NoOfRecords INT = 50
    6. )   
    7. BEGIN  
    8. …………    
    9. END   
  2. Indexing

    Indexing is the key to performance. Indexing the tables on a proper key when there is huge data to be fetched, will largely improve the performance.

  3. Limit data fetch

    Avoid sending a huge amount of data over the network.


Similar Articles