Sei sulla pagina 1di 26

Developing Web Applications Using ASP.

NET
Objectives

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

Ver. 1.0

Slide 1 of 26

Developing Web Applications Using ASP.NET


Demo: Managing State for a Web Application

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.

Ver. 1.0

Slide 2 of 26

Developing Web Applications Using ASP.NET


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

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.

Ver. 1.0

Slide 3 of 26

Developing Web Applications Using ASP.NET


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

Solution:
To solve this problem, you need to perform the following tasks:
1. Configuring ViewState Properties for Web Server Controls
a.
b.
c.
d.
e.

Open the Adventure Works Web site.


Set the EnableViewState property of Web server controls.
Test the ViewState after a Web page is submitted.
Disable ViewState for the BikeReview.aspx page.
Work with the MaxPageStateFieldLength property to divide the
ViewState data into chunks.

2. Storing and Retrieving Application and Session State


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

Ver. 1.0

Slide 4 of 26

Developing Web Applications Using ASP.NET


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

3. Implementing Out-of-Process Session State


a.
b.
c.
d.

Enable users to save their favorite trails in Session data.


Configure IIS, and then test the application.
Enable StateServer session management.
Enable SQLServer session management.

4. Storing and Managing State Data in the Cache Object


a.
b.
c.
d.

Ver. 1.0

Store data and retrieve data from the Cache object.


Define dependencies between items in the Cache object.
Delete invalid cached data.
Implement Change Notifications in cached data to ensure that deleted
items are reloaded into the cache.

Slide 5 of 26

Developing Web Applications Using ASP.NET


Database Connections and the Web.Config File

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.

Ver. 1.0

Slide 6 of 26

Developing Web Applications Using ASP.NET


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

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.

Ver. 1.0

Slide 7 of 26

Developing Web Applications Using ASP.NET


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

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>

Ver. 1.0

Slide 8 of 26

Developing Web Applications Using ASP.NET


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

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();

Ver. 1.0

Slide 9 of 26

Developing Web Applications Using ASP.NET


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

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 %>"/>

Ver. 1.0

Slide 10 of 26

Developing Web Applications Using ASP.NET


Relational Data and Data Source Controls

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

Ver. 1.0

Slide 11 of 26

Developing Web Applications Using ASP.NET


Relational Data and Data Source Controls (Contd.)

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.

Ver. 1.0

Slide 12 of 26

Developing Web Applications Using ASP.NET


Relational Data and Data Source Controls (Contd.)

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

Ver. 1.0

Slide 13 of 26

Developing Web Applications Using ASP.NET


Relational Data and Data Source Controls (Contd.)

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.

Ver. 1.0

Slide 14 of 26

Developing Web Applications Using ASP.NET


Relational Data and Data Source Controls (Contd.)

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.

Ver. 1.0

Slide 15 of 26

Developing Web Applications Using ASP.NET


Relational Data and Data Source Controls (Contd.)

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.

Ver. 1.0

Slide 16 of 26

Developing Web Applications Using ASP.NET


Relational Data and Data Source Controls (Contd.)

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.

Ver. 1.0

Slide 17 of 26

Developing Web Applications Using ASP.NET


Relational Data and Data Source Controls (Contd.)

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>
Ver. 1.0

Slide 18 of 26

Developing Web Applications Using ASP.NET


Relational Data and Data Source Controls (Contd.)

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>

Ver. 1.0

Slide 19 of 26

Developing Web Applications Using ASP.NET


Relational Data and Data Source Controls (Contd.)
<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>

Ver. 1.0

Slide 20 of 26

Developing Web Applications Using ASP.NET


XML Data and Data Source Controls
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.

Ver. 1.0

Slide 21 of 26

Developing Web Applications Using ASP.NET


XML Data and Data Source Controls (Contd.)

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.

Ver. 1.0

Slide 22 of 26

Developing Web Applications Using ASP.NET


Object Data and Data Source Controls
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.

Ver. 1.0

Slide 23 of 26

Developing Web Applications Using ASP.NET


Object Data and Data Source Controls (Contd.)
ObjectDataSource controls support the following
properties:
SelectMethod
UpdateMethod
InsertMethod
DeleteMethod

Ver. 1.0

Slide 24 of 26

Developing Web Applications Using ASP.NET


Summary

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.

Ver. 1.0

Slide 25 of 26

Developing Web Applications Using ASP.NET


Summary (Contd.)
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.

Ver. 1.0

Slide 26 of 26

Potrebbero piacerti anche