Exception Handling

The error-handling model in ASP.NET lets you handle errors easily at the method, page, and application levels of your web applications. Most applications will use some combination of these to handle problems when they arise.

Method level: In method you can handle the exception using try…Catch…Finally block.

private void anyRoutine( )
{
	try
	{
		// Routine code in this block
	}
        catch (Exception exc)
	{
	        // error handling in this block
	}
	finally
	{
		// cleanup performed in this block
	}
} // anyRoutine

Page level: Why would you want to use this approach? It allows you to handle all page-level errors in a uniform way, which can simplify error-handling code and make it more consistent and robust. The trick is in keeping all the error-handling code in one place in the original page by leveraging the Page_Error method

private void Page_Error(Object sender,
			System.EventArgs e)
{
	Exception lastError = null;
	// get the last error that occurred
	lastError = Server.GetLastError( );

	// do any logging, notifications, etc. here

	// set the URL of the page that will display the error and
	// include querystring parameters to allow the page to display
	// what happened
	Page.ErrorPage = "ErrorPage.aspx" +
		 "?PageHeader=Error Occurred" +
		 "&Message1=" + lastError.Message +
		 "&Message2=" +
		 "This error was processed at the page level";
 } // Page_Error

Application level: By handling all exceptions at the application level, rather than at the method or page level, you can process all errors for the application in a single location. Doing all error handling in one place in an application is key to writing effective code.

protected void Application_Error(Object sender, EventArgs e)
{
	const String EVENT_LOG_NAME = "Application";

	Exception lastException = null;
	System.Diagnostics.EventLog log = null;
	String message = null;

	// get the last error that occurred
	lastException = Server.GetLastError( );

	// create the error message from the message in the last exception along
	// with a complete dump of all of the inner exceptions (all exception
	// data in the linked list of exceptions)
	message = lastException.Message +
			  "\r\r" +
			  lastException.ToString( );

	// Insert error information into the event log Or Log the Exception in text file
	// perform other notifications, etc. here
	

	// clear the error and redirect to the page used to display the
	// error information
	Server.ClearError( );
	Server.Transfer("ErrorPage.aspx" +
			"?PageHeader=Error Occurred" +
			"&Message1=" + lastException.Message +
			"&Message2=" +
			"This error was processed at the application level");
} // Application_Error

#exception-handling

Selecting the Right Tabular Control

Problem
You want to use an ASP.NET control to display some data in a tabular format.

Solution
Use a Repeater, DataList, DataGrid, or GridView control. Always choose the smallest and fastest control that meets your needs, which invariably will be influenced by other criteria as in these examples:

If you need a quick and easy solution
Use a GridView.

If you need a lightweight read-only tabular display
Use a Repeater.

If you need your solution to be small and fast
Use a Repeater (lightest) or DataList (lighter).

If you want to use a template to customize the appearance of the display
Choose a Repeater or DataList.

If you want to select rows or edit the contents of a data table
Choose a DataList, a DataGrid, or a GridView.

If you want built-in support to sort your data by column or paginate its display
Choose a GridView.

If you want to use custom pagination
Choose a DataGrid.

Comparative summary of native tabular control features

Feature

Repeater control

DataList control

DataGrid control

GridView control

Default appearance

None (template-driven)

Table

Table

Table

Automatically generates columns from the data source

No

No

Yes

Yes

Header can be customized

Yes

Yes

Yes

Yes

Data row can be customized

Yes

Yes

Yes

Yes

Supports alternating row customization

Yes

Yes

Yes

Yes

Supports customizable row separator

Yes

Yes

No

Yes

Footer can be customized

Yes

Yes

Yes

Yes

Supports pagination

No

No

Yes

Yes

Supports custom paging

No

No

Yes

No

Supports sorting

No

No

Yes

Yes

Supports editing contents

No

Yes

Yes

Yes

Supports selecting a single row

No

Yes

Yes

Yes

Supports selecting multiple rows

No

No

No

No

Supports arranging data items horizontally or vertically (from left-to-right or top-to-bottom)

No

Yes

No

No

Supports sorting and paging using asynchronous callbacks

No

No

No

Yes

#tabular-controls