You are on page 1of 41

ER/CORP/CRS/LA1010/003 Ver. No.: 4.0 Copyright 2008, Infosys Technologies Ltd.

Education and Research


We enable you to leverage knowledge anytime, anywhere!
ASP.NET Web Applications and Web Services
(Long Cycle)
Day 2
Copyright 2008, Infosys Technologies Ltd.
General Guideline
(2008) Infosys Technologies Ltd.

This document contains valuable confidential and proprietary information of Infosys. Such
confidential and proprietary information includes, amongst others, proprietary intellectual
property which can be legally protected and commercialized. Such information is furnished
herein for training purposes only. Except with the express prior written permission of Infosys,
this document and the information contained herein may not be published, disclosed, or used
for any other purpose.
2
Copyright 2008, Infosys Technologies Ltd.
Confidential Information
This Document is confidential to Infosys Technologies Limited. This document
contains information and data that Infosys considers confidential and proprietary
(Confidential Information).
Confidential Information includes, but is not limited to, the following:
Corporate and Infrastructure information about Infosys
Infosys project management and quality processes
Project experiences provided included as illustrative case studies
Any disclosure of Confidential Information to, or use of it by a third party, will be
damaging to Infosys.
Ownership of all Infosys Confidential Information, no matter in what media it resides,
remains with Infosys.
Confidential information in this document shall not be disclosed, duplicated or used
in whole or in part for any purpose other than reading without specific written
permission of an authorized representative of Infosys.
This document also contains third party confidential and proprietary information.
Such third party information has been included by Infosys after receiving due written
permissions and authorizations from the party/ies. Such third party confidential and
proprietary information shall not be disclosed, duplicated or used in whole or in part
for any purpose other than reading without specific written permission of an
authorized representative of Infosys.
3
Copyright 2008, Infosys Technologies Ltd. 4
Recap
Web Applications
IIS
ASP.NET Overview
ASP.NET Programming Model
Server Controls
Validation Controls
User Controls


Copyright 2008, Infosys Technologies Ltd. 5
Session Plan - Day2

Error Handling
Data Binding
Data Controls
ASP.NET Objects
Tracing ( Self Study)

Copyright 2008, Infosys Technologies Ltd. 6
Configuration Files
Configuration files are XML files which are used to configure the web
sites.These files usually contain the settings which need to be done for all the
pages of web sites.

Web.config
Web application specific
Used to perform settings at application level

Machine.config
Specific to all the applications on a machine
Contains the configuration which is common to all the web sites


ER/CORP/CRS/LA1010/003 Ver. No.: 4.0 Copyright 2008, Infosys Technologies Ltd.
Education and Research
We enable you to leverage knowledge anytime, anywhere!
Error Handling in ASP.NET
Copyright 2008, Infosys Technologies Ltd. 8
Error Handling
.NET CLR provides structured Exception handling
Using try catch block
ASP.NET provides declarative error handling
Automatically redirect users to error page when unhandled exceptions occur
Prevents ugly error messages from being sent to users

<customErrors mode=On defaultRedirect=error.htm>
<error statusCode=404 redirect=adminmessage.htm/>
<error statusCode=403
redirect=noaccessallowed.htm/>
</customErrors>
Copyright 2008, Infosys Technologies Ltd. 9
Error Handling (Contd)
The mode attribute can be one of the following:
On
Error details are not shown to anybody, even local users
If you specify a custom error page it will be always used

Off
Everyone will see error details, both local and remote users
If you specify a custom error page it will NOT be displayed

RemoteOnly
Local users will see detailed error pages
Remote users will be presented with a concise page notifying them that an
error occurred

Note : Local user means User browsing the site on the same machine
where web applications are deployed
Copyright 2008, Infosys Technologies Ltd. 10
HTTP Status Codes
When content on a server that is running Internet Information Services
(IIS) is accessed using HTTP, IIS returns a numeric code that indicates the
status of the response

A few of the common Status Codes are:
404 - Not found
401 - Access denied.
404.7 - File extension denied.

Copyright 2008, Infosys Technologies Ltd. 11
Can you answer these questions?
What is difference between error handling done with Try-Catch-Finally
and the one that we discussed just now ?

Where you can see all the status codes for some standard errors?

Find out the meaning of status codes 501 and 502.

Copyright 2008, Infosys Technologies Ltd.
Data Binding

Data binding helps us to develop applications where controls can be
bound to the values stored in data source
Syntax for data binding is <%# source %>
When the DataBind method of the control is called, the expression is
evaluated and bound
DataBind for a single control (and subcontrols)
Page.DataBind binds all controls on a page

Copyright 2008, Infosys Technologies Ltd.
Data Binding
Supports binding to any data source
Properties, expressions, method calls
Collections (Array, Hashtable, etc.)
DataSet, DataTable, DataView, DataReader
XML
Single Value Data Binding
Involves binding server controls which can display single data at a time
TextBox,Label etc
Multi Record Data Binding
Involves binding server controls to structures ,ArrayList,DataView ,DataSet etc
DropDownList,ListBox,DataGrid,Repeater etc


Copyright 2008, Infosys Technologies Ltd. 14
Binding text to Label
Displaying Date and Time:
<asp:Label id=lblDateTime runat=server>
<%# DateTime.Now.ToString() %>
</asp:Label>


Displaying sum of two numbers:
<asp:Label id=lblSum runat=server>
<%# "The sum is "+ (23+10) %>
</asp:Label>

Copyright 2008, Infosys Technologies Ltd. 15
Binding text to DropdownList
// Create an object of ArrayList
ArrayList albooks=new ArrayList();
//Add Books into this collection
albooks.Add("Gone with The Wind");
albooks.Add("Visual Basic BlackBook");
albooks.Add("C# Complete Reference");
//set arraylist as dropdownlist datasource and bind it
drpBooks.DataSource =books;
drpBooks.DataBind();

ER/CORP/CRS/LA1010/003 Ver. No.: 4.0 Copyright 2008, Infosys Technologies Ltd.
Education and Research
We enable you to leverage knowledge anytime, anywhere!
Data Source controls in ASP.NET 3.5

Copyright 2008, Infosys Technologies Ltd. 17
Data Source controls
Data Controls
SqlDataSource Control
ObjectDataSource
SiteMapDataSource control
AccessDataSource control
XMLDataSource control
LINQ DataSource control



Copyright 2008, Infosys Technologies Ltd. 18
Data Source controls
SqlDataSource Control

Is a data source control that represents a connection to an ADO.NET SQL
database provider, such as SQL, OLEDB, ODBC, or Oracle
This control can be declaratively bound to any data-bound control that
supports the DataSourceID property
Replaces the ADO.NET code , normally write in a page to create a connection
and command to query a database



Copyright 2008, Infosys Technologies Ltd. 19
SqlDataSource Control

ConnectionString
ProviderName
SelectCommand


Copyright 2008, Infosys Technologies Ltd. 20
Object Data Source controls

ObjectDataSource

This control object model is similar to the SqlDataSource control.
ObjectDataSource exposes a TypeName property that specifies an object type
(class name) to instantiate for performing data operations
This control allows developers to structure their applications using this
traditional three-tiered architecture

Properties of Object Data Source Control
TypeName
SelectMethod
UpdateMethod
DeleteMethod
InsertMethod






Copyright 2008, Infosys Technologies Ltd. 21
ObjectDataSource Control
TypeName property:

This is one of the important attributes which needs to be set for any
ObjectDataSource control.
Here, a class name is to specified. And the class contains all the
methods for data access.
This class is to be kept in App_Code folder of ASP.NET application




Copyright 2008, Infosys Technologies Ltd. 22
ObjectDataSource Control
Few attributes for data access class/methods:

DataObject Attribute: (Used for classes)
If true, class becomes a data component and will be listed during configuration
of ObjectDataSource control, if Show only data components check box is
checked.

DataObjectMethod Attribute: (Used for methods)
Can have values like
DataObjectMethodType.Select
DataObjectMethodType.Update etc.


Copyright 2008, Infosys Technologies Ltd. 23
Data Source controls
SiteMapDataSource
This control enables you to declaratively data bind to site navigation data in
your pages
The SiteMapDataSource control is associated to a data-bound control through
the control's DataSourceID property
(Note: It is to be discussed after Navigation Controls)
XmlDataSource Control
This control supports declarative databinding to XML files
This Control is an example of a hierarchical data source
AccessDataSource Control
This control represents a connection to a Microsoft Access database.
This control derives from SqlDataSource
exposes a simple DataFile property for specifying the path to an MDB file,
instead of the complete connection string

Note: XmlDataSource, AccessDataSource and LinqDataSource will not be covered in
FP training


Copyright 2008, Infosys Technologies Ltd. 24
Data-bound Controls
Data Controls
GridView
Supports sorting, footers and headers
Events fired on the GridView

DetailsView
Great control that contains templates for CRUDing - Create, Retrieve, Update and
Delete (CRUD ) statements
EditTemplate, InsertTemplate, ReadOnlyTemplate, etc.

Form View



ER/CORP/CRS/LA1010/003 Ver. No.: 4.0 Copyright 2008, Infosys Technologies Ltd.
Education and Research
We enable you to leverage knowledge anytime, anywhere!
ASP.NET Objects
Copyright 2008, Infosys Technologies Ltd. 26
Request and Response Object Model
Client
Client
.
.
.
Request
Response
Request
Response
Web Server
Session 1
Session 2
Copyright 2008, Infosys Technologies Ltd. 27
ASP.NET Intrinsic objects
Object
Name
Description ASP.NET Class
Response We can use this class to inject text into
the page, write cookies etc
HttpResponse
Request Provides access to the current page
request, including the request headers,
cookies, client certificate, query string
etc
HttpRequest
Copyright 2008, Infosys Technologies Ltd. 28
ASP.NET Intrinsic objects
Object
Name
Description ASP.NET Class
Server Exposes utility methods that we
can use to transfer control
between pages, get information
about the most recent error and to
map physical path with virtual path
HttpServerUtility
Application Provides access to application-
wide methods and events for all
sessions
HttpApplicationState
Session Provides information to the current
user session
HttpSessionState
Copyright 2008, Infosys Technologies Ltd. 29
Request Object
Provides access to the current page request, including the request
headers, cookies, client certificate, query string and so on
Browser
Contains information sent by the client browser
Browser properties like Version Number, Whether Cookies enabled etc.
Cookies
Get information about client through cookies
QueryString
Get information about query string passed through URL


Request.QueryString[id]
Request.Cookies[cookiename]
svrname=Request.ServerVariables("SERVER_NAME")

Copyright 2008, Infosys Technologies Ltd. 30
Response Object
Used to send output to the user from the server
Used to write text to the page, write cookies etc



Response.Write(Hello)
Response.Add.Cookies(CookieObject)
Response.Redirect("http://www.microsoft.com/gohere/look.h
tm");

Copyright 2008, Infosys Technologies Ltd.
Response Object
HttpResponse.Redirect Method
Redirects a client to a new URL.

HttpResponse.Write Method
Writes information to an HTTP output content stream.

HttpResponse.Cookies Property
Gets the response cookie collection.

31
Copyright 2008, Infosys Technologies Ltd.
Server Object
The methods and properties of the HttpServerUtility class are exposed
through the intrinsic Server object provided by ASP.NET.

Provides helper methods for processing Web requests.

HttpServerUtility.MachineName Property
Gets the server's computer name.

HttpServerUtility.Transfer Method
For the current request, terminates execution of the current page and begins
execution of a new page using the specified URL path to the page.

32
Copyright 2008, Infosys Technologies Ltd. 33
Summary
Error Handling
Tracing ( Self Study)
ASP.NET Built in objects
Request
Response
Server
Session
Application




Copyright 2008, Infosys Technologies Ltd. 34 34
Thank You
The contents of this document are proprietary and confidential to Infosys
Technologies Ltd. and may not be disclosed in whole or in part at any time, to any
third party without the prior written consent of
Infosys Technologies Ltd.

2008 Infosys Technologies Ltd. All rights reserved. Copyright in the whole and
any part of this document belongs to Infosys Technologies Ltd. This work may not
be used, sold, transferred, adapted, abridged, copied or reproduced in whole or in
part, in any manner or form, or in any media, without the prior written consent of
Infosys Technologies Ltd.
ER/CORP/CRS/LA1010/003 Ver. No.: 4.0 Copyright 2008, Infosys Technologies Ltd.
Education and Research
We enable you to leverage knowledge anytime, anywhere!
Appendix
Copyright 2008, Infosys Technologies Ltd. 36
DataList Control
The DataList control displays data items in a repeating list
Features:
Provides list output with editing
Default look is a table
Customized via templates
Directional rendering (horizontal or vertical)
Alternate item
Updateable
No paging

Copyright 2008, Infosys Technologies Ltd.
Repeater Control
Standard templates for Repeater controls
HeaderTemplate: rendered once before all data bound rows
ItemTemplate: rendered once for each row in the data source
AlternatingItemTemplate: like ItemTemplate, but when present is used for
every other row
SeparatorTemplate: rendered between each row
FooterTemplate: rendered once, after all data bound rows



Copyright 2008, Infosys Technologies Ltd.
Repeater Control

To bind embedded controls to the data source of the container control
DataBinder.Eval() method is used
DataBinder.Eval() method is provided by .net to evaluate expression

Syntax
<%# DataBinder.Eval(Container.DataItem,DataFieldname") %>
Container represents the container for data items
DataField represents the name of data item field
ER/CORP/CRS/LA1010/003 Ver. No.: 4.0 Copyright 2008, Infosys Technologies Ltd.
Education and Research
We enable you to leverage knowledge anytime, anywhere!
Tracing in ASP.NET
Copyright 2008, Infosys Technologies Ltd. 40
Tracing
ASP.NET supports tracing
Easy way to include debug statements
No more messy Response.Write() calls
Great way to collect request details
Server control tree,Server variables, headers, cookies
Form/Query string parameters
Tracing provides a wealth of information about the page
Can be enabled at page or application level

<trace enabled="false
requestLimit="10
pageOutput="false
traceMode="SortByTime
localOnly="true" />
Copyright 2008, Infosys Technologies Ltd. 41
Tracing (Contd)
Methods
Trace.Write()
Writes category and text to trace
Trace.Warn()
Writes category and text to trace in red

Properties
Trace.IsEnabled
True if tracing is turned on for the application or just that page
Tracemode = [SortByTime | SortByCategory ]

Trace.axd
Http Handler( An Http Handler is a coding option that allows to handle
request/responses at the most basic level)