06 asp.net session08

26
Slide 1 of 26 Ver. 1.0 Developing Web Applications Using ASP.NET In this session, you will learn to: Configure ViewState properties and ControlState properties for Web server controls Store and retrieve Application and Session state data Implement out-of-process session state Store and manage state data in the Cache object Explain how to store and retrieve database connections by using the Web.Config file Explain how to use data source controls to access relational data Explain how to use data source controls to access XML data Explain how to use data source controls to access object data Objectives

Transcript of 06 asp.net session08

Page 1: 06 asp.net session08

Slide 1 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

In this session, you will learn to:Configure ViewState properties and ControlState properties for Web server controls

Store and retrieve Application and Session state data

Implement out-of-process session state

Store and manage state data in the Cache object

Explain how to store and retrieve database connections by using the Web.Config file

Explain how to use data source controls to access relational data

Explain how to use data source controls to access XML data

Explain how to use data source controls to access object data

Objectives

Page 2: 06 asp.net session08

Slide 2 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

Problem Statement:You are a developer in the Adventure Works organization, a fictitious bicycle manufacturer. You have been asked to assist in the development of the Business-to-Consumer (B2C) Web application and a related Business-to-Employee (B2E) extranet portal.

Decisions on the design of the application have already been made. As part of the first phase of the B2C development, you have been asked to configure the behavior of Web server controls by manipulating their ViewState properties. You have also been asked to add a site-counter to the site, which will display user numbers and store data in the Application and Session objects.

Demo: Managing State for a Web Application

Page 3: 06 asp.net session08

Slide 3 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

You will also specify how the Web application stores and manages session state data. Finally, you have been asked to implement caching by using the Cache object to store bike review data.

Demo: Managing State for a Web Application (Contd.)

Page 4: 06 asp.net session08

Slide 4 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

Solution:To solve this problem, you need to perform the following tasks:

1. Configuring ViewState Properties for Web Server Controlsa. Open the Adventure Works Web site.

b. Set the EnableViewState property of Web server controls.

c. Test the ViewState after a Web page is submitted.

d. Disable ViewState for the BikeReview.aspx page.

e. Work with the MaxPageStateFieldLength property to divide the ViewState data into chunks.

2. Storing and Retrieving Application and Session Statea. Initialize information in the Application object.

b. Write code to increment the UserCount value when a new user session begins.

c. Store Data in the Session object.

d. Retrieve Application and Session state data and display it on the Web page.

Demo: Managing State for a Web Application (Contd.)

Page 5: 06 asp.net session08

Slide 5 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

3. Implementing Out-of-Process Session Statea. Enable users to save their favorite trails in Session data.

b. Configure IIS, and then test the application.

c. Enable StateServer session management.

d. Enable SQLServer session management.

4. Storing and Managing State Data in the Cache Objecta. Store data and retrieve data from the Cache object.

b. Define dependencies between items in the Cache object.

c. Delete invalid cached data.

d. Implement Change Notifications in cached data to ensure that deleted items are reloaded into the cache.

Demo: Managing State for a Web Application (Contd.)

Page 6: 06 asp.net session08

Slide 6 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

Database Connections and Connection Strings:Web applications use database connections as the underlying mechanism to perform various operations on the databases.

Database connections are defined in terms of a string that specifies the properties of the connection.

Different databases support different properties and therefore require different connection strings.

Database Connections and the Web.Config File

Page 7: 06 asp.net session08

Slide 7 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

Using the Web.config File to Simplify Connection String Management:

Connection strings can be hard coded into the Web application pages.

This approach is difficult to manage because databases may be moved, redefined, or upgraded.

This problem can be solved by using the Web.Config file to store the connection strings.

This approach retrieves the details of connection strings at run time from the Web.config file.

Database Connections and the Web.Config File (Contd.)

Page 8: 06 asp.net session08

Slide 8 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

This sample code shows part of a Web.Config file with a connection string:<connectionStrings>

<add name="AdvWorks"

connectionString="Server=MySQLSever;

Database=AdventureWorks;

Integrated Security=SSPI;Persist Security

Info=True"

providerName="System.Data.SqlClient"/>

</connectionStrings>

Database Connections and the Web.Config File (Contd.)

Page 9: 06 asp.net session08

Slide 9 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

Retrieving Connection Strings at Run Time:Connection string stored in the connectionStrings section of the Web.Config file, can be retrieved at run time.

ConnectionStrings collection of ConfigurationManager class is used to retrieve such connection string.

This sample code shows how to retrieve connection strings at run time and how to use them to open a database connection:string myDataString =ConfigurationManager.ConnectionStrings["AdvWorks"].ConnectionString;

System.Data.SqlClient.SqlConnection sqlConn =new System.Data.SqlClient.SqlConnection (myDataString);

sqlConn.Open();

Database Connections and the Web.Config File (Contd.)

Page 10: 06 asp.net session08

Slide 10 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

Data Source Controls:A data source control pulls together all the elements of connecting to a database to retrieve or manipulate data.

These elements includes:Provider

Connection string

Query

Example: SqlDataSource control <asp:SqlDataSource ID="SqlDataSource1"

Runat="server"

SelectCommand="SELECT * from Customers"

ConnectionString="<%$ ConnectionStrings:AppConnString%>"

ProviderName="<%$ ConnectionStrings:AppConnString.ProviderName %>"/>

Database Connections and the Web.Config File (Contd.)

Page 11: 06 asp.net session08

Slide 11 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

Accessing Relational Databases by Using Data Source Controls:

Data source controls are provided for accessing the data stored in relational databases.

Relational data source controls provide standard database functionality for any type of database used. This functionality includes:

Retrieval of data

Insertion of new data

Updating of existing data

Deletion of data

Relational Data and Data Source Controls

Page 12: 06 asp.net session08

Slide 12 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

The basic process of displaying data from relational databases on a Web page is:1. Add a data source control to the Web page, and then

configure it to connect to the required database.

2. Specify the SELECT statement in the SelectCommand property of the data source control, to retrieve the data.

3. Bind data controls or data-aware controls to the data source control.

If application has complex data access requirements, it may not be feasible to use a data source control.

In such a case, data access can be coded by the programmer by using ADO.NET classes.

Relational Data and Data Source Controls (Contd.)

Page 13: 06 asp.net session08

Slide 13 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

Providers:A provider is a class that can communicate with a specific type of database or data store.

ASP.NET provides a number of providers that can communicate with a wide variety of data stores.

The providers include, the .NET framework data provider for:SQL server in the System.Data.SqlClient namespace

OLE DB in the System.Data.OleDb namespace

ODBC in the System.Data.Odbc namespace

Oracle in the System.Data.OracleClient namespace

Relational Data and Data Source Controls (Contd.)

Page 14: 06 asp.net session08

Slide 14 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

Format of returned Data:SqlDataSource control can return data in two forms:

DataSet

Data reader

Any of the above forms can be selected by setting DataSourceMode property of the data source control.

DataSet: Allows users to manipulate data after retrieving it.

Is used when a user want to filter, sort, or page through data after retrieving it.

Data reader:Provides a read-only cursor that can fetch individual records.

Is used to return and load the data into a control on the page.

Relational Data and Data Source Controls (Contd.)

Page 15: 06 asp.net session08

Slide 15 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

Caching:The SqlDataSource control can cache data that it has retrieved.

Caching can be enabled by setting EnableCaching to true.

CacheDuration property can be set to specify the number of seconds to cache data.

Cache dependency feature of SQL Server can also be used by SqlDataSource control.

By using cache dependency, retrieval of data can be minimized.

Relational Data and Data Source Controls (Contd.)

Page 16: 06 asp.net session08

Slide 16 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

Sorting and Filtering:The SqlDataSource control can be used to filter the data without rerunning the query, provided you have:

Cached the data

Specified DataSet as the format of the returned data

FilterExpression property of SqlDataSource control can be used to specify the selection criteria that are applied to the data maintained by the data source control.

Filter expression can be parameterized by creating special FilterParameters objects.

Relational Data and Data Source Controls (Contd.)

Page 17: 06 asp.net session08

Slide 17 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

Data source controls provide the following properties:SelectCommand

UpdateCommand

InsertCommand

DeleteCommand

To enable data editing in a Web page, templates with editable controls can be defined for the data controls.

Button controls with specific CommandName arguments can be included to invoke specific data operations.

Relational Data and Data Source Controls (Contd.)

Page 18: 06 asp.net session08

Slide 18 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

Templates for data controls:To use the FormView control to display data from a database on a Web page:

Create an ItemTemplate element between the opening and closing tags of the FormView control.

Include static label controls and the necessary markup to bind the static label controls to the fields in the data source as:

<ItemTemplate>

<b>Product ID: </b>

<asp:Label ID="lblProductID" runat="server" Text= '<%# Eval("ProductID") %>'>

</asp:Label><br/>

<b>Product Name:</b>

<asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName") %>'>

</asp:Label><br/> <asp:Button ID="btnAdd" Text="Add review“ CommandName="New“ runat="server" />

</ItemTemplate>

Relational Data and Data Source Controls (Contd.)

Page 19: 06 asp.net session08

Slide 19 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

To use the FormView control to add new data :Create an InsertItemTemplate element between the opening and closing tags of the FormView control.

Include editable controls and the necessary markup to bind the controls to the fields in the data source as:

<InsertItemTemplate>

<table width="100%">

<tr> <td> Product: </td>

<td> <% =Request.QueryString["ProductID"]%></td>

</tr>

<tr> <td> Product Name: </td> <td> <asp:TextBox

ID="txtProductName" runat="server" Text='<%# Bind("ProductName") %>' />

</td> </tr>

Relational Data and Data Source Controls (Contd.)

Page 20: 06 asp.net session08

Slide 20 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

<tr>

<td>

<asp:Button ID="btnInsert" runat="server" CommandName="Insert" Text="Save"/>

</td>

<td>

<asp:Button ID="btnCancel" runat="server" CommandName="Cancel" Text="Cancel"/>

</td>

</tr>

</table>

</InsertItemTemplate>

Relational Data and Data Source Controls (Contd.)

Page 21: 06 asp.net session08

Slide 21 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

XMLDataSource control is provided for accessing XML data.

This control can be configured to retrieve data from:XML files

Web services that return XML data

String variables that contain XML data

In-memory XMLDataDocument objects

XML data retrieved by the XMLDataSource control is read only.

XML Data and Data Source Controls

Page 22: 06 asp.net session08

Slide 22 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

The basic process for displaying XML data on Web pages is:1. Add an XmlDataSource control to the Web page, and

configure it to connect to the required XML source.

2. Bind data-aware controls that are capable of displaying XML data to the XmlDataSource control.

TreeView control and Xml control are example of controls that are capable of displaying XML data.

XML Data and Data Source Controls (Contd.)

Page 23: 06 asp.net session08

Slide 23 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

ObjectDataSource controls are provided for accessing the data managed by business objects.

The functionality of the business object defines the functionality of the ObjectDataSource control.

The basic process for displaying data from business objects on the Web pages is:1. Add an ObjectDataSource control to the Web page, and

then configure it to connect to the required business object.

2. Specify the name of the method used to retrieve data from the business object in the SelectMethod property.

3. Bind data controls or data-aware controls to the data source control.

Object Data and Data Source Controls

Page 24: 06 asp.net session08

Slide 24 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

ObjectDataSource controls support the following properties:

SelectMethod

UpdateMethod

InsertMethod

DeleteMethod

Object Data and Data Source Controls (Contd.)

Page 25: 06 asp.net session08

Slide 25 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

In this session, you learned that:Database connections are defined in terms of a string that specifies the properties of the connection.

Using Web.config file is a better approach than hard-coding the connection string in a Web application.

Web page retrieves the details of the connection string at the run time from the Web.Config file.

ConfigurationManager class is used to retrieve the connection string at run time.

Data source controls are provided to access the data stored in a relational database.

Data source controls can select, update, insert, and delete data from a relational database.

Summary

Page 26: 06 asp.net session08

Slide 26 of 26Ver. 1.0

Developing Web Applications Using ASP.NET

XMLDataSource control is provided for accessing XML data.

XMLDataSource control provides read-only access to the XML data.

ObjectDataSource control provides access to the data managed by business objects.

ObjectDataSource control can display, update, insert, and delete object data, provided the underlying object supports these functionalities.

Summary (Contd.)