1 data bound controls ii chapter 15 - 16. 2 objectives you will be able to use a data source control...
TRANSCRIPT
![Page 1: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/1.jpg)
1
Data Bound Controls II
Chapter 15 - 16
![Page 2: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/2.jpg)
2
Objectives
You will be able to Use a Data Source control to get
data from a SQL database and make it available to display components
Use a GridView control to display data from a Data Source control. All without writing any C# code.
![Page 3: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/3.jpg)
Data Binding
Permits us to bind a control to a data source. Example: SQL Server database table. Many others.
Control automatically shows data from the data source. No C# code required.
Can update the data source with new values input by the user.
![Page 4: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/4.jpg)
4
The Northwind Traders Database
We will use the Customers table from the Northwind Traders database for an example today. Need own copy so that we can modify it.
Download script from the class web site: http://www.cse.usf.edu/~turnerr/Web_Application_Design/Downloads
/ File create_customers.sql
Put the file into a convenient folder.
![Page 5: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/5.jpg)
5
Script create_customers.sql
![Page 6: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/6.jpg)
6
Executing the Script
Set up VPN connection.
![Page 7: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/7.jpg)
7
Check the Table in Visual Studio
![Page 8: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/8.jpg)
8
Show Table Data
![Page 9: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/9.jpg)
9
Table Customers
![Page 10: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/10.jpg)
10
Set Primary Key
In order to update the table, it must have a Primary Key. Set in the standard Northwinds
Database version of the table. Not in our copy.
View Server Explorer Expand Customers table
Open Table Definition
![Page 11: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/11.jpg)
11
Set Primary Key
Right-click hereSelect Set Primary Key
![Page 12: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/12.jpg)
12
Set Primary Key
![Page 13: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/13.jpg)
13
Data Source Controls
Family of ASP.NET controls with similar functionality and identical interfaces
Connection information Query information Parameters
Avoid need to write low level code to access the database.
Bind to display controls to show the data on a web page.
![Page 14: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/14.jpg)
14
The GridView Control
Displays data in a grid Think Excel spreadsheet http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.a
spx http://msdn.microsoft.com/en-us/magazine/cc163933.aspx
Binds to a Data Source control
Many properties! Control appearance and behavior
![Page 15: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/15.jpg)
15
Create a New Web Site
![Page 16: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/16.jpg)
16
Rename the .aspx file
![Page 17: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/17.jpg)
17
Drag a SqlDataSource to the Page
![Page 18: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/18.jpg)
18
Configure the DataSource
Click here
Click here
![Page 19: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/19.jpg)
19
Set Up a new Connection
Click here
![Page 20: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/20.jpg)
20
Add Connection
Click here
![Page 21: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/21.jpg)
21
Configure the DataSource
Click here
![Page 22: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/22.jpg)
22
Save the Connection String
Say “yes”
![Page 23: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/23.jpg)
23
Specify the Query
![Page 24: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/24.jpg)
24
Test the Query
Click here
![Page 25: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/25.jpg)
25
Query Result
Click here
![Page 26: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/26.jpg)
26
The SqlDataSource Ready for Action
The SqlDataSource control holds the connection and query information that will be used by a data bound control.
![Page 27: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/27.jpg)
What We Wrote
![Page 28: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/28.jpg)
$ Expressions
<%$ ConnectionStrings:wpusr40 %>
Will be evaluated by the server as the .aspx file is processed.
Invokes an expression builder ConnectionStrings in this case.
Does not emit HTML. Used only to set a property value of the
enclosing ASPX object SqlDataSource in this case.
![Page 29: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/29.jpg)
web.config
![Page 30: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/30.jpg)
$ Expressions
<%$ ConnectionStrings:wpusr40 %>
The expression builder ConnectionStrings will return the connection string with name wpusr40 from the <connectionStrings> section of web.config.
That string will be the value of the ConnectionString property of SqlDataSource1.
Equivalent to writing
SqlDataSource1.ConnectionString = WebConfigurationManager.ConnectionStrings["wpusr40"].ConnectionString;
in Page_Load.
![Page 31: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/31.jpg)
31
Drag a GridView onto the Page
![Page 32: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/32.jpg)
32
Bind to the SqlDataSource
Click here to select data source
![Page 33: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/33.jpg)
33
Bind to the SqlDataSource
Select SqlDataSource1
![Page 34: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/34.jpg)
34
The GridView
The GridView is now specific for the data source.
![Page 35: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/35.jpg)
Try it!
![Page 36: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/36.jpg)
Zoom Out and Resize the Window
36
![Page 37: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/37.jpg)
37
Enable Paging
![Page 38: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/38.jpg)
38
View the Source
![Page 39: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/39.jpg)
Paging Enabled
39
![Page 40: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/40.jpg)
40
Make it look nicer.
Click on the GridView’s smart tag and select Auto Format. Explore the choices
![Page 41: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/41.jpg)
41
Enable Sorting
![Page 42: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/42.jpg)
42
Run the App!
Note the page selections.
Click on the column headings to sort by that column.
![Page 43: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/43.jpg)
43
Compare Paging to Scrolling
Back in the design view Click on the GridView’s smart tag and
unselect Enable Paging.
Build and run the app again. Try the scroll bar.
![Page 44: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/44.jpg)
44
Paging Disabled
![Page 45: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/45.jpg)
45
Adding Insert, Update, and Delete
Currently we can only display the table contents. No capability to modify the database.
Look at the source. Find <asp:SqlDataSource ... >
![Page 46: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/46.jpg)
46
The SqlDataSource
![Page 47: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/47.jpg)
47
Add Update Capability
To add the capability of updating the database, go back to design view. Click the smart tag on SqlDataSource1. Click Configure Data Source
![Page 48: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/48.jpg)
48
Add Update Capability
![Page 49: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/49.jpg)
49
Add Update Capability
Click here.
![Page 50: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/50.jpg)
50
Add Update Capability
Click here.
![Page 51: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/51.jpg)
51
Add Update Capability
Click here.
![Page 52: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/52.jpg)
52
Add Update Capability
Click here.
![Page 53: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/53.jpg)
53
Add Update Capability
Click here.
Click here to test.
![Page 54: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/54.jpg)
54
Back in Design View
Click on the GridView and then click on the smart tag
which will appear at the upper right corner.
![Page 55: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/55.jpg)
55
Adding Update Capability
Select Enable Editing and Enable Deleting
![Page 56: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/56.jpg)
56
Source View
![Page 57: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/57.jpg)
57
Try it!
Click on “Edit” link to edit a row.
![Page 58: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/58.jpg)
58
Doing Edits
Edit textboxes then click “Update”.
![Page 59: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/59.jpg)
59
Updated Field
![Page 60: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/60.jpg)
60
Back in Visual Studio Server Explorer
![Page 61: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/61.jpg)
61
Buttons Instead of Links
Click the GridView smart tag and select Edit Columns
![Page 62: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/62.jpg)
62
Buttons Instead of Links
Click here.
![Page 63: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/63.jpg)
63
Buttons Instead of Links
1. Select “CommandField”
2. Select Button from dropdown list.
3. Click OK
![Page 64: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/64.jpg)
64
Visual Studio Design View
![Page 65: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available](https://reader035.vdocuments.us/reader035/viewer/2022062518/56649ea25503460f94ba67aa/html5/thumbnails/65.jpg)
65
The App in Action