Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Working With Databases in ASP.NET. Listing 8.1 – ShowListControls Uses The SqlDataSource control for estabishing database connectivity and.

Similar presentations


Presentation on theme: "Chapter 8 Working With Databases in ASP.NET. Listing 8.1 – ShowListControls Uses The SqlDataSource control for estabishing database connectivity and."— Presentation transcript:

1 Chapter 8 Working With Databases in ASP.NET

2

3 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

4 Listing 8.1 ShowListControls.aspx Each of these are subclasses of the ListControl class. ListControl is an abstract class.

5 Listing 8.1 ShowListControls.aspx The 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

6 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

7 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;"

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.

9 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" />

10 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.

11

12 Listing 8.6 BoundGridView.aspx GridView is a databound control that renders an HTML table based on a query.

13

14 These six Web controls are more complex and can display multiple separate columns from a query. Listing 8.2 ShowTabularDataBound.aspx

15 Browser’s rendering of HTML code.

16 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 paging Subclasses of CompositeDataBoundControl Subclasses of DataBoundControl (newer) Not descended from DataBoundControl (older) http://msdn.microsoft.com/en-us/library/ms228214.aspx

17 Using Item Templates <asp:FormView id="FormView1" DataSourceId="srcMovies" AllowPaging="true" Runat="server"> directed by 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. <asp:Repeater id="Repeater1" DataSourceId="srcMovies" Runat="server"> directed by

18 Old ASP <asp:FormView id="FormView1" DataSourceId="srcMovies" AllowPaging="true" Runat="server"> directed by Before.NET, ASP tags looked like this: The Eval function call returns the value of the field identified in its argument. <asp:Repeater id="Repeater1" DataSourceId="srcMovies" Runat="server"> directed by

19

20 Listing 8.10 ShowLinks.aspx ItemTemplates can contain other tags as sub- elements.

21

22 Listing 8.13 ShowFormView.aspx EditItemTemplates can contain other 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).

23

24 Listing 8.7 ShowControlParameter.aspx This example illustrates the use of control parameters to modify queries based on user selection from a DropDownList

25 Database Tables for this Example Two tables, with one-to-many relationship.

26 Listing 8.7 ShowControlParameter.aspx A DropDownList databound to a SqlDataSource for the dominant table in the relationship.

27 Listing 8.7 ShowControlParameter.aspx Clicking the button simply performs the submit to the server.

28 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.

29 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.

30 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


Download ppt "Chapter 8 Working With Databases in ASP.NET. Listing 8.1 – ShowListControls Uses The SqlDataSource control for estabishing database connectivity and."

Similar presentations


Ads by Google