8/12/2019 Chapter 13 Slides ASP
1/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 1
Chapter 13
How to useSQL data sources
8/12/2019 Chapter 13 Slides ASP
2/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 2
ObjectivesApplied Use a SQL data source to get the data that an application requires. Use the Query Builder to create the Select statement for a SQL
data source. Use a DataList control to present the data thats ret rieved by a
SQL data source. Use a list control such as a drop-down list to present the datathats retrieved by a SQL data source.
Use a SQL data source to update the data in a database.
8/12/2019 Chapter 13 Slides ASP
3/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 3
Objectives (cont.)Knowledge Describe how a select parameter that gets its value from a control
is defined. Describe how you use the Eval and Bind methods within the
templates of a DataList control to bind the template controls tocolumns of a data source.
Describe the additional code thats generated for a data source tha tcan update the database.
Explain when you might change the DataSourceMode attribute ofa data source from its default setting of DataSet to DataReader.
Explain when you might want to store the data thats retrieved bya data source in the servers cache memory.
8/12/2019 Chapter 13 Slides ASP
4/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 4
The Product List application displayed in a webbrowser
8/12/2019 Chapter 13 Slides ASP
5/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 5
The starting dialog box of the Data SourceConfiguration Wizard
8/12/2019 Chapter 13 Slides ASP
6/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 6
Aspx code generated for a basic SqlDataSourcecontrol
8/12/2019 Chapter 13 Slides ASP
7/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 7
Basic SqlDataSource control attributes
Attribute DescriptionID The ID for the SqlDataSource control.Runat Must specify server.ConnectionString The connection string. In most cases, you
should use a
8/12/2019 Chapter 13 Slides ASP
8/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 8
The dialog boxes for defining a connection
8/12/2019 Chapter 13 Slides ASP
9/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 9
The dialog box for saving the connection string inthe web.config file
8/12/2019 Chapter 13 Slides ASP
10/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 10
The ConnectionStrings section of the web.configfile
Aspx code that refers to a connection string in theweb.config file
8/12/2019 Chapter 13 Slides ASP
11/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 11
The dialog box for defining the Select statement
8/12/2019 Chapter 13 Slides ASP
12/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 12
The dialog box for entering a custom Selectstatement
8/12/2019 Chapter 13 Slides ASP
13/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 13
The Add WHERE Clause dialog box
8/12/2019 Chapter 13 Slides ASP
14/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 14
The WHERE clause section after a condition hasbeen added
8/12/2019 Chapter 13 Slides ASP
15/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 15
The aspx code for a SqlDataSource control thatincludes a select parameter
8/12/2019 Chapter 13 Slides ASP
16/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 16
Elements used to define select parameters
SelectParameters ControlParameter QueryStringParameter FormParameter SessionParameter ProfileParameter CookieParameter
8/12/2019 Chapter 13 Slides ASP
17/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 17
The ControlParameter elementAttribute Description
Name The parameter name.Type The SQL data type of the parameter.ControlID The ID of the web form control that supplies the
value for the parameter.PropertyName The name of the property from the web form control
that supplies the value for the parameter.
8/12/2019 Chapter 13 Slides ASP
18/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 18
The Query Builder dialog box
8/12/2019 Chapter 13 Slides ASP
19/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 19
The dialog box for defining parameters
8/12/2019 Chapter 13 Slides ASP
20/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 20
Parameter sources
Source DescriptionControl The parameters value comes from a control on the
page.QueryString The parameters value comes from a query string in
the URL used to request the page.
Form The parameters value comes from an HTML formfield.
Session The parameters value comes from an item in sessionstate.
Profile The parameters value com es from a profile property.Cookie The parameters value comes from a cookie.
8/12/2019 Chapter 13 Slides ASP
21/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 21
A simple list displayed by a DataList control
The asp tag for the DataList control
8/12/2019 Chapter 13 Slides ASP
22/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 22
Basic attributes of the DataList control
Attribute DescriptionID The ID for the DataList control.Runat Must specify server. DataSourceID The ID of the data source to bind the data list to.
8/12/2019 Chapter 13 Slides ASP
23/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 23
The Item template in template-editing mode
A Header template
8/12/2019 Chapter 13 Slides ASP
24/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 24
Common template elements for a data list
Element DescriptionHeaderTemplate Displayed before the first item in the data
source.FooterTemplate Displayed after the last item in the data
source.ItemTemplate Displayed for each item in the data source.AlternatingItemTemplate Displayed for alternating items in the data
source.SeparatorTemplate Displayed between items.
8/12/2019 Chapter 13 Slides ASP
25/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 25
The Format page of the Properties dialog box
8/12/2019 Chapter 13 Slides ASP
26/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 26
Common style elements for a data listElement DescriptionHeaderStyle The style used for the header.FooterStyle The style used for the footer.ItemStyle The style used for each item in the data
source.
AlternatingItemStyle The style used for alternating items in the datasource.
SeparatorStyle The style for the separator.
The asp tag for a Header style
8/12/2019 Chapter 13 Slides ASP
27/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 27
The Data Source Configuration Wizard for bindinga drop-down list
8/12/2019 Chapter 13 Slides ASP
28/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 28
List control attributes for data bindingAttribute Description
DataSourceID The ID of the data source to bind the list to.DataTextField The name of the data source field that should be
displayed in the list.DataValueField The name of the data source field whose value
should be returned by the SelectedValue property ofthe list.
The aspx code for a drop- down list thats bound toa SQL data source
8/12/2019 Chapter 13 Slides ASP
29/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 29
The DataBindings dialog box for binding a control
8/12/2019 Chapter 13 Slides ASP
30/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 30
The syntax of the Eval and Bind methods
Code examples
8/12/2019 Chapter 13 Slides ASP
31/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 31
Default.aspx file: Product List application
Choose a category:
8/12/2019 Chapter 13 Slides ASP
32/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 32
Default.aspx file: Product List application (cont.)
ID Product
Unit Price
On Hand
8/12/2019 Chapter 13 Slides ASP
33/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 33
Default.aspx file: Product List application (cont.)
8/12/2019 Chapter 13 Slides ASP
34/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 34
Default.aspx file: Product List application (cont.)
8/12/2019 Chapter 13 Slides ASP
35/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 35
Default.aspx file: Product List application (cont.)
8/12/2019 Chapter 13 Slides ASP
36/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 36
The Advanced SQL Generation Options dialog
box
8/12/2019 Chapter 13 Slides ASP
37/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 37
The aspx code for a SqlDataSource control thatuses action queries
DeleteCommand="DELETE FROM [Categories]
WHERE [CategoryID] = @CategoryID"
8/12/2019 Chapter 13 Slides ASP
38/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 38
The aspx code for a SqlDataSource control thatuses action queries (continued)
8/12/2019 Chapter 13 Slides ASP
39/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 39
The DataSourceMode attributeAttribute Description
DataSourceMode DataSet or DataReader. The default is DataSet, but you can specify DataReader if the data sourceis read-only.
A SqlDataSource control that uses a data reader
8/12/2019 Chapter 13 Slides ASP
40/41
Murachs ASP.NET 3.5/C#, C13 2006, Mike Murach & Associates, Inc. Slide 40
SqlDataSource attributes for caching EnableCaching CacheDuration CacheExpirationPolicy CacheKeyDependency
8/12/2019 Chapter 13 Slides ASP
41/41
A SqlDataSource control that uses caching