Top Banner
Chapter 8 Working With Databases in ASP .NET
30

Chapter 8

Jan 07, 2016

Download

Documents

Kira

Chapter 8. Working With Databases in ASP .NET. Listing 8.1 – ShowListControls. Uses The SqlDataSource control for estabishing database connectivity and query/update Includes all the list controls: BulletedList, CheckBoxList, DropDownList, ListBox, and RadioButtonList - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 8

Chapter 8

Working With Databases in ASP .NET

Page 2: Chapter 8
Page 3: Chapter 8

Listing 8.1 – ShowListControls

• Uses The SqlDataSource control for estabishing database connectivity and query/update

• Includes all the list controls:– BulletedList, CheckBoxList, DropDownList, ListBox, and

RadioButtonList

• Each List control has these properties for data binding using an SqlDataSource– DataSourceId – the ID of the SqlDataSource object– DataTextField – the name of the column from the SQL

command that should be displayed

Page 4: Chapter 8

Listing 8.1 ShowListControls.aspx

Each of these are subclasses of the ListControl class. ListControl is an abstract class.

Page 5: Chapter 8

Listing 8.1 ShowListControls.aspxThe SQLDataSource control allows the .aspx page to set both database connections and default queries.

With this approach, database connectivity and query is part of the markup, not the code-behind

Page 6: Chapter 8

SqlDataSource Control

• Represents an SQL database to data-bound controls• Used to establish connection to a data source and

perform queries and updates to the data source.• Some Properties

– ConnectionString– SelectCommand– InsertCommand– UpdateCommand– DeleteCommand

Page 7: Chapter 8

ConnectionString Property

<asp:SqlDataSource id="srcMovies" ConnectionString="Data Source=.\SQLExpress; AttachDbFilename=|DataDirectory|MyDatabase.mdf; Integrated Security=True;User Instance=True" SelectCommand="SELECT Title FROM Movies" Runat="server" />

The ConnectionString property’s value depends on the database server and host’s configurations. Above is the connection string from the book. For the Lab computers use the following:

ConnectionString="Server=localhost;Database=MyDatabase;Trusted_Connection=Yes;"

Page 8: Chapter 8

SelectCommand

<asp:SqlDataSource id="srcMovies" ConnectionString="Data Source=.\SQLExpress; AttachDbFilename=|DataDirectory|MyDatabase.mdf; Integrated Security=True;User Instance=True" SelectCommand="SELECT Title FROM Movies" Runat="server" />

The SelectCommand property’s is a SQL SELECT statement.

Other properties include InsertCommand, UpdateCommand, and DeleteCommand.

Page 9: Chapter 8

List Control Data Binding Properties

All List controls (databound) include these two properties:

• DataSourceId – the ID of the SqlDataSource object

• DataTextField – the name of the column from the SQL SelectCommand that should be displayed

<asp:BulletedList id="BulletedList1" DataSourceId="srcMovies" DataTextField="Title" Runat="server" />

Page 10: Chapter 8

Browser’s rendering of HTML code.

Note, each list contains the result of the SQL SELECT query of the associated SqlDataSource, with the Title column being displayed.

Page 11: Chapter 8
Page 12: Chapter 8

Listing 8.6 BoundGridView.aspx

GridView is a databound control that renders an HTML table based on a query.

Page 13: Chapter 8
Page 14: Chapter 8

These six Web controls are more complex and can display multiple separate columns from a query.

Listing 8.2 ShowTabularDataBound.aspx

Page 15: Chapter 8

Browser’s rendering of HTML code.

Page 16: Chapter 8

Tabular DataBound Controls

• Multiple Data Records– GridView – HTML table, one record per

displayed row– DataList – HTML table, multiple records can

display on a row, requires templates– Repeater – does not render an HTML table,

requires templates– ListView – displayed via template; allows

sorting, paging, editing

• Single Data Record– DetailsView – HTML Table of a single record– FormView – Single record displayed via

templates, allows pagingSub

clas

ses

of

CompositeDataBoundControl

Subclasses of DataBoundControl(newer)

Not

des

cend

ed f

rom

DataBoundControl

(older)

http://msdn.microsoft.com/en-us/library/ms228214.aspx

Page 17: Chapter 8

Using Item Templates

ItemTemplates allow you to specify, using html and “old ASP”, specific content to display.

ItemTemplate is a sub-element of the data bound Web control.

Page 18: Chapter 8

Old ASP

Before .NET, ASP tags looked like this:

<%# server-side code here

%>

The Eval function call returns the value of the field identified in its argument.

Page 19: Chapter 8
Page 20: Chapter 8

Listing 8.10 ShowLinks.aspxItemTemplates can contain other <asp:> tags as sub-elements.

Page 21: Chapter 8
Page 22: Chapter 8

Listing 8.13 ShowFormView.aspx

EditItemTemplates can contain other <asp:> tags as sub-elements, and allow for Buttons to submit data to the server.

The UpdateCommand includes @FldName. This is called a placeholder.

This allows you to retrieve the data from the associated TextBox controls in the form (and DataKeyName for the primary key).

Page 23: Chapter 8
Page 24: Chapter 8

Listing 8.7 ShowControlParameter.aspx

This example illustrates the use of control parameters to modify queries based on user selection from a DropDownList

Page 25: Chapter 8

Database Tables for this Example

Two tables, with one-to-many relationship.

Page 26: Chapter 8

Listing 8.7 ShowControlParameter.aspx

A DropDownList databound to a SqlDataSource for the dominant table in the relationship.

Page 27: Chapter 8

Listing 8.7 ShowControlParameter.aspx

Clicking the button simply performs the submit to the server.

Page 28: Chapter 8

Listing 8.7 ShowControlParameter.aspx

Based on the user-selected value from the DropDownList, the other SqlDataSource can customize a query. This is done through the control parameter.

Page 29: Chapter 8

Listing 8.7 ShowControlParameter.aspx

A control parameter links a particular control to an item in a SelectCommand (or Update, Insert, Delete).

Note: there are other types of parameters. The SelectParameters sub-element represents a collection of parameters to be used for the SelectCommand. Each SelectParameter will be associated with a placeholder in the query.

Page 30: Chapter 8

Listing 8.7 ShowControlParameter.aspx

ControlParameter Properties:

• Name – identifies the placeholder to replace in the query

• ControlID – identifies the control that contains the value to put into the placeholder