0% found this document useful (0 votes)
186 views

Session 11

This document discusses developing web applications using ASP.NET. It covers accessing data using LINQ, including LINQ to SQL and LINQ to XML. It also discusses handling and logging errors at both the page and application level, such as using try-catch blocks, the Page_Error subroutine, and the <customErrors> element in web.config.

Uploaded by

Prerana Tokas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views

Session 11

This document discusses developing web applications using ASP.NET. It covers accessing data using LINQ, including LINQ to SQL and LINQ to XML. It also discusses handling and logging errors at both the page and application level, such as using try-catch blocks, the Page_Error subroutine, and the <customErrors> element in web.config.

Uploaded by

Prerana Tokas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 19

Developing Web Applications Using ASP.

NET
Objectives

In this session, you will learn to:


Access data from disparate data sources by using LINQ Handle and log errors

Ver. 1.0

Slide 1 of 19

Developing Web Applications Using ASP.NET


Accessing Data by Using the LINQ Queries

To access data from a database, you can use LINQ to ADO.NET. LINQ to ADO.NET includes the following technologies to access data:
LINQ to dataset LINQ to SQL

Ver. 1.0

Slide 2 of 19

Developing Web Applications Using ASP.NET


Accessing Data by Using the LINQ Queries (Contd.)

Dataset is a collection of cached data presented in a tabular form. The data from a Dataset can be retrieved by using LINQ. LINQ to Dataset is basically used in situations where you have used ADO.NET for accessing data from a database. LINQ to SQL provides a run-time infrastructure for managing relational data as objects. In LINQ to SQL, the data model of a relational database is mapped to an object model created by the developer. LINQ to SQL entity classes are also used by the ASP.NET Dynamic Data to determine the user interface that is based on the LINQ to SQL data model.

Ver. 1.0

Slide 3 of 19

Developing Web Applications Using ASP.NET


Accessing Data by Using the LINQ Queries (Contd.)

ASP.NET Dynamic Data is a framework that enables a user to create data-driven ASP.NET Web applications with minimal or no coding. The data-driven ASP.NET Web applications created by using ASP.NET Dynamic Data provides:
Data access operations such as create, update, remove, and display, relational operations, and data validation. Built-in support for foreign-key relationships. The ability to customize the user interface rendered to display and edit specific data fields. The ability to customize data field validation. Let us see how to create an ASP.NET dynamic data website

Ver. 1.0

Slide 4 of 19

Developing Web Applications Using ASP.NET


Accessing Data by Using the LINQ Queries (Contd.)

Extensible Markup Language (XML) is widely used to exchange data between various applications. LINQ to XML is a programming interface that enables you to access data stored in XML files. To access the data from the CustomersDetails.Xml by using LINQ, you need to write the following code snippet:
IEnumerable<string> city = from cus in CustomersDetails.Descendants("City") select (string) cus.Attribute("City");

Ver. 1.0

Slide 5 of 19

Developing Web Applications Using ASP.NET


Activity 7.1: Using LINQ to Retrieve and Store Data

Problem Statement:
The MusicMania store caters to the demand for latest music of their customers. As a developer, you need to add a Web page, AddAlbumDetails.aspx, to the MusicMania website to allow the administrator to add the details of the latest albums that are available in the store. This would enable the customers to view the latest albums. The details of the latest albums can only be added by the administrator.

Ver. 1.0

Slide 6 of 19

Developing Web Applications Using ASP.NET


Activity 7.1: Using LINQ to Retrieve and Store Data (Contd.)

Solution:
To allow the administrator to add the details of the latest albums, you need to perform the following tasks:
1. Add a new Web page. 2. Design the Web page. 3. Execute the application.

Ver. 1.0

Slide 7 of 19

Developing Web Applications Using ASP.NET


Accessing Data by Using the LINQ Data Source Control

The LinqDataSource control:


Enables you to connect a data control to a wide variety of data sources, such as a database, data-source classes, and inmemory collections. Can be connected to any kind of data collection that is stored in a public field or property.

You can bind data-bound controls to a LinqDataSource control by using the DataSourceID property of the databound control.

Ver. 1.0

Slide 8 of 19

Developing Web Applications Using ASP.NET


Accessing Data by Using the LINQ Data Source Control (Contd.)

The following table lists some properties of the LinqDataSource control.


Property ContextTypeName Description Gets or sets the name of the type that contains the property whose value has the data that is to be retrieved. Gets or sets the name of the property or field in the data context object that represents a data collection. Gets the collection of parameters that are used during the delete operation. Performs the delete operation. Gets the collection of parameters that are used during the insert operation. Performs the insert operation. Gets the collection of parameters that are used during the data retrieval operation. Gets or sets the properties and the calculated values included in the retrieved data. Gets the collection of parameters that are used during the update operation. Performs the insert operation.

TableName
DeleteParameters Delete InsertParameters Insert SelectParameters Select UpdateParameters Update

Ver. 1.0

Slide 9 of 19

Developing Web Applications Using ASP.NET


Handling and Logging Errors

A Web application may contain various types of errors or bugs. Errors such as typing errors and syntax errors are detected at compile time. However, runtime errors such as division by zero and type mismatch cannot be detected at compile time. Such type of errors can be dealt with by implementing effective error handling and debugging techniques.

Ver. 1.0

Slide 10 of 19

Developing Web Applications Using ASP.NET


Handling and Logging Errors (Contd.)

For implementing effective error handling techniques, you should be able to classify errors and identify situations where errors can occur in your Web applications. When an error occurs, details about the error can be added to an event log. The logging of errors helps in debugging because the users can see the event log to know about the errors that occurred in the application.

Ver. 1.0

Slide 11 of 19

Developing Web Applications Using ASP.NET


Handling Errors

ASP.NET allows you to handle errors at the following levels:


Page-level Application-level

Ver. 1.0

Slide 12 of 19

Developing Web Applications Using ASP.NET


Handling Errors (Contd.)

Page-level error handling:


Used to handle errors that occur on ASP.NET pages. Can be implemented either by using the try-catch block or by using the Page_Error subroutine.

A try-catch block:
Is an error handling feature provided by most of the languages. Enables you to programmatically handle the errors that occur at runtime. Can be used to retrieve error information and then handle the error.

Ver. 1.0

Slide 13 of 19

Developing Web Applications Using ASP.NET


Handling Errors (Contd.)

The exceptions that occur in a Web application are inherited from the Exception class. The following properties included in the Exception class help you to handle errors easily and efficiently. You can use the following properties of the Exception class to handle errors:
Message: It returns a string that represents the error message. Source: It returns a string that represents the object or application that caused the error. StackTrace: It returns a string that represents the methods called immediately before the error occurred. TargetSite: It returns a MethodBase object, which represents the method that caused the error.

Ver. 1.0

Slide 14 of 19

Developing Web Applications Using ASP.NET


Handling Errors (Contd.) You can use the Page_Error subroutine to handle the unexpected errors that occur while executing an application. This subroutine executes in case an unhandled exception occurs on a page. For example, you can use the Page_Error subroutine to display an error message, as shown in the following code snippet:
void Page_Error (Object sender , EventArgs e) { Response.Write( "Sorry, an error was encountered:" ); Response.Write( "<p>" ); Response.Write( Server.GetLastError().Message); Server.ClearError(); }
Ver. 1.0

Slide 15 of 19

Developing Web Applications Using ASP.NET


Handling Errors (Contd.)

Application-level error handling:


Enables you to handle errors on ASP.NET pages, irrespective of where the errors occur in the application. Can be implemented either by using the <customErrors> element in the web.config file or by using the Application_Error subroutine in the global.asax file.

You can use the <customErrors> element of the web.config file to:
Control the details of the error information made available to the users of ASP.NET applications. Completely hide the error information from the users or display customized error messages. Redirect users to an appropriate error page in case an unhandled error occurs.

Ver. 1.0

Slide 16 of 19

Developing Web Applications Using ASP.NET


Handling Errors (Contd.) The two attributes of the <customErrors> element are:
mode: It specifies the mode for handling custom errors. The mode attribute can be set to any of the following values:
Off On RemoteOnly

defaultRedirect: It specifies the URL of the customized error page that should be displayed in case of an error.

Ver. 1.0

Slide 17 of 19

Developing Web Applications Using ASP.NET


Handling Errors (Contd.)

Application-level errors can also be handled in the event handler for the Application.Error event that occurs whenever an unhandled error occurs in an application. The event handler for this event is written in the Global.asax file. You can use this event handler to capture the errors for logging or notification.

Ver. 1.0

Slide 18 of 19

Developing Web Applications Using ASP.NET


Summary

In this session, you learned that:


The errors can be handled at the following two levels:
Page-level Application-level

The page-level error can be handled either by using the try-catch block or by using the Page_Error subroutine.

Application-level error handling enables you to handle errors on ASP.NET pages, irrespective of where they occur in the application. The application-level error can be handled either by using the <customErrors> element in the web.config file or by using the Application_Error subroutine in the Global.asax file.

Ver. 1.0

Slide 19 of 19

You might also like