Work with data in ASP.NET
-
Upload
peter-gfader -
Category
Education
-
view
2.583 -
download
0
description
Transcript of Work with data in ASP.NET
![Page 1: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/1.jpg)
Developing Windows and Web Applications using Visual Studio.NET
Eric Phan
Solution Architect
![Page 2: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/2.jpg)
Homework?
Address Control
Credit Card Validation
![Page 3: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/3.jpg)
http://sharepoint.ssw.com.au/Training/UTSNET/
Part 2: .NET Webforms
1. Overview of .NET Webforms2. Data in Webforms TODAY3. Usability
• Rich Web Forms and • Other ASP.NET Features
4. Web Security 5. Advanced Topics & Future Technology (Silverlight)
Webforms
![Page 4: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/4.jpg)
Recap
![Page 5: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/5.jpg)
ClientGet URL Request ServerResponse
1
2
![Page 6: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/6.jpg)
Client
Post requestwith data
ServerResponse
![Page 7: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/7.jpg)
Session 7: Data in the Web• Goal: Bringing data to the weband back :-)
•Old Style
•New Style
•DataBinding
•DataGrid
•Input Validation
Click icon to add picture
![Page 8: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/8.jpg)
Data in our database
![Page 9: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/9.jpg)
Data
![Page 10: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/10.jpg)
Bring database to the web
Data
Show/view
Enter
![Page 11: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/11.jpg)
OLD way
![Page 12: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/12.jpg)
Set of classes that expose data access services to the .NET programmer
ADO.NET provides a rich set of components for creating distributed, data-sharing applications
It is an integral part of the .NET Framework, providing access to relational data, XML, and application data
What is ADO.NET?
![Page 13: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/13.jpg)
WebForm Apps WinForm Apps
DataSets
SQL Data Providers
SQLConnection
SQLCommand
SQLDataAdapter
SQLDataReader
SQL Data XML DataOracle Data
…
ADO.NET
![Page 14: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/14.jpg)
Objects
Instead of DataSets we use classes
Extensible Can embed logic and validation A more OO approach
![Page 15: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/15.jpg)
WebForm Apps WinForm Apps
Objects/Entities
Data Sources
SQL Data XML DataOracle Data
…
![Page 16: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/16.jpg)
Why use DataSets
A highly functional container for data that can be strongly typed
Automatic change tracking for contained data Seamless data-binding support
Why Use Objects
Business Objects (NHibernate, NTiers, LINQ to SQL, LINQ to Entities)
Light weight Good data-binding support Extensible and OO Very easy to use No SQL command Strings
DataSets or Business Objects
![Page 17: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/17.jpg)
The new way!
LINQ 2 EntitiesLINQ 2 SQL
![Page 18: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/18.jpg)
Brand new in the .NET 3.5 Framework
Easily access and query your database through classes
Logical access to class hierarchies Supports Inheritance Intuitive to use
LINQ to SQL/Entities
![Page 19: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/19.jpg)
Database Mapping Language
Contains a mapping of classes to database tables
Has built in logic to handle selection, updates and deletes
Can be customized
www.hookedonlinq.com
DBML File
![Page 20: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/20.jpg)
var customers = from c in db.Customers
where c.FirstName.StartsWith(“A”)select c
SQL Syntax
![Page 21: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/21.jpg)
var customers = db.Customers.Where(c => c.FirstName.StartsWith(“A”);
Lambda Syntax
![Page 22: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/22.jpg)
Challenge, use this instead of SQL Server Management Studio for a month
LINQPad
![Page 23: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/23.jpg)
![Page 24: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/24.jpg)
See our Rules
http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterLINQ.aspx
![Page 25: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/25.jpg)
Databinding
![Page 26: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/26.jpg)
Bind the data
DataData source
![Page 27: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/27.jpg)
From MSDN: Data binding is the process that establishes connection between the application UI and business logic
Binding controls to data from database
Bind a grid on Webform to a database table
Simple, convenient, and powerful way to create a read/write link between the controls on a form and the data in their application
Data Binding
DataData source
![Page 28: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/28.jpg)
C#NorthwindDataContext db = new NorthwindDataContext();
GridView1.DataSource = db.Customers;
GridView1.DataBind();
English Instantiate our data access class Make the datasource of GridView1 all the customers
from our database Bind the Grid
Databinding the GridView (code)
![Page 29: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/29.jpg)
Databinding the GridView (designer)
![Page 30: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/30.jpg)
Databinding the GridView (designer)
![Page 31: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/31.jpg)
Databinding the GridView (designer)
![Page 32: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/32.jpg)
Databinding the GridView (designer)
![Page 33: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/33.jpg)
Databinding the GridView (designer)
![Page 34: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/34.jpg)
Databinding the GridView (running)
![Page 35: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/35.jpg)
GridView
• Select sort and edit
• Display a Table from a Database
• Easy to configure
• Without writing any code at all
![Page 36: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/36.jpg)
Lots of built in features like:
SortingPagingInsertingUpdating DeletingSelecting
GridView
![Page 37: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/37.jpg)
Enable Delete, Insert, Update on the DataSource
Enabling these features
![Page 38: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/38.jpg)
Enabling these features
Enable them on the GridView
![Page 39: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/39.jpg)
Easy!
![Page 40: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/40.jpg)
Bindable to Lists DataList DetailsView FormView Repeater/ListView DropDownList ListBox CheckBoxList RadioButtonList
Bindable to Properties/Columns Most other controls
Other Data Bindable Web Controls
![Page 41: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/41.jpg)
Most controls in .NET Framework support data binding
Controls inherit from the WebControl base classwhich implements IDataBindingsAccessor interface
Important Data-binding properties
DataSource DataMember DisplayMember ValueMember
Design your own controls or purchase 3rd party controls
Data-Bound Controls
![Page 42: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/42.jpg)
Passing Parameters
![Page 43: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/43.jpg)
Pass parameters to pages or controls
Pass parameters to our data sources
Parameters can be obtained from: Query String Session Variable Control Property Hidden Field Cookie
Passing Parameters between pages
![Page 44: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/44.jpg)
Add a Details View and create a new Datasource
1. Create a CustomerDetails.aspx
![Page 45: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/45.jpg)
2. Create a LINQ Data Source
![Page 46: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/46.jpg)
3. Specify the Where clause
![Page 47: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/47.jpg)
4. Make it read from the Query String
![Page 48: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/48.jpg)
5. Change the Select on the GridView to Link to the CustomerDetails Page
![Page 49: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/49.jpg)
6. Edit the templates
![Page 50: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/50.jpg)
7. Delete Select and Add a Hyperlink
![Page 51: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/51.jpg)
CustomerDetails.aspx?CustomerID={0}
8. Configure the Hyperlink
![Page 52: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/52.jpg)
Run it
![Page 53: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/53.jpg)
Validation – Don’t trust anyone
![Page 54: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/54.jpg)
Validation - Bring database to the web
Data
Show/view
Enter
![Page 55: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/55.jpg)
Why do we need it?
PEBCAC Bad Data
Validation
![Page 56: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/56.jpg)
Input Validation
![Page 57: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/57.jpg)
Seamless verification of data fields Check for mandatory fields Check for correct data formats Check for data ranges
Reduces incorrect data entered into your database
Prevent possible runtime exceptions
Why use Validations?
![Page 58: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/58.jpg)
Validation Types
ClientData
Show/view
Enter
Client Server
![Page 59: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/59.jpg)
Client-Side Validation
Validations are performed before the form is posted back Quick and responsive for the end user Insecure
Server-Side Validation
Validation checks are performed on the server Secure because these checks cannot be easily by passed Sometimes can be slow
Recommendation
Use both the types This approach provides the best of both worlds
Validation Types
ClientData
Show/view
Enter
Client Server
![Page 60: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/60.jpg)
RequiredFieldValidator
CompareValidator
RangeValidator
RegularExpressionValidator
CustomValidator
ValidationSummary
ASP.NET Validation Server Controls
![Page 61: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/61.jpg)
To put the Northwind database up on the web?
CRUD for all tables Pages for all tables Validation
So how long do you think it would take...
![Page 62: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/62.jpg)
1 line of code
5 Minutes
![Page 63: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/63.jpg)
Awesome starting point
Learn more from http://www.asp.net/dynamicdata/
ASP.NET Dynamic Data
![Page 64: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/64.jpg)
Extend what’s given to you and you can make really great websites
You can use it as a framework
![Page 65: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/65.jpg)
![Page 66: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/66.jpg)
Any issues ... questions
![Page 67: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/67.jpg)
Thank You!
Gateway Court Suite 10 81 - 91 Military Road Neutral Bay, Sydney NSW 2089 AUSTRALIA
ABN: 21 069 371 900
Phone: + 61 2 9953 3000 Fax: + 61 2 9953 3105
![Page 68: Work with data in ASP.NET](https://reader033.fdocuments.us/reader033/viewer/2022061221/54be78f14a7959af118b45c9/html5/thumbnails/68.jpg)
www.hookedonlinq.com
http://www.asp.net/get-started/
http://weblogs.asp.net/scottgu/
http://www.asp.net/dynamicdata/
If you have the next awesome web idea http://www.microsoft.com/BizSpark/
http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterLINQ.aspx
Resources