ASP - NET Class Notes
ASP - NET Class Notes
Net
&
ASP.Net UNIT-V
Mr.Chirag Joshi | DIT University
UNIT#V
ADO.NET: ODBC
The ODBC .NET data provider communicates with native ODBC drivers through COM
interop (.NET’s interoperability layer for COM). The following ODBC providers are
guaranteed to be compatible with the ODBC.NET data provider:
The .NET data provider for ODBC connects to ODBC data sources through the
OdbcConnection object. The ODBC driver connection string is specified using the
ConnectionString property. It includes settings needed to establish the connection to the data
source. The connection string format closely matches the ODBC connection string format.
Additionally, you can specify an ODBC data source name (DSN) or file DSN by setting the
ConnectionString attribute to "DSN=myDSN" or "FileDSN=myFileDSN". For more
information about specifying ODBC connection strings, see the topic “SQLDriverConnect”
in the ODBC Programmer’s Reference within MSDN Library.
The disconnected environment allows data retrieved from the data source to be manipulated
and later reconciled with the data source. The disconnected classes provide a common way to
work with disconnected data regardless of the underlying data source. They include the
DataSet, DataTable, DataColumn, DataRow, Constraint, DataRelationship, and DataView
classes. Finally, ADO.NET introduces the connected DataAdapter class to bridge the data
Connected Classes
The following classes are used by ADO.NET to communicate directly with the data source:
Connection
Maintains information required to connect to the data source through a connection string. The
connection string contains information such as the name of the data source and its location,
and authorization credentials and settings. The Connection class has methods to open and
close the connection, for transactions to be initiated on the connection, as well as control
other properties of the connection.
Command
Executes SQL statements or stored procedures against the data source. The command class
has a ParameterCollection object containing Parameter objects that allow parameterized SQL
statements and stored procedures to be used against the data source.
DataReader
Allows parameters for both parameterized queries and stored procedures to be defined and set
to appropriate values. The Parameter class is accessed through the ParametersCollection
object within a Command object. It supports input and output parameters as well as return
values from stored procedures.
Transaction
Allows transactions to be created on a connection so that multiple changes to data in a data
source are treated as a single unit of work and either all committed or cancelled.
DataAdapter
Bridges the data source and the disconnected DataSet or DataTable classes. The DataAdapter
wraps the connected classes to provide this functionality. It provides a method to retrieve data
Provides a consistent way to deal with disconnected data completely independently of the
data source. The DataSet is essentially an in-memory relational database, serving as a
container for the DataTable, DataColumn, DataRow, Constraint, and DataRelation objects.
The XML format serializes and transports a DataSet. A DataSet can be accessed and
manipulated either as XML or through the methods and properties of the DataSet
interchangeably; the XmlDataDocument class represents and synchronizes the relational data
within a DataSet object of DataColumn and DataRow classes. The DataTable allows
constraints such as foreign keys and unique constraints to be defined using the Constraint
class.
DataColumn
Corresponds to a column in a table. The DataColumn class stores metadata about the
structure of the column that, together with constraints, defines the schema of the table. The
DataColumn can also create expression columns based on other columns in the table.
DataRow
Corresponds to a row in a table and can examine and update data in the DataTable. The
DataTable exposes DataRow objects through the DataRowCollection object it contains. The
DataRow caches changes made to data contained in its columns, storing both original and
current values. This allows changes to be cancelled or to be later reconciled with the data
source.
Constraint
Allows constraints to be placed on data stored within a DataTable. Unique and foreign key
constraints can be created to maintain data integrity.
DataRelation
Provides a way to indicate a relationship between different DataTable objects within a
DataSet. The DataRelation relates columns in the parent and child tables allowing navigation
between the parent and child tables and referential integrity to be enforced through cascading
updates and deletes.
DataView
Allows data, once retrieved into a DataSet or DataTable, to be viewed in different ways. It
allows data to be sorted based on column values and for a subset of the data to be filtered so
that only rows matching specified criteria are displayed.
ASP.NET :
User makes a request for accessing application resource, a page. Browser sends this
request to the web server.
A unified pipeline receives the first request and the following events take place:
An object of the class ApplicationManager is created.
An object of the class HostingEnvironment is created to provide information regarding the
resources.
Top level items in the application are compiled.
Response objects are created. The application objects such as HttpContext, HttpRequest
and HttpResponse are created and initialized.
An instance of the HttpApplication object is created and assigned to the request.
The request is processed by the HttpApplication class. Different events are raised by this
class for processing the request.
Postback event handling - If the request is a postback (old request), the related event
handler is invoked.
Page rendering - At this stage, view state for the page and all controls are saved. The
page calls the Render method for each control and the output of rendering is written to
the OutputStream class of the Response property of page.
Unload - The rendered page is sent to the client and page properties, such as Response
and Request, are unloaded and all cleanup done.
At each stage of the page life cycle, the page raises some events, which could be
coded. An event handler is basically a function or subroutine, bound to the event, using
declarative attributes such as Onclick or handle.
Following are the page life cycle events:
PreInit - PreInit is the first event in page life cycle. It checks the IsPostBack property and
determines whether the page is a postback. It sets the themes and master pages, creates
dynamic controls, and gets and sets profile property values. This event can be handled
by overloading the OnPreInit method or creating a Page_PreInit handler.
Init - Init event initializes the control property and the control tree is built. This event
can be handled by overloading the OnInit method or creating a Page_Init handler.
InitComplete - InitComplete event allows tracking of view state. All the controls turn on
view-state tracking.
LoadViewState - LoadViewState event allows loading view state information into the
controls.
LoadPostData - During this phase, the contents of all the input fields are defined with
the <form> tag are processed.
PreLoad - PreLoad occurs before the post back data is loaded in the controls. This event
can be handled by overloading the OnPreLoad method or creating a Page_PreLoad
handler.
Load - The Load event is raised for the page first and then recursively for all child
controls. The controls in the control tree are created. This event can be handled by
overloading the OnLoad method or creating a Page_Load handler.
LoadComplete - The loading process is completed, control event handlers are run, and
page validation takes place. This event can be handled by overloading the OnLoadComplete
method or creating a Page_LoadComplete handler.
PreRender - The PreRender event occurs just before the output is rendered. By handling
this event, pages and controls can perform any updates before the output is rendered.
PreRenderComplete - As the PreRender event is recursively fired for all child controls,
this event ensures the completion of the pre-rendering phase.
SaveStateComplete - State of control on the page is saved. Personalization, control state
and view state information is saved. The HTML markup is generated. This stage can be
handled by overriding the Render method or creating a Page_Render handler.
UnLoad - The UnLoad phase is the last phase of the page life cycle. It raises the UnLoad
event for all controls recursively and lastly for the page itself. Final cleanup is done and
all resources and references, such as database connections, are freed. This event can be
handled by modifying the OnUnLoad method or creating a Page_UnLoad handler.
Hyper Text Transfer Protocol (HTTP) is a stateless protocol. When the client disconnects
from the server, the ASP.NET engine discards the page objects. This way, each web
application can scale up to serve numerous requests simultaneously without running out of
Methods Description
Adds an item to the view state
Add(name, value) collection and existing item is
updated.
Clear Removes all the items from the
collection.
Determines whether the specified object
Equals(Object) is equal to the current
object.
Session State
When a user connects to an ASP.NET website, a new session object is created. When session
state is turned on, a new session state object is created for each new request. This session
state object becomes part of the context and it is available through the page.
Session state is generally used for storing application data such as inventory, supplier list,
customer record, or shopping cart. It can also keep information about the user and his
preferences, and keep the track of pending operations.
Sessions are identified and tracked with a 120-bit SessionID, which is passed from client to
server and back as cookie or a modified URL. The SessionID is globally unique and random.
The session state object is created from the HttpSessionState class, which defines a collection
of session state items. The HttpSessionState class has the following properties:
Properties Description
SessionID The unique session
identifier.
The value of the session
state item with the specified
Item(name) name. This
is the default property of the
HttpSessionState class.
Count The number of items in the
session state collection.
Gets and sets the amount of
time, in minutes, allowed
TimeOut between
requests before the session-
state provider terminates the
session.
Application State
The ASP.NET application is the collection of all web pages, code and other files within a
single virtual directory on a web server. When information is stored in application state, it is
available to all the users. To provide for the use of application state, ASP.NET creates an
application state object for each application from the HTTPApplicationState class and stores
this object in server memory. Thisobject is represented by class file global.asax.
Application State is mostly used to store hit counters and other statistical data, global
application data like tax rate, discount rate etc. and to keep the track of users visiting the site.
The HttpApplicationState class has the following properties:
Properties Description
The value of the application state item
Item(name) with the specified name.
This is the default property of the
HttpApplicationState class.
Methods Description
Add(name, value) Adds an item to the application state collection.
ASP and ASP.Net are very different programming languages. ASP is a scripting
language, whereas ASP.NET is the web formulation of a compiled language (Visual Basic,
C#, J#,
C++, .Net). Moreover, unlike ASP, ASP.NET is an object-oriented language.
Process Isolation
ASP is run under the inetinfo.exe(IIS) process space and is therefore susceptible to
application crashes due to IIS being stopped or restarted.On the other hand, the ASP.NET
worker process is a distinct process (aspnet_wp.exe) separate from the IIS process
inetinfo.exe. The process model in ASP.NET is unrelated to process isolation
settings in IIS.
Interpretation vs. Compilation
When a traditional ASP page is requested, the text of that page is parsed linearly. All content
that is not server-side script is rendered as-is back to the response. All server-side script in the
page is first run through the appropriate interpreter (JScript or VBScript), the output of which
is then rendered back to the response.
In contrast, ASP.NET pages are always compiled into .NET classes housed within
assemblies. This class includes all of the server-side code and the static HTML, so once a
page is accessed for the first time (or any page within a particular directory is accessed),
subsequent rendering of that page is serviced by executing compiled code. This eliminates all
the inefficiencies of the scripting model of traditional ASP.
Performance Implications
Since ASP scripts are interpreted on the fly, there is a performance impact. A common
optimization for ASP applications, therefore, is to move a lot of server-side script into
precompiled COM components to improve response times. Since all components in
ASP.NET are assemblies, there is no performance degradation by using server-side code.
With ASP, intermingling server-side evaluation blocks with static HTML is less efficient
than a single server-side script block, because the interpreter has to be invoked multiple
times. To avoid this, many ASP developers resort to large blocks of server-side script,
replacing static HTML elements with Response.Write() invocations instead. For ASP.NET,
such steps are not required for performance improvement.
ASP allows different blocks of script within a page to be written in different scripting
languages. While this may be appealing in some ways, it also degrades performance by
requiring that a particular page load both scripting engines (JScript, VBScript) to process
a request, which takes more time and memory than using just one language. ASP.NET
has "code-behind" in .aspx files that are parsed and compiled. Multiple server-side
languages cannot be used within a single .aspx file.
Debugging
Since ASP involves scripts being interpreted, debugging is difficult. But with ASP.NET, all
the tools available to the .NET developer are applicable to the .aspx developer. Errors with
pages are generated as compiler errors, and there is a good chance that most errors will be
found at compilation time instead of runtime, because VB.NET and C# are both strongly
typed languages.
ASP:
i. It has limited oops support and not having built in support for xml.
ii. Very less development and debugging tool available. Meaning that difficult to debug the
code.
iii. ASP you can only do scripting using visual basic scripting and java scripting.
iv. Error handling is very poor.
v. It has no high level programming structure. Mixed of html and server side scripting.
vi. You must be entering first line as -
<%LANGUAGE="VBSCRIPT" CODEPAGE="960"%>
vii. It has no in built validation control. Meaning that validating page is difficult for
developers.
viii. In the classic ASP if you need to update code on the existing page then it is mandatory to
restart the server to get reflect.
ASP.NET
i. ASP.NET is full featured object oriented programming.
ii. It has full support of xml. Which helps easy data exchange.
iii. Various tools and compiler available. Microsoft Visual studio makes your debugging job
easier.
iv. ASP.NET we can use either C# or VB.NET as server side programming language.
v. ASP.NET gives you three tire architecture. It allow you to keep your business logic, views
everything separate. Meaning that easy to enhance applications.
vi. Error handling is very good.
vii. You are required to make language directive with page as below.
<%@Page Language="VB" CodePage="960"%>
<%@QutputCache Duration="60" VaryByParam="none" %>
viii. It has state management support.
ix. In built validation controls. It has rich validation set - custom validator, range validator,
regular expression, compare and require field validation control which makes your job easier.
PostBack is the name given to the process of submitting an ASP.NET page to the server for
processing. PostBack is done if certain credentials of the page are to be checked against some
sources (such as verification of username and password using database). This is something
that a client machine is not able to accomplish and thus these details have to be 'posted back'
to the server.
A post back is round trip from the client (Browser) to the server and then back to the client.
This enables you page to go through the asp engine on the server and any dynamic content to
be updated.
To work with the ASP.Net Web Controls events, we need a solid understanding of the web
page life cycle. The following actions will be taken place when a user changes a control that
has the AutoPostBack property set to true :
1. On the client side, the JavaScript _doPostBack function is invoked, and the page is
resubmitted to the server.
2. ASP.NET re-creates the Page object using the .aspx file.
3. ASP.NET retrieves state information from the hidden view state field and updates the
controls accordingly.
4. The Page.Load event is fired.
5. The appropriate change event is fired for the control. (If more than one control has
been changed, the order of change events is undetermined.)
6. The Page.PreRender event fires, and the page is rendered (transformed from a set of
objects to an HTML page).
7. Finally, the Page.Unload event is fired.
8. The new page is sent to the client.
The master page defines placeholders for the content, which can be overridden by content
pages. The output result is a combination of the master page and the content page . This
improves the maintainability of your site and avoids unnecessary duplication of code for
shared site structure or behaviour.
A single master page defines the look and feel and standard behaviour that you want for all of
the pages (or a group of pages) in your application. Defining a Master page is similar to
defining a normal page, but saving a Master page is different from saving a normal page. You
must save the Master pages by using the .master file extension. Master pages are completely
transparent to end users and permit developers to create Web sites where pages share a
common layout . The page layout for Master Pages is easy to create, simple to maintain, and
simple to assign to a Web application. The master page is identified by a special @ Master
directive that replaces the @ Page directive that is used for ordinary .aspx pages.
Creating a set of controls that are common across all the web pages and attaching them to all
the web pages.
A centralized way to change the above created set of controls which will effectively change
all the web pages.
Dynamically changing the common UI elements on master page from content pages based on
user preferences.