Session 11
Session 11
NET
Objectives
Ver. 1.0
Slide 1 of 19
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
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
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
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
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
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
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
TableName
DeleteParameters Delete InsertParameters Insert SelectParameters Select UpdateParameters Update
Ver. 1.0
Slide 9 of 19
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
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
Ver. 1.0
Slide 12 of 19
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
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
Slide 15 of 19
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
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
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
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