0% found this document useful (0 votes)
119 views10 pages

Working With Databases: Retrieve and Display Data

The document discusses working with databases in ASP.NET, including: 1) An overview of ADO.NET objects like connections, data adapters, and datasets that provide a bridge between ASP.NET controls and backend data sources. 2) How to access and display data using data source controls and data view controls. 3) Details on working with specific controls like data grids, grid views, and binding data to controls from databases.

Uploaded by

Sanju Ravi
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)
119 views10 pages

Working With Databases: Retrieve and Display Data

The document discusses working with databases in ASP.NET, including: 1) An overview of ADO.NET objects like connections, data adapters, and datasets that provide a bridge between ASP.NET controls and backend data sources. 2) How to access and display data using data source controls and data view controls. 3) Details on working with specific controls like data grids, grid views, and binding data to controls from databases.

Uploaded by

Sanju Ravi
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/ 10

Working with databases: introduction - What are databases? - Working with ADO.

NET -
Overview of ADO.NET objects – Connections- Dataadapters - Data set - Accessing data with
server explorer -Understanding data source controls - Data binding using data bound controls -
Working with data grids - Customizing data grids - Working with grid view controls - Data
binding with grid view control- Creating a new data connection - Using access data source
class.
UNIT V

Working with databases

Introduction

ASP.NET allows the following sources of data to be accessed and used:


 Databases (e.g., Access, SQL Server, Oracle, MySQL)
 XML documents
 Business Objects
 Flat files
ASP.NET hides the complex processes of data access and provides much higher level of
classes and objects through which data is accessed easily. These classes hide all complex
coding for connection, data retrieving, data querying, and data manipulation.
ADO.NET is the technology that provides the bridge between various ASP.NET control
objects and the backend data source. In this tutorial, we will look at data access and
working with the data in brief.
Retrieve and display data
It takes two types of data controls to retrieve and display data in ASP.NET:
-A data source control - It manages the connection to the data, selection of data, and
other jobs such as paging and caching of data etc.
-A data view control - It binds and displays the data and allows data manipulation.

What are databases?

A database is a collection of information that is organized so that it can be easily accessed,


managed and updated.
Data is organized into rows, columns and tables, and it is indexed to make it easier to find
relevant information. Data gets updated, expanded and deleted as new information is
added. Databases process workloads to create and update themselves, querying the data
they contain and running applications against it.

Overview of ADO.Net
 ADO.NET provides a bridge between the front end controls and the back end
database. The ADO.NET objects encapsulate all the data access operations and the
controls interact with these objects to display data, thus hiding the details of
movement of data.
 The two key components of ADO.NET are Data Providers and DataSet .
DATA PROVIDER
The .Net Framework includes mainly three Data Providers for ADO.NET. They are
o Microsoft SQL Server Data Provider,
o OLEDB Data Provider
o ODBC Data Provider.
SQL Server uses the SqlConnection object,
OLEDB uses the OleDbConnection Object
ODBC uses OdbcConnection Object respectively.

The following figure shows the ADO.NET objects at a glance:

The four Objects from the .Net Framework provide the functionality of Data Providers in the
ADO.NET. They are
Connection Object
Command Object
DataReader Object
DataAdapter Object.
-The Connection Object provides physical connection to the Data Source.
-The Command Object uses to perform SQL statement or stored procedure to be executed
at the Data Source.
-The DataReader Object is a stream-based , forward-only, read-only retrieval of query
results from the Data Source, which do not update the data.
-Finally the DataAdapter Object , which populate a Dataset Object with results from a
Data Source .

THE DATASET
The dataset represents a subset of the database. It does not have a continuous connection
to the database. To update the database a reconnection is required. The DataSet contains
DataTable objects and DataRelation objects. The DataRelation objects represent the
relationship between two tables.

CONNECTION OBJECT
The Connection Object is a part of ADO.NET Data Provider and it is a unique session with
the Data Source. In .Net Framework the Connection Object is Handling the part of physical
communication between the application and the Data Source. Depends on the parameter
specified in the Connection String , ADO.NET Connection Object connect to the specified
Database and open a connection between the application and the Database . When the
connection is established , SQL Commands may be executed, with the help of the
Connection Object, to retrieve or manipulate data in the Database. Once the Database
activity is over , Connection should be closed and release the resources .

In ADO.NET the type of the Connection is depend on what Database system you are
working with. The following are the commonly using the connections in the ADO.NET
 SqlConnection
 OleDbConnection
 OdbcConnection

DATA ADAPTERS
DataAdapter is a part of the ADO.NET Data Provider. DataAdapter provides the
communication between the Dataset and the Datasource. We can use the DataAdapter in
combination with the DataSet Object. That is these two objects combine to enable both data
access and data manipulation capabilities.

The DataAdapter can perform Select , Insert , Update and Delete SQL operations in the Data
Source. The Insert , Update and Delete SQL operations , we are using the continuation of the
Select command perform by the DataAdapter. That is the DataAdapter uses the Select
statements to fill a DataSet and use the other three SQL commands (Insert, Update, delete)
to transmit changes back to the Database. From the following links describe how to use
SqlDataAdapter and OleDbDataAdapter in detail.
 SqlDataAdapter
 OleDbDataAdapter

DATA SET
The dataset represents a subset of the database. It does not have a continuous connection
to the database. To update the database a reconnection is required. The DataSet contains
DataTable objects and DataRelation objects. The DataRelation objects represent the
relationship between two tables.

ACCESSING DATA WITH SERVER EXPLORER


To connect to a database from Server Explorer
1. From the View menu, choose Server Explorer.
2. In Server Explorer right-click Data Connections and choose Add Connection.
3. If this is the first connection you make, the Choose Data Source dialog box
appears. Under Data Source choose the kind of data source to which you are
connecting, and then under Data Provider choose the appropriate provider for the
application you are working with. Finally click Continue.
4. In the Add Connection dialog box, enter the requested information.
This information is different for each provider. For more help on this tab, click
the Help icon at the top of the dialog box or press F1 with the dialog box selected.
5. Choose the Advanced button to open a list of settings you can change for the
selected provider.
6. Choose Test Connection to check the connection without closing the dialog box.
This way you can make adjustments to the settings if the connection does not
succeed.
7. Click OK.
Your connection appears in Server Explorer under the Data Connections node.

UNDERSTANDING DATA SOURCE CONTROLS


A data source control interacts with the data-bound controls and hides the complex data
binding processes. These are the tools that provide data to the data bound controls and
support execution of operations like insertions, deletions, sorting, and updates.
Each data source control wraps a particular data provider-relational databases, XML
documents, or custom classes and helps in:
 Managing connection
 Selecting data
 Managing presentation aspects like paging, caching, etc.
 Manipulating data
There are many data source controls available in ASP.NET for accessing data from SQL
Server, from ODBC or OLE DB servers, from XML files, and from business objects.
Based on type of data, these controls could be divided into two categories:
 Hierarchical data source controls
 Table-based data source controls

The data source controls used for hierarchical data are:


XMLDataSource - It allows binding to XML files and strings with or without schema
information.
SiteMapDataSource - It allows binding to a provider that supplies site map information.
The data source controls used for tabular data are:
Data source controls Description

SqlDataSource It represents a connection to an ADO.NET data provider that


returns SQL data, including data sources accessible via
OLEDB and ODBC.

ObjectDataSource It allows binding to a custom .Net business object that


returns data.

LinqdataSource It allows binding to the results of a Linq-to-SQL query


(supported by ASP.NET 3.5 only).

AccessDataSource It represents connection to a Microsoft Access database.


DATA BINDING USING DATA BOUND CONTROLS

ASP.NET provides a wide variety of rich controls that can be bound to data. Under the Data
tab of the Visual Studio Toolbox, you can get several controls under the Data tab that could
be used to display data from a data source, like a database or XML file.

The standard ASP.NET data presentation controls are:


 DataList
 DetailsView
 FormView
 GridView
 ListView
 Repeater

WORKING WITH DATA GRIDS


A DataGrid is a control that displays data in a customizable grid. It provides a flexible way
to display a collection of data in rows and columns. The hierarchical inheritance of DataGrid
class.
This is a control in Visual Basic .NET, which provides you with an interactive user-interface
to show information graphically. The user-interface is interactive, because the end-user of
the software can interact with it if the developer of the software allows. The end-user can
add, edit and remove columns if the developer allows. And also, they can edit the content if
the permission is given. The DataGridView control looks like the Excel spreadsheet, which
can be added onto the form so that the end-user can use it for specific purposes.
CUSTOMIZING DATA GRIDS

DataGridView is very powerful and flexible control for displaying records in a tabular (row-
column) form. Here I am describing a different way of databinding with a DataGridView
control.

Take a windows Form Application -> take a DataGridView control.

Follow the given steps.

Step 1 : Select DataGridView control and click at smart property. Look at the following
figure.

Step 2 : After clicking, a pop-up window will be open.

Step 3 : Click ComboBox.


Step 4 : Click at Add Project Data Source (Look at above figure). A new window will be
opened to choose Data Source Type.

Step 5 : Choose Database (By default it is selected) and click the next button. A new
window will be open to Database Model.

Step 6 : Select DataSet (By default it is selected) and click the next button. A new window
will be open.
The DataGridView control provides several properties that you can use to adjust the
appearance and basic behavior (look and feel) of its cells, rows, and columns. If you have
special needs that go beyond the capabilities of the DataGridViewCellStyle class, however,
you can also implement owner drawing for the control or extend its capabilities by creating
custom cells, columns, and rows.

To paint cells and rows yourself, you can handle various DataGridView painting events. To
modify existing functionality or provide new functionality, you can create your own types
derived from the existing DataGridViewCell , DataGridViewColumn ,
and DataGridViewRow types. You can also provide new editing capabilities by creating
derived types that display a control of your choosing when a cell is in edit mode.

-Getting rows: the Rows property


‘Rows’ is a read-only property, which is used to get a collection of all the rows.
-Getting columns: the Columns property
‘Columns’ is a read-only property, which is used to get a collection of all the columns.
-Selecting rows: the Selected property

WORKING WITH GRID VIEW CONTROLS


The GridView control is a feature rich and versatile control used to accept, display, and
edit data on a web page. It is a commonly used control in ASP.Net web applications. To
use a GridView control a DataSource control has to be attached to the GridView
control.

GridView control renders data items in an HTML table. Each data item is rendered in a
distinct HTML table row. For example, given code will demonstrates how we use the
GridView to display the contents of the database table.

DATA BINDING WITH GRID VIEW CONTROL


1) Click Design Button-> Add Grid View From Toolbox. ToolBox-> Data-> Grid View.
2) Right Click on Grid View-> choose data source -> select the database created for the projet
3) select the database and it will be displayed.

CREATING A NEW DATA CONNECTION


ASP.NET gives you flexibility in how you connect to databases. A simple way is to use data
source controls, which allow you to encapsulate data access in a control that you can configure
with connection and query information.
Write the steps you have done in the lab

USING ACCESS DATA SOURCE CLASS.


It uses OleDb data provider internally. OleDb data provider classes are defined in the
System.Data.OleDb namespace. The AccessDataSource class is a data source control that
works with Microsoft Access databases. Like its base class, SqlDataSource,
the AccessDataSource control uses SQL queries to perform dataretrieval.

You might also like