You are on page 1of 41

Chapter 13

How to use SQL data sources

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 1

Objectives
Applied 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 retrieved by a SQL data source. Use a list control such as a drop-down list to present the data thats retrieved by a SQL data source. Use a SQL data source to update the data in a database.

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 2

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 to columns of a data source. Describe the additional code thats generated for a data source that can update the database. Explain when you might change the DataSourceMode attribute of a data source from its default setting of DataSet to DataReader. Explain when you might want to store the data thats retrieved by a data source in the servers cache memory.

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 3

The Product List application displayed in a web browser

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 4

The starting dialog box of the Data Source Configuration Wizard

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 5

Aspx code generated for a basic SqlDataSource control


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString= "<%$ ConnectionStrings:HalloweenConnectionString %>" SelectCommand="SELECT [CategoryID], [LongName] FROM [Categories] ORDER BY [LongName]"> </asp:SqlDataSource>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 6

Basic SqlDataSource control attributes


Attribute ID Runat ConnectionString Description The ID for the SqlDataSource control. Must specify server. The connection string. In most cases, you should use a <%$ expression to specify the name of a connection string saved in the web.config file. The name of the provider used to access the database. The default is System.Data.SqlClient. The SQL Select statement executed by the data source to retrieve data.

ProviderName SelectCommand

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 7

The dialog boxes for defining a connection

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 8

The dialog box for saving the connection string in the web.config file

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 9

The ConnectionStrings section of the web.config file


<connectionStrings> <add name="HalloweenConnectionString" connectionString="Data Source=localhost\sqlexpress; Initial Catalog=Halloween;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>

Aspx code that refers to a connection string in the web.config file


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString= "<%$ ConnectionStrings:HalloweenConnectionString %>" SelectCommand="SELECT [CategoryID], [LongName] FROM [Categories] ORDER BY [LongName]"> </asp:SqlDataSource>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 10

The dialog box for defining the Select statement

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 11

The dialog box for entering a custom Select statement

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 12

The Add WHERE Clause dialog box

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 13

The WHERE clause section after a condition has been added

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 14

The aspx code for a SqlDataSource control that includes a select parameter
<asp:SqlDataSource ConnectionString= "<%$ ConnectionStrings:HalloweenConnectionString %>" ID="SqlDataSource2" runat="server" SelectCommand= "SELECT [ProductID], [Name], [UnitPrice], [OnHand] FROM [Products] WHERE ([CategoryID] = @CategoryID) ORDER BY [ProductID]"> <SelectParameters> <asp:ControlParameter Name="CategoryID" Type="String" ControlID="ddlCategory" PropertyName="SelectedValue" /> </SelectParameters> </asp:SqlDataSource>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 15

Elements used to define select parameters


SelectParameters ControlParameter QueryStringParameter FormParameter SessionParameter ProfileParameter CookieParameter

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 16

The ControlParameter element


Attribute Name Type ControlID PropertyName Description The parameter name. The SQL data type of the parameter. The ID of the web form control that supplies the value for the parameter. The name of the property from the web form control that supplies the value for the parameter.

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 17

The Query Builder dialog box

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 18

The dialog box for defining parameters

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 19

Parameter sources
Source Control QueryString Form Session Profile Cookie Description The parameters value comes from a control on the page. The parameters value comes from a query string in the URL used to request the page. The parameters value comes from an HTML form field. The parameters value comes from an item in session state. The parameters value comes from a profile property. The parameters value comes from a cookie.

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 20

A simple list displayed by a DataList control

The asp tag for the DataList control


<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource2"> <ItemTemplate> <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label> <asp:Label ID="lblUnitPrice" runat="server" Text='<%# Eval("UnitPrice", "{0:C}") %>'> </asp:Label> </ItemTemplate> </asp:DataList>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 21

Basic attributes of the DataList control


Attribute ID Runat DataSourceID Description The ID for the DataList control. Must specify server. The ID of the data source to bind the data list to.

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 22

The Item template in template-editing mode

A Header template

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 23

Common template elements for a data list


Element HeaderTemplate Description 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.

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 24

The Format page of the Properties dialog box

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 25

Common style elements for a data list


Element HeaderStyle FooterStyle ItemStyle AlternatingItemStyle SeparatorStyle Description The style used for the header. The style used for the footer. The style used for each item in the data source. The style used for alternating items in the data source. The style for the separator.

The asp tag for a Header style


<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 26

The Data Source Configuration Wizard for binding a drop-down list

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 27

List control attributes for data binding


Attribute DataSourceID DataTextField DataValueField Description The ID of the data source to bind the list to. The name of the data source field that should be displayed in the list. The name of the data source field whose value should be returned by the SelectedValue property of the list.

The aspx code for a drop-down list thats bound to a SQL data source
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True" Width="130px" DataSourceID="SqlDataSource1" DataTextField="LongName" DataValueField="CategoryID"/> </asp:DropDownList>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 28

The DataBindings dialog box for binding a control

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 29

The syntax of the Eval and Bind methods


<%# {Eval|Bind}(NameString [, FormatString]) %>

Code examples
<%# Eval("Name") %> <%# Eval("UnitPrice", "{0:C}") %> <%# Bind("UnitPrice", "{0:C}") %>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 30

Default.aspx file: Product List application


<body> <form id="form1" runat="server"> <div> <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/banner.jpg" /> <br /><br />Choose a category: <asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True" Width="130px" DataSourceID="SqlDataSource1" DataTextField="LongName" DataValueField="CategoryID"></asp:DropDownList> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HalloweenConnectionString %>" SelectCommand="SELECT [CategoryID], [LongName] FROM [Categories] ORDER BY [LongName]"></asp:SqlDataSource> <br /><br />

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 31

Default.aspx file: Product List application (cont.)


<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource2" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"> <HeaderTemplate> <table><tr> <td class="style1">ID</td> <td class="style2">Product</td> <td class="style3" align="right"> Unit Price</td> <td class="style3" align="right"> On Hand</td></tr> </table> </HeaderTemplate>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 32

Default.aspx file: Product List application (cont.)


<ItemTemplate> <table><tr> <td class="style1"> <asp:Label ID="lblID" runat="server" Text='<%# Eval("ProductID") %>'> </asp:Label></td> <td class="style2"> <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'> </asp:Label></td> <td class="style3" align="right"> <asp:Label ID="lblUnitPrice" runat="server" Text='<%# Eval( "UnitPrice", "{0:C}") %>'> </asp:Label></td>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 33

Default.aspx file: Product List application (cont.)


<td class="style3" align="right"> <asp:Label ID="lblOnHand" runat="server" Text='<%# Eval("OnHand") %>'> </asp:Label></td></tr> </table> </ItemTemplate> <AlternatingItemStyle BackColor="#CCCCCC" /> <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> </asp:DataList>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 34

Default.aspx file: Product List application (cont.)


<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:HalloweenConnectionString %>" SelectCommand="SELECT [ProductID], [Name], [UnitPrice], [OnHand] FROM [Products] WHERE ([CategoryID] = @CategoryID) ORDER BY [ProductID]"> <SelectParameters> <asp:ControlParameter Name="CategoryID" Type="String" ControlID="ddlCategory" PropertyName="SelectedValue" /> </SelectParameters> </asp:SqlDataSource> </div> </form> </body>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 35

The Advanced SQL Generation Options dialog box

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 36

The aspx code for a SqlDataSource control that uses action queries
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString= "<%$ ConnectionStrings:HalloweenConnectionString %>" SelectCommand="SELECT [CategoryID], [ShortName], [LongName] FROM [Categories]" InsertCommand="INSERT INTO [Categories] ([CategoryID], [ShortName], [LongName]) VALUES (@CategoryID, @ShortName, @LongName)" UpdateCommand="UPDATE [Categories] SET [ShortName] = @ShortName, [LongName] = @LongName WHERE [CategoryID] = @CategoryID"> DeleteCommand="DELETE FROM [Categories] WHERE [CategoryID] = @CategoryID" <DeleteParameters> <asp:Parameter Name="CategoryID" Type="String" /> </DeleteParameters>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 37

The aspx code for a SqlDataSource control that uses action queries (continued)
<UpdateParameters> <asp:Parameter Name="ShortName" Type="String" /> <asp:Parameter Name="LongName" Type="String" /> <asp:Parameter Name="CategoryID" Type="String" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="CategoryID" Type="String" /> <asp:Parameter Name="ShortName" Type="String" /> <asp:Parameter Name="LongName" Type="String" /> </InsertParameters> </asp:SqlDataSource>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 38

The DataSourceMode attribute


Attribute DataSourceMode Description DataSet or DataReader. The default is DataSet, but you can specify DataReader if the data source is read-only.

A SqlDataSource control that uses a data reader


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString= "<%$ ConnectionStrings:HalloweenConnectionString %>" DataSourceMode="DataReader" SelectCommand="SELECT [CategoryID], [LongName] FROM [Categories] ORDER BY [LongName]" </asp:SqlDataSource>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 39

SqlDataSource attributes for caching


EnableCaching CacheDuration CacheExpirationPolicy CacheKeyDependency

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 40

A SqlDataSource control that uses caching


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString= "<%$ ConnectionStrings:HalloweenConnectionString %>" EnableCaching="True" CacheDuration="60" SelectCommand="SELECT [CategoryID], [LongName] FROM [Categories] ORDER BY [LongName]" </asp:SqlDataSource>

Murachs ASP.NET 3.5/C#, C13

2006, Mike Murach & Associates, Inc.

Slide 41

You might also like