68
Developing Windows and Web Applications using Visual Studio.NET Eric Phan Solution Architect

Work with data in ASP.NET

Embed Size (px)

DESCRIPTION

Goal: Bringing data to the web and back :-) Old Style New Style DataBinding DataGrid Input Validation

Citation preview

Page 1: Work with data in ASP.NET

Developing Windows and Web Applications using Visual Studio.NET

Eric Phan

Solution Architect

Page 2: Work with data in ASP.NET

Homework?

Address Control

Credit Card Validation

Page 3: Work with data in ASP.NET

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

Recap

Page 5: Work with data in ASP.NET

ClientGet URL Request ServerResponse

1

2

Page 6: Work with data in ASP.NET

Client

Post requestwith data

ServerResponse

Page 7: Work with data in ASP.NET

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

Data in our database

Page 9: Work with data in ASP.NET

Data

Page 10: Work with data in ASP.NET

Bring database to the web

Data

Show/view

Enter

Page 11: Work with data in ASP.NET

OLD way

Page 12: Work with data in ASP.NET

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

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

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

WebForm Apps WinForm Apps

Objects/Entities

Data Sources

SQL Data XML DataOracle Data

Page 16: Work with data in ASP.NET

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

The new way!

LINQ 2 EntitiesLINQ 2 SQL

Page 18: Work with data in ASP.NET

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

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

var customers = from c in db.Customers

where c.FirstName.StartsWith(“A”)select c

SQL Syntax

Page 21: Work with data in ASP.NET

var customers = db.Customers.Where(c => c.FirstName.StartsWith(“A”);

Lambda Syntax

Page 22: Work with data in ASP.NET

Challenge, use this instead of SQL Server Management Studio for a month

LINQPad

Page 23: Work with data in ASP.NET
Page 24: Work with data in ASP.NET

See our Rules

http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterLINQ.aspx

Page 25: Work with data in ASP.NET

Databinding

Page 26: Work with data in ASP.NET

Bind the data

DataData source

Page 27: Work with data in ASP.NET

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

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

Databinding the GridView (designer)

Page 30: Work with data in ASP.NET

Databinding the GridView (designer)

Page 31: Work with data in ASP.NET

Databinding the GridView (designer)

Page 32: Work with data in ASP.NET

Databinding the GridView (designer)

Page 33: Work with data in ASP.NET

Databinding the GridView (designer)

Page 34: Work with data in ASP.NET

Databinding the GridView (running)

Page 35: Work with data in ASP.NET

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

Lots of built in features like:

SortingPagingInsertingUpdating DeletingSelecting

GridView

Page 37: Work with data in ASP.NET

Enable Delete, Insert, Update on the DataSource

Enabling these features

Page 38: Work with data in ASP.NET

Enabling these features

Enable them on the GridView

Page 39: Work with data in ASP.NET

Easy!

Page 40: Work with data in ASP.NET

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

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

Passing Parameters

Page 43: Work with data in ASP.NET

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

Add a Details View and create a new Datasource

1. Create a CustomerDetails.aspx

Page 45: Work with data in ASP.NET

2. Create a LINQ Data Source

Page 46: Work with data in ASP.NET

3. Specify the Where clause

Page 47: Work with data in ASP.NET

4. Make it read from the Query String

Page 48: Work with data in ASP.NET

5. Change the Select on the GridView to Link to the CustomerDetails Page

Page 49: Work with data in ASP.NET

6. Edit the templates

Page 50: Work with data in ASP.NET

7. Delete Select and Add a Hyperlink

Page 51: Work with data in ASP.NET

CustomerDetails.aspx?CustomerID={0}

8. Configure the Hyperlink

Page 52: Work with data in ASP.NET

Run it

Page 53: Work with data in ASP.NET

Validation – Don’t trust anyone

Page 54: Work with data in ASP.NET

Validation - Bring database to the web

Data

Show/view

Enter

Page 55: Work with data in ASP.NET

Why do we need it?

PEBCAC Bad Data

Validation

Page 56: Work with data in ASP.NET

Input Validation

Page 57: Work with data in ASP.NET

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

Validation Types

ClientData

Show/view

Enter

Client Server

Page 59: Work with data in ASP.NET

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

RequiredFieldValidator

CompareValidator

RangeValidator

RegularExpressionValidator

CustomValidator

ValidationSummary

ASP.NET Validation Server Controls

Page 61: Work with data in ASP.NET

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

1 line of code

5 Minutes

Page 63: Work with data in ASP.NET

Awesome starting point

Learn more from http://www.asp.net/dynamicdata/

ASP.NET Dynamic Data

Page 64: Work with data in ASP.NET

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
Page 67: Work with data in ASP.NET

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

[email protected]

Page 68: Work with data in ASP.NET

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