380 likes | 459 Views
Beginning Web Site Development Module 2 – Web Data Building Data-Driven Sites With ASP.NET and C# Version. Introduction. Target audience New to programming but want to build a dynamic data-backed Web site Prerequisites Basic understanding of HTML, familiarity with Web interfaces
E N D
Beginning Web Site DevelopmentModule 2 – Web DataBuilding Data-Driven SitesWith ASP.NET andC# Version
Introduction • Target audience • New to programming but want to build a dynamic data-backed Web site • Prerequisites • Basic understanding of HTML, familiarity with Web interfaces • Completed “Beginning Web Site Development Module 1” • Expectations • Learn the basics of building dynamic data-backed Web sites with Visual Web Developer Express and ASP.NET 2.0
Agenda • Dynamic, data-driven sites • Using SQL Server 2005 Express • Creating and using local databases • Structured Query Language (SQL) • Talking to a database with SQL • Data binding • Binding ASP.NET controls to data
Dynamic Web Sites • Many sites today are dynamic and interactive • Display content that is updated frequently • News sites, sports sites, stock analysis sites • Allow clients to interact with site • Shopping sites, internet portals, auction sites • Interaction requires storage…
Saving client-entered data • Where should user-entered data go? Web Server Client Browser AddPoem.aspx AddPoem.aspx.cs ?
Saving client-entered data • Problem: interactive Web pages need a place to store data • Solution: relational database • Store data in tabular format (think spreadsheet) • Handles multiple users simultaneously • Standard language (SQL) and many tools for manipulating data
SQL Server 2005 Express • VWD Express installs SQL 2005 Express • Local file-based database engine • Full SQL support, databases up to 4GB • Easy to deploy – copy database file (.mdf) to deployment server
Creating a database • Add SQL 2005 Express database • Stores data in tables you define Right click
Database tables • Databases store data in tables • Conceptually similar to spreadsheets Each table is described as a collection of columns, each storing a specific type of data A table contains rows of data that conform to the table description
Defining a table • Create table(s) to store data • Separate column for each attribute • Unique ID column Right click
Identity columns • Identity column • Column storing a unique number for each row • Good practice to have an identity column • Improves performance, adds flexibility • Unique ID generated for each new entry
Filling tables with data • You can add data to tables you create • Initial seed data or complete data for tables that store read-only data (like a list of items and their prices) Right click
Creating a SQL 2005 Express Database Creating the initial database Adding tables Specifying identity columns Populating table data
SQL • Structured Query Language (SQL) • Standard way to retrieve data from and modify data in a database • Basic understanding necessary to use ASP.NET data controls • Four standard operations for manipulating and retrieving data SELECT UPDATE INSERT DELETE
Testing queries • VWD Express Database Explorer provides query builder Run Query Right click Tables in query listed here Columns in query listed here Query text Query results
SELECTing data • SQL method for retrieving data is SELECT SELECT ID, Author, Quote FROM Quotes Column names Table name =
Constraining with WHERE • Can add criteria to constrain results of SELECT with WHERE clause SELECT ID, Author, Quote FROM Quotes WHERE (Author = 'William Shakespeare') = SELECT ID, Author, Quote FROM Quotes WHERE (Author LIKE '%ea%') =
INSERTing rows • SQL method for inserting rows is INSERT Column names Table name INSERT (Author, Quote) INTO Quotes VALUES ('Me', 'Life is good!') Values to insert
UPDATEing rows • SQL method for updating rows is UPDATE Table name New column value(s) UPDATE Quotes SET Author='Joe' WHERE ID=3 Constraint (which row(s) )
DELETEing rows • SQL method for deleting rows is DELETE Table name DELETE FROM Quotes WHERE ID=3 Constraint (which row(s) )
Interacting with a database using SQL Selecting data Inserting data Updating data Deleting data
Data binding • Data binding • A mechanism for displaying and modifying dynamic data from a Web page • ASP.NET supports data binding • Can point controls to database table as source for data • Displayed content drawn from table • Interface for modifying data built into some controls (update, delete, insert)
Data binding Client Browser Web Server Default.aspx Quotes Database Data-binding
Binding controls to data • Several ASP.NET controls designed to bind to data • GridView • Display/edit a database table as a grid • DetailsView • Display one table row at a time, insert new items • BulletedList • Display a list of items from a table • Many more …
SqlDataSource control • SqlDataSource control handles data retrieval, inserts, updates, and deletes • Acts as bridge between database and data-bound control • Contains SQL statements to perform database calls <asp:SqlDataSourceID="QuotesDataSource" runat="server" SelectCommand="SELECT ID, Author, Quote FROM Quotes" ConnectionString="…"/>
Adding a GridView 2. Open Database Explorer 3. Drag table onto page 4. Select desired options 5. Optionally Auto Format… 6. Edit Columns and setID column Visible=false 1. Set page to Design mode
Generated source <asp:GridViewID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText=“No records."> <Columns> <asp:BoundFieldDataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" Visible="False" /> <asp:BoundFieldDataField="Author" HeaderText="Author" SortExpression="Author" /> <asp:BoundFieldDataField="Quote" HeaderText="Quote" SortExpression="Quote" /> </Columns> </asp:GridView> <asp:SqlDataSourceID="SqlDataSource1" runat="server" SelectCommand= "SELECT ID, Author, Quote FROM Quotes" ConnectionString= "<%$ ConnectionStrings:QuotesConnectionString1 %>"/> GridView declaration BoundFields for columns SqlDataSource declaration SQL query to retrieve data Connection string pointing to local Quotes.mdf file web.config Default.aspx <configuration> <connectionStrings> <addname="QuotesConnectionString1" connectionString="Data Source=… " providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
Adding a GridView Adding a GridView to display table data SqlDataSource control Enabling sorting and paging Formatting the GridView
SQL parameters • SQL supports parameters to fill values in dynamically • SQL Server syntax is @varname • Before executing statement, parameters must be associated with values UPDATE Quotes SET Author=@Author WHERE ID=@ID parameters
SqlDataSource parameters • How to associate parameters with SqlDataSource Add Update and Delete commands with parameters <asp:SqlDataSourceID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:QuotesConnectionString1 %>" SelectCommand="SELECT [ID], [Author], [Quote] FROM [Quotes]" UpdateCommand="UPDATE Quotes SET Author=@Author, Quote=@Quote WHERE ID=@ID" DeleteCommand="DELETE FROM Quotes WHERE ID=@ID"> <UpdateParameters> <asp:ParameterName="Author" Type="String" /> <asp:ParameterName="Quote" Type="String" /> <asp:ParameterName="ID" Type="Int32" /> </UpdateParameters> <DeleteParameters> <asp:ParameterName="ID" Type="Int32" /> </DeleteParameters> </asp:SqlDataSource> List parameter names and types Note: Parameters should always be named the same as the corresponding column names to work properly with the GridView and DetailsView controls
GridView updates and deletes • Once your SqlDataSource has Update and Delete commands, the GridView can be enabled with Update and Delete features Must set DataKeyNames to identity column name to support Update/Delete
Inserting with a DetailsView • DetailsView provides insert feature • Can also be used to display/update/delete one row at a time Configure new data source
Inserting with a DetailsView • Add InsertCommand to data source • Enable Inserting and set DefaultMode=Insert <asp:SqlDataSourceID="SqlDataSource2" runat="server" ConnectionString="<%$ConnectionStrings:QuotesConnectionString1 %>" SelectCommand="SELECT [ID], [Author], [Quote] FROM [Quotes]" InsertCommand="INSERT INTO Quotes (Author, Quote) VALUES (@Author, @Quote)"> <InsertParameters> <asp:ParameterName="Author" Type="string" /> <asp:ParameterName="Quote" Type="string" /> </InsertParameters> </asp:SqlDataSource>
Updating, Deleting, and Inserting Enabling GridView Updating and Deleting Using a DetailsView for inserting
Summary • Interactive sites make the internet what it is today • VWD Express includes database engine • SQL Server 2005 Express • ASP.NET supports data binding • Display database content • Update, insert, delete
Resources • VWD 2005 Express home • http://msdn.microsoft.com/vstudio/express/vwd/default.aspx • SQL 2005 Express home • http://msdn.microsoft.com/vstudio/express/sql/default.aspx • Writing Transact-SQL Statements Tutorial • http://msdn2.microsoft.com/en-us/library/ms365303.aspx • ASP.NET 2.0 QuickStart Tutorials • http://quickstarts.asp.net/QuickStartv20/aspnet/Default.aspx