asp.net session 11

Upload: prerana-tokas

Post on 14-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 ASP.net Session 11

    1/19

    Slide 1 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    In this session, you will learn to:

    Access data from disparate data sources by using LINQ

    Handle and log errors

    Objectives

  • 7/30/2019 ASP.net Session 11

    2/19

    Slide 2 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    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

    Accessing Data by Using the LINQ Queries

  • 7/30/2019 ASP.net Session 11

    3/19

    Slide 3 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    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.NETDynamic Data to determine the user interface that is based

    on the LINQ to SQL data model.

    Accessing Data by Using the LINQ Queries (Contd.)

  • 7/30/2019 ASP.net Session 11

    4/19

    Slide 4 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    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.

    Accessing Data by Using the LINQ Queries (Contd.)

    Let us see how to create an ASP.NET dynamic data website

  • 7/30/2019 ASP.net Session 11

    5/19

    Slide 5 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    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 byusing LINQ, you need to write the following code snippet:IEnumerable city = from cus in

    CustomersDetails.Descendants("City")

    select (string) cus.Attribute("City");

    Accessing Data by Using the LINQ Queries (Contd.)

  • 7/30/2019 ASP.net Session 11

    6/19

    Slide 6 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    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 viewthe latest albums. The details of the latest albums can only be

    added by the administrator.

    Activity 7.1: Using LINQ to Retrieve and Store Data

  • 7/30/2019 ASP.net Session 11

    7/19Slide 7 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    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.

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

  • 7/30/2019 ASP.net Session 11

    8/19Slide 8 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    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 in-

    memory 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 LinqDataSourcecontrol by using the DataSourceID property of the data-

    bound control.

    Accessing Data by Using the LINQ Data Source Control

  • 7/30/2019 ASP.net Session 11

    9/19Slide 9 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    The following table lists some properties of the

    LinqDataSource control.

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

    Property Descr ipt ion

    ContextTypeName Gets or sets the name of the type that contains the

    property whose value has the data that is to be

    retrieved.TableName Gets or sets the name of the property or field in the

    data context object that represents a data collection.DeleteParameters Gets the collection of parameters that are used

    during the delete operation.Delete Performs the delete operation.

    InsertParameters Gets the collection of parameters that are used

    during the insert operation.

    Insert Performs the insert operation.SelectParameters Gets the collection of parameters that are used

    during the data retrieval operation.Select Gets or sets the properties and the calculated values

    included in the retrieved data.UpdateParameters Gets the collection of parameters that are used

    during the update operation.Update Performs the insert operation.

  • 7/30/2019 ASP.net Session 11

    10/19Slide 10 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    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 typemismatch cannot be detected at compile time.

    Such type of errors can be dealt with by implementing

    effective error handling and debugging techniques.

    Handling and Logging Errors

  • 7/30/2019 ASP.net Session 11

    11/19Slide 11 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    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.

    Handling and Logging Errors (Contd.)

  • 7/30/2019 ASP.net Session 11

    12/19Slide 12 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

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

    Page-level

    Application-level

    Handling Errors

  • 7/30/2019 ASP.net Session 11

    13/19Slide 13 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    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 byusing 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.

    Handling Errors (Contd.)

  • 7/30/2019 ASP.net Session 11

    14/19Slide 14 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    The exceptions that occur in a Web application are inheritedfrom 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 Exceptionclass 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.

    Handling Errors (Contd.)

  • 7/30/2019 ASP.net Session 11

    15/19Slide 15 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    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 todisplay 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( "

    " );

    Response.Write( Server.GetLastError().Message);

    Server.ClearError();

    }

    Handling Errors (Contd.)

  • 7/30/2019 ASP.net Session 11

    16/19Slide 16 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    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

    element in the web.config file or by using the

    Application_Error subroutine in the global.asax file.

    You can use the 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 displaycustomized error messages.

    Redirect users to an appropriate error page in case an

    unhandled error occurs.

    Handling Errors (Contd.)

  • 7/30/2019 ASP.net Session 11

    17/19Slide 17 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    The two attributes of the 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.

    Handling Errors (Contd.)

  • 7/30/2019 ASP.net Session 11

    18/19Slide 18 of 19Ver. 1.0

    Developing Web Applications Using ASP.NET

    Application-level errors can also be handled in the eventhandler 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.

    Handling Errors (Contd.)

  • 7/30/2019 ASP.net Session 11

    19/19Slide 19 of 19V 1 0

    Developing Web Applications Using ASP.NET

    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 thetry-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

    element in the web.config file or by usingthe Application_Error subroutine in the Global.asax file.

    Summary