Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

50
ASP.NET 2.0 Chapter 8 Binding Data to Web Controls

Transcript of Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Page 1: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

ASP.NET 2.0

Chapter 8Binding Data to Web Controls

Page 2: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Objectives

ASP.NET 2.0, Third Edition 2

Page 3: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Designing a Web Data Application in the .NET Framework

Today, business needs demand flexible database management systems. This flexibility has been achieved by means of the data access model, which provides a method whereby data can be shared across different applications and platforms.

Data source is a term that represents the data. The application data source can be a simple data structure, XML data, a file database such as Microsoft Access, or stored in a data server such as SQL Server or Oracle. The data access layer, also called the data tier or middle tier, interfaces with the data source.

ASP.NET 2.0, Third Edition 3

Page 4: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Designing a Web Data Application in the .NET Framework (continued)

ASP.NET 2.0, Third Edition 4

Page 5: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Data Access Components

The DataSet object can contain one or more DataTable objects. The DataTable object contains one or more rows of data with one or more columns.

A DataReader object is a read-only, forward-only stream of data from the database. A DataReader object is similar to the DataView object because it returns one or more rows of data. The DataReader object, however, is not a disconnected source of data. The data connection remains open while the DataReader object reads the data, row by row. The DataReader object provides better performance than the DataView or DataSet objects. If you use the DataReader object, you can read the rows only once.

ASP.NET 2.0, Third Edition 5

Page 6: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Creating DataSets and DataAdapters

ASP.NET 2.0, Third Edition 6

Page 7: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Creating DataSets and DataAdapters (continued)

ASP.NET 2.0, Third Edition 7

Page 8: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Creating DataSets and DataAdapters (continued)

ASP.NET 2.0, Third Edition 8

Page 9: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Creating DataSets and DataAdapters (continued)

ASP.NET 2.0, Third Edition 9

Page 10: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Creating DataSets and DataAdapters (continued)

ASP.NET 2.0, Third Edition 10

Page 11: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Creating DataSets and DataAdapters (continued)

ASP.NET 2.0, Third Edition 11

Page 12: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Creating DataSets and DataAdapters (continued)

ASP.NET 2.0, Third Edition 12

Page 13: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Creating DataSets and DataAdapters (continued)

ASP.NET 2.0, Third Edition 13

Page 14: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Data Binding Techniques

ASP.NET 2.0, Third Edition 14

Page 15: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Binding Techniques

The process of assigning the data source to a Web control is called data binding. The two types of data binding are single-expression binding and repeat-expression binding.

Web controls that are commonly used to display bound data are called Data controls.

Data binding allows you to get records synchronously, which means the ability to retrieve records is not tied to the page loading process.ASP.NET 2.0, Third Edition 15

Page 16: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Binding Data to Web Controls

ASP.NET 2.0, Third Edition 16

Page 17: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Binding Data to Web Controls (continued)

ASP.NET 2.0, Third Edition 17

Page 18: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Repeated-Expression Binding

The CheckBoxList and RadioButtonList controls both display the data using the XHTML input tag with the type set to check box or radio. These controls are often used to represent Boolean data values in the database.

The DropDownList and ListBox controls both display the data using the XHTML select tag. The DropDownList control usually only displays one value at a time, however, while the ListBox control generally displays all the values at the same time.

The Repeater control is a small, lightweight control. The Repeater control displays the data using controls that you can define. No visible interface or formatting is available with the Repeater control; it simply repeats the content that you define.

ASP.NET 2.0, Third Edition 18

Page 19: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Repeated-Expression Binding (continued)

The DataList control displays the data as a basic list. You can modify the layout of the list by changing the number of horizontal or vertical columns.

The GridView control repeats content you define once for each data row. When the control is rendered, the page places the data in a table.

The DetailsView control displays content in a table like the GridView control, but only for one data row. It is often used to display information about a record that has been selected from a GridView control.

The FormsView control repeats content you define once for each data row. The FormsView control uses templates to render the content. The FormsView control is often used to create custom forms to insert, edit, and delete records.

ASP.NET 2.0, Third Edition 19

Page 20: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Binding Data to Web Controls and Data Controls

Data source is identified with the DataSource property of the DropDownList control; specify which item is displayed on the web page using the DataTextField property.

You use the DataValueField property to specify the value property of the DropDownList items

The DataValueField property corresponds to the value property of the HTML <option>control

The value is formatted using the DataTextFormatString property of the DropDownList control The formats are entered using the standard format

string rules, such as {0:c} for currency

ASP.NET 2.0, Third Edition 20

Page 21: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Data Binding Methods

DataBinder.Eval method is used to bind data one way, which means that the data can be read from the database

Bind method is used to provide two-way binding, which means that the data can be read from the database and the data will be uploaded to the database if the value changes

Container.DataItem naming container stores the results as the first argument

ASP.NET 2.0, Third Edition 21

Page 22: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Binding Data in a Collection to a Web Control

ASP.NET 2.0, Third Edition 22

Page 23: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Binding Data to a Data Control

ASP.NET 2.0, Third Edition 23

Page 24: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Binding Data to a Data Control (continued)

ASP.NET 2.0, Third Edition 24

Page 25: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Binding Data to a Data Control (continued)

ASP.NET 2.0, Third Edition 25

Page 26: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Automatic Binding Techniques

Automatic binding allows you to use the built-in wizards, and bind to data sources such as a Microsoft Access or SQL Server database, XML file, sitemap file, or business object

You do not have to call the DataBind method automatically

ASP.NET 2.0, Third Edition 26

Page 27: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Binding Data to a DataList Control

The DataList control allows you to display values as a simple list

The number of columns repeated is formatted using the RepeatColumns property; the RepeatColumns property must be an integer

The RepeatDirections property stores the direction of the columns

ASP.NET 2.0, Third Edition 27

Page 28: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Binding Data to a DataList Control(continued)

ASP.NET 2.0, Third Edition 28

Page 29: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Binding Data to a DataList Control (continued)

ASP.NET 2.0, Third Edition 29

Page 30: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Start 5/21/085:00 PM Segment

ASP.NET 2.0, Third Edition 30

Page 31: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Binding Relational Data to a GridView Control

The GridView control binds data to a table The AutoGenerateColumns property does not

allow you to specify the columns that you want to bind to your data source (Note: You do not have to bind all of the data columns

in your data source to the GridView control) The HeaderText property allows you to change the

string displayed at the top of the column headings The DataFormatString property is used to display

currency and is applied to the values displayed within the GridView control

The AllowSorting and AllowPaging properties allow you to configure the GridView control to support sorting and paging features

ASP.NET 2.0, Third Edition 31

Page 32: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Binding Relational Data to a GridView Control (continued)

ASP.NET 2.0, Third Edition 32

Ch8_gridview.aspx Instructions on 441: how to use the

‘smart tag’ to configure

Page 33: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Amortization Table

Creating DataTable’s in program code http://

msdn.microsoft.com/en-us/library/system.data.datatable.aspx http://

msdn.microsoft.com/en-us/library/system.data.datatable_members.aspx

ASP.NET 2.0, Third Edition 33

Page 34: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Overview of the ADO.NET Data Model

ASP.NET 2.0, Third Edition 34

Page 35: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

ODBC and OLE DB

Open Database Connectivity (ODBC) was created to provide a common interface to relational database systems ODBC drivers are used to provide access to a

legacy ODBC-compliant database Object Linking and Embedding

Database (OLE DB) OLE DB providers allow your application to

access a database without an application- or database-specific interface

ASP.NET 2.0, Third Edition 35

Page 36: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

.NET Providers

SQL Server .NET Provider is used to connect to an SQL Server database

OLE DB .NET Provider is used to connect to any other data source that can be accessed via the OLE DB interface

ODBC .NET Provider is used to access data using the ODBC interface

Oracle Provider is used to access an Oracle database

ASP.NET 2.0, Third Edition 36

Page 37: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

.NET Providers (continued)

ASP.NET 2.0, Third Edition 37

Page 38: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

.NET Providers (continued)

The Connection object provides the connection to the database; within the data connection, you must specify the location of the database, the access permissions, and any additional required parameters using a connection string

The Command object is used to identify an SQL command or a stored procedure; SQL is the Structured Query Language that is used to identify what records to add, delete, or modify within the database

ASP.NET 2.0, Third Edition 38

Page 39: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Retrieving Data in the .NET Framework

The DataReader object is used to deliver a stream of data from the database

When you call the ExecuteReader method of the Command object, the DataReader object opens a connection to the database and then retrieves rows of data as they are located in the database The Read method of the DataReader object

returns a single row and caches each row in memory only

ASP.NET 2.0, Third Edition 39

Page 40: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Using the DataReader to Retrieve Data from a Database

ASP.NET 2.0, Third Edition 40

Page 41: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Using The Data Reader

Web site chapter 8 Ch8_datareader.aspx.vb Open a connection Set up a command with SQL Create a reader object with the result Iterate through the results and

concatenate one column together Populate a label with the result

ASP.NET 2.0, Third Edition 41

Page 42: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

The DataAdapter and DataSet Objects

The DataAdapter object primarily is used to populate a DataSet object

The DataSet object is effectively a private copy of the database, and does not necessarily reflect the current state of the database SelectCommand is used to retrieve data; when you

use SelectCommand, the data must be inserted into the DataSet object Fill method of the Data-Adapter object inserts the

data returned from the SelectCommand into the DataSet object

InsertCommand – add a new record UpdateCommand – modify the data within an existing

record DeleteCommand – remove a record from the

database ASP.NET 2.0, Third Edition 42

Page 43: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

DataAdapter and DataSet

ASP.NET 2.0, Third Edition 43

Page 44: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

DataSet and DataTables

ASP.NET 2.0, Third Edition 44

Page 45: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Using the DataSet to Retrieve Data

from a Database

ASP.NET 2.0, Third Edition 45

chapter8/ch8_dataset.aspx Create a connection Create a data adapter for the SQL

command and the connection Tell the adapter to fill a DataSet with the

content of a named table Bind the DataSet to a GridView control

Page 46: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Using the DataSet Object to Retrieve

Data from an XML File

ASP.NET 2.0, Third Edition 46

Ch8_dataset_new.aspx Look at structure of file Code behind

Open file as a StreamReader Populate a DataSet from the StreamReader Bind the DataSet to a DataList

Web form An item template picks out the fields of

interest and formats those

Page 47: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

The DataView Object

ASP.NET 2.0, Third Edition 47

A DataView gives a view of the data in a DataSet

A DataTable has a default view containing all its data

Ch8_dataview.aspx Create connection, SQL, DataAdapter Fill a DataSet Create a DataView from the first table in the

DataSet Bind the DataView to a GridView The GridView column templates pick out the

appropriate data

Page 48: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Summary The Data Access Model provides a way to organize a data

application logically. The data can be stored in various formats including XML, database files, and database servers. The data access layer provides the ability to connect to and manage the CRUD methods within the data source.

DataSet object is a disconnected set of data that is stored in memory. The DataSet object created by the DataAdapter object maintains the changes in the DataSet object, and pushes the changes to the server.

DataSet object consists of a DataTablesCollection and DataRelationsCollection. The DataTable object can consist of many DataView objects.

A DataView object is a subset of data from a single DataTable object. A DataSet object can be persisted as an XML document. You can import XML data into a DataSet object, or store the DataSet object as an XML document.

ASP.NET 2.0, Third Edition 48

Page 49: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Summary (continued) A DataReader object is a read-only, forward-only stream

of data from the database. The data connection remains open while the DataReader object reads the data.

Web controls use the DataSource property to identify the data to be bound.

The DataList control uses an ItemTemplate to display the values. When you use a template such as the ItemTemplate to display the data, use the DataItem property.

While the DataBinder.Eval method is used for one-way data binding, you need to use the Bind method for two-way data binding so the data can be read from the database and the data will be uploaded to the database if the value changes.

The GridView control allows you to create bound and unbound columns. Bound columns contain a value that is bound to a value in the database.

ASP.NET 2.0, Third Edition 49

Page 50: Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.

Summary (continued) The DataList and GridView controls use templates to create

the header, footer, and item sections of the Data control. You can format these controls using the Auto Format templates.

Repeater control must be edited manually in HTML view. No graphical tool exists to edit the Repeater control.

In the ADO.NET model, you connect to data sources using Managed Providers. These providers maintain the interface with the lower-level providers and drivers that communicate directly with the data source.

You can write your application to interface directly with the ADO.NET framework, or use the built-in DataSource objects and Data controls.

The ADO.NET object model consists of four primary objects: Connection, Command, DataAdapter, and DataReader. Each of the .NET Providers contains its own version of these four ADO.NET objects. The DataAdapter object serves as a bridge between the DataSet object and the data source. The DataReader object is a high-performance object that provides a stream of data. The data is read-only and forward-only.

ASP.NET 2.0, Third Edition 50