0% found this document useful (0 votes)
62 views11 pages

Data Access

Uploaded by

manthanb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views11 pages

Data Access

Uploaded by

manthanb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Asp.

net Data Access

ASP.NET includes features that enable you to add data access to your ASP.NET Web
pages with little or no code. You can connect to databases, XML data and files, and
business objects as data sources. You can then display data by using a variety of
controls that provide great flexibility in how you present data on the page.

Data Source Controls

ASP.NET includes data source controls that allow you to work with different types of
data sources such as a database, an XML file, or a middle-tier business object. Data
source controls connect to and retrieve data from a data source and make it
available for other controls to bind to, without requiring code. They can also support
modifying data.

ObjectDataSource

Enables you to work with a business object or other class and create Web
applications that rely on middle-tier objects to manage data. Supports advanced
sorting and paging scenarios unavailable with the other data source controls.

SqlDataSource

Enables you to work with Microsoft SQL Server, OLE DB, ODBC, or Oracle databases.
When used with SQL Server, supports advanced caching capabilities. The control
also supports sorting, filtering, and paging when data is returned as
a DataSet object.

XmlDataSource

Enables you to work with an XML file, especially for hierarchical ASP.NET server
controls such as the TreeView or Menu control. Supports filtering capabilities using
XPath expressions and enables you to apply an XSLT transformation to the data.
TheXmlDataSource allows you to update data by saving the entire XML document
with changes.

SiteMapDataSource

Used with ASP.NET site navigation

AccessDataSource

Enables you to work with a Microsoft Access database. Supports sorting, filtering,
and paging when data is returned as a DataSetobject.
SqlDataSource

The SqlDataSource control enables you to use a Web server control to access data
that is located in a relational data base. This can include Microsoft SQL Server and
Oracle databases, as well as OLE DB and ODBC data sources. You can use
the SqlDataSource control with data-bound controls such as theGridView, FormView,
and DetailsView controls to display and manipulate data on an ASP.NET Web page,
using little or no code.

Data-Bound Web Server Controls

Every ASP.Net web form control inherits the DataBind method from its parent
Control class, which gives it an inherent capability to bind data to at least one of its
properties. This is known as simple data binding or inline data binding.

The controls capable of declarative data binding

GridView:

The GridView control displays data as a table and provides the capability to sort
columns, page through data, and edit or delete a single record.

DetailsView

The DetailsView control renders a single record at a time as a table and provides
the capability to page through multiple records, as well as to insert, update, and
delete records. The DetailsView control is often used in master-detail scenarios
where the selected record in a master control such as aGridView control determines
the record displayed by the DetailsView control.

FormView

The FormView control renders a single record at a time from a data source and
provides the capability to page through multiple records, as well as to insert,
update, and delete records, similar to the DetailsView control. However, the
difference between the FormView and the DetailsView controls is that
the DetailsView control uses a table-based layout where each field of the data
record is displayed as a row in the control. In contrast, the FormView control does
not specify a pre-defined layout for displaying a record. Instead, you create
templates that contain controls to display individual fields from the record. The
template contains the formatting, controls, and binding expressions used to lay out
the form.
Configuring SqlDataSource:

Steps

1) Take a gridview on form.

2) go to smart field choose datasource and select sql data source


2) Choose connection string
3) Next select your table from dropdown.

5) Go to advance for insert update and delete.


6) Next and test the query.

Parameterized Commands

You can use parameterized queries for commands and supply parameters at
run time
Using parameter commands
Objectdatasource and sqldatasource.

Allows specifying a parameterized filter expression for a control.

Using filterparameters collection.


The parameters value can retrieve data from controls, the cookies, form
fields, query string object and session state.
Using Store procedure

Stored procedures (sprocs) are generally an ordered series of Transact-SQL


statements bundled into a single logical unit. They allow for variables and
parameters, as well as selection and looping constructs. A key point is that
sprocs are stored in the database rather than in a separate file.

Advantages over simply sending individual statements to the server


include:

1. Referred to using short names rather than a long string of text;


therefore, less network traffiic is required to run the code within the
sproc.
2. Pre-optimized and precompiled, so they save an incremental amount
of time with each sproc call/execution.
3. Encapsulate a process for added security or to simply hide the
complexity of the database.
4. Can be called from other sprocs, making them reusable and reducing
code size.

Customizing GridView

Gridview can be customized by using

Formatting fields by using styles.

Using additional template fields to add custom columns.

Using template field to configure the appearance of data items.


Data manipulation using GridView Control

Gridview relies on data source controls capabilities and provide automatic


sort, update and delete

Functionality.

Gridview control support select , update and delete operation via properties

Like:

AutoGenerated Select

AutoGenerated Delete

AutoGenerated Edit
Multi-tier Application

The objectdatasource control enable developers to use an Asp.net data


source control while retaining their three-tier application architecture.

The ASP.NET ObjectDataSource control represents a middle-tier object with


data retrieval and update capabilities. The ObjectDataSource control acts as
a data interface for data-bound controls such as the GridView, FormView,
or DetailsView controls. You can use these controls to display and edit data
from a middle-tier business object on an ASP.NET Web page.

Each time the ObjectDataSource needs to make a CRUD operation, it will try
to find (using reflection) the method to call (of course, you need to set some
properties to help it).
If it’s a static method, it will get the parameters and call it.
If it’s an instance method, it will create an object of the class, it will call the
method, and then destroy the instance (there is an exception to this, but
more about it later).

Displaying Master/Detail Data from a Database


first examine how to display master/detail data from a one-to-many relationship
when such data is coming directly from a database. On our ASP.NET page we'll
need two user interface elements:

A DropDownList that lists the items from the "one" entity.

A GridView that will display the records from the "many" entity that are
associated with the selected "one" entity.

The database has a number of one-to-many relationships. For this


demonstration, let's use the one-to-many relationship between the Products
table and the Order Details table. (This relationship exists and is evident
since the Order Details table has the foreign key field ProductID, indicating
that one product can exist in an arbitrary number of Order Detail records.)
Caching DataSource Control

DataSource Controls can cache data. Improves the performance of


applications in which the data does not change frequently.
One of the most important factors in building high-performance, scalable Web
applications is the ability to store items, whether data objects, pages, or parts
of a page, in memory the initial time they are requested. You can cache, or
store, these items on the Web server or other software in the request stream,
such as the proxy server or browser

ASP.NET navigation controls overview

Menu

The Menu control allows you to develop both statically and dynamically
displayed menus for your ASP.NET web pages. You can configure the
contents of the Menu control directly in the control, or you can specify the
contents by binding the control to a data source.
You can define content for the Menu control in two ways: by interactively (or
programmatically) adding individual menu items or by data binding the
control to a Site Map or XML data source.

TreeView

The TreeView Web server control is used to display hierarchical data, such as
a table of contents or file directory, in a tree structure. It supports the
following features:
Automatic data binding, which allows the nodes of the control to be bound to
hierarchical data, such as an XML document.
Site navigation support through integration with
the SiteMapDataSource control.
Node text that can be displayed as either selectable text or hyperlinks.

SiteMapPath

The SiteMapPath control displays a navigation path (which is also known as


a breadcrumb or eyebrow) that shows the user the current page location and
displays links as a path back to the home page. The control provides many
options for customizing the appearance of the links

source, which supports the required interfaces to allow hierarchical data-


bound controls, such as TreeView and Menu, to traverse the hierarchy of
nodes to produce their rendering. XmlDataSource also supports the tabular
data source interfaces required to bind list controls
like GridView, DropDownList, and DataList.

Xmldatasource

The XmlDataSource control belongs to the family of data source controls in


ASP.NET, which enables a declarative databinding model against a variety of
underlying data stores. The XMLDataSource control supports declarative
databinding to XML files. XmlDataSource is an example of a hierarchical data

SiteMapDataSource

The SiteMapDataSource 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

You might also like