ITEC5620 - Advance Web Programming
Advance Web Programming
อ.ชไลเวท พิพฒพรรณวงศ์
ั
Chalaivate Pipatpannawong
Computer Instructor
Microsoft Certificate Professional
Web : www.9Expert.co.th ; Thaiwebdev.com
E-Mail: chalaivate@chalaivate.com
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ
Chalaivate Pipatpannawong
ITEC5620 - Advance Web Programming::
Current
Rangsit University instructor
Ramkhamhange Unversity instructor
Contact Info.
E-mail : chalaivate@9expert.co.th
MSN: chalaivate@hotmail.com
Mobile: 08-1850-3286
Web Site : http://www.chalaivate.com
Community : http://www.thaiwebdev.com
Microsoft Certify Professional (MCP)
2
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Course Outline
ITEC5620 - Advance Web Programming::
Review Web Programming
Review ASP.NET
Introduction to Web Services
XML and XML Schema for WSDL
Introducing SOAP
Consuming a Web Service
Implement an XML Web service consumer by using Microsoft Visual Studio .NET
Implement a simple XML Web service by using Visual Studio .NET.
Publish and deploy an XML Web service.
Web Services Enhancements 3.0
.NET Web Services Security
Deploying and Publishing an XML Web Service
XML Security
Implement WS-Security
Enhance Security with WS-Policy
3
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Course Outline
ITEC5620 - Advance Web Programming::
Design Patterns for Building Message-Oriented Web Services
Design Patterns for Building Service-Oriented Web Services
Addressing, Messaging, and Routing
Web Services Interoperability Issues
Designing for flexible computing
Service-Oriented Architecture
Benefits of Service-Oriented Architecture (SOA)
Advantages of contract-based design for interoperability
Creating and Consuming a Service
Building a service
Configuring the client with service details
Generating the client proxy and consuming the service
Individual Project
4
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
EVALUATION
ITEC5620 - Advance Web Programming::
Class
Attendance
10%
Individual
Final Project
40% 20%
Mid Term
30%
5
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Today’s Agenda
ITEC5620 - Advance Web Programming::
Review Web Programming
Review ASP.NET
Review ADO.NET
LAB
6
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Agenda
Introductionto OOP using C#
Intermediate C#
XML Web Service
What’s new in ASP.NET 2.0
Hand on Labs
7
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Intro to OOP using C#
Classes & Objects
Attributes
Methods
Objects in memory
Visibility
Properties (get & set)
Constructors
8
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Classes
ITEC5620 - Advance Web Programming::
Encapsulate data and functions
Enable more complex programming
Requires“Object think”
Security/reliability
Design by contract
9
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Defining a Class
ITEC5620 - Advance Web Programming::
class NAME {
ATTRIBUTES (i.e. data)
METHODS (i.e. functions)
}
10
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Class Example ITEC5620 - Advance Web Programming::
class BMW_Z4 {
int ModelYear;
string LicensePlate; Attributes (data)
bool TopUp;
void Drive()
{
Console.WriteLine(“Roadin’ and Rockin’“);
}
void OpenTop() Methods
{ (functions)
TopUp = false;
}
}
11
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Instantiating Classes
Definingthe class is akin to defining a type
An object is an instance of a class
Class:Object::Type:Variable
Use new to instantiate the class
12
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Instantiating Class & Using Object
ITEC5620 - Advance Web Programming::
BMW_Z4 myCar;
myCar = new BMW_Z4();
myCar.LicensePlate = "BMR4ME";
myCar.ModelYear = 2004;
myCar.Drive();
myCar.OpenTop();
myCar.Drive();
13
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Memory Trace
Dog d1, d2;
null
d1 = new Dog (5, “bob”);
d2 = new Dog (3, “ethel”);
d1 = d2;
14
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Memory Trace
ITEC5620 - Advance Web Programming::
(declare the variables; they are dead)
Dog d1, d2;
null
d1 = new Dog (5, “bob”);
d1 d2
d2 = new Dog (3, “ethel”);
d1 = d2;
15
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Memory Trace ITEC5620 - Advance Web Programming::
(bring d1 to life)
Dog d1, d2; null
d1 = new Dog (5, “bob”);
d1 d2
d2 = new Dog (3, “ethel”);
d1 = d2;
// Remember, new opens up
// space and …
16
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Memory Trace
ITEC5620 - Advance Web Programming::
(bring d1 to life)
Dog d1, d2;
d1 = new Dog (5, “bob”); null
d2 = new Dog (3, “ethel”); d1 d2
d1 = d2; rabid=false
weight=5
// Remember, new opens up name=“bob”
// space and calls the class
17
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Memory Trace
ITEC5620 - Advance Web Programming::
(bring d2 to life)
Dog d1, d2;
d1 = new Dog (5, “bob”); null
d2 = new Dog (3, “ethel”); d1 d2
d1 = d2; rabid=false
weight=5
// Remember, new opens up name=“bob”
// space and…
18
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Memory Trace
ITEC5620 - Advance Web Programming::
(bring d2 to life)
Dog d1, d2;
d1 = new Dog (5, “bob”); null
d2 = new Dog (3, “ethel”); d1 d2
d1 = d2; rabid=false rabid=false
weight=5 weight=3
// Remember, new opens up name=“bob” name=“ethel”
// space and calls the class
19
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Memory Trace
ITEC5620 - Advance Web Programming::
(comparison)
d1 and d2 do NOT occupy
null
the same space in memory,
so they are NOT == d1 d2
rabid=false rabid=false
Example: weight=5 weight=3
name=“bob” name=“ethel”
d1 == d2
evaluates to false 20
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Memory Trace
ITEC5620 - Advance Web Programming::
(have d1 point to d2)
Dog d1, d2;
d1 = new Dog (5, “bob”); null
d2 = new Dog (3, “ethel”); d1 d2
d1 = d2; rabid=false rabid=false
weight=5 weight=3
// See the result in next slide name=“bob” name=“ethel”
21
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Memory Trace
ITEC5620 - Advance Web Programming::
(have d1 point to d2)
Dog d1, d2;
d1 = new Dog (5, “bob”); null
d2 = new Dog (3, “ethel”); d1 d2
d1 = d2; rabid=false rabid=false
weight=5 weight=3
name=“bob” name=“ethel”
22
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Memory Trace
ITEC5620 - Advance Web Programming::
(have d1 point to d2)
Now, d1 and d2 are ==
null
d1 d2
But no one is pointing to this rabid=false rabid=false
weight=5 weight=3
name=“bob” name=“ethel”
Because no one is pointing to
it, we can no longer reference it.
23
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Visibility
OO motivation: protection/security
We need a way of selectively “publishing” parts
of a class and “hiding” other parts of the class
Public & private
24
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Visilibity Example
ITEC5620 - Advance Web Programming::
class BMW_Z4 {
private int ModelYear;
Note the visibility change
public string LicensePlate; will be
(most attributes
private)
private bool TopUp;
public void Drive()
{
Console.WriteLine(“Roadin’ and
Methods are
typically public
Rockin’“);
}
public void OpenTop()
25
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Object Method & Attribute Visibility
BMW_Z4 myCar;
myCar = new BMW_Z4();
myCar.LicensePlate = "BMR4ME";
myCar.ModelYear = 2004;
myCar.Drive(); Illegal b/c private
myCar.OpenTop();
myCar.Drive();
26
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Properties
ITEC5620 - Advance Web Programming::
Combines field/attribute with method
Standard:
Make attributes private
Lower-case first letter of attribute
Make properties public
Upper-case first letter of properties
Define “get” and “set” for each property (selectively)
27
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Properties Example
ITEC5620 - Advance Web Programming::
class BMW_Z4 {
private int modelYear; Attributes: Lower-case first
private string licensePlate;
letter of attribute
private bool topUp;
public int ModelYear
{ Properties:
Upper-case
get { return modelYear; } first letter of
properties
set { if (value >= 2003)
modelYear = value; }
}
28
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Constructors
ITEC5620 - Advance Web Programming::
So far, we’ve seen attributes and methods
Constructor is a unique method
Named same as the class name
Automatically called when class is instantiated
Useful for setting attributes & initializing the instance
No return type (not even void)
Must be public
Defaultconstructor is used unless you specify a
constructor
29
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Constructor Example
class BMW_Z4 {
private int modelYear;
private string licensePlate;
private bool topUp;
public BMW_Z4 ()
{
ModelYear = 2004;
TopUp = false;
LicensePlate = "DEALER";
}
30
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Intermediate C#
Parameter passing (ref, out, & value)
Keywords: static, const, read-only
Overloading
Inheritance
Polymorphism
31
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Parameter Passing
By default, parameters are passed by value
A copy of the value is passed
The original is not modifiable
Use “ref” to create by-reference parameter passing
A reference to the value is passed (akin to a pointer)
The original (actual) parameter is modifiable
Use “out” when data is strictly coming out of (and being
created in) the function
The original has no value
A new value will be placed into the parameter by the
32
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
By Value Parameter Example
float CalculateCost (float unit_cost,
int how_many)
{
return unit_cost * how_many;
}
static void Main() Even if you change
{ these locally, they won’t
change in Main.
...
line_cost = CalculateCost(uc, hm);
...
}
33
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
By Reference Parameter Example
ITEC5620 - Advance Web Programming::
void UpdateGrade (ref float grade, float
factor)
{
grade += factor;
}
static void Main()
{
...
for(i=0; i 4))
}
static void Main()
{
...
ReadChoice(c); Choice is generated in
... the module and then
} returned to Main
35
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Static
ITEC5620 - Advance Web Programming::
Each instance of a class (called an object) has a
copy of the attributes
Changing an attribute in one object doesn’t
affect the attribute of another object
But what if we want persistence (shared) among
all instances?
36
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Static Variables
ITEC5620 - Advance Web Programming::
void PrintNumbers ()
{
static int count = 0;
Console.WriteLine(count);
count++;
}
static void Main()
{
...
for(i=0; i MaxSpeed)
currentSpeed = MaxSpeed;
}
...
}
40
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Read-Only Fields
const only works if the initial value is known at
compile time.
The readonly keyword allows a field to be initialized
by the constructor, but not subsequently modified.
class Foo {
public readonly int numUsers;
Foo() {
numUsers = 10;
}
}
41
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Overloading
Overloading involves using the same
method/function name
Vary the number of parameters
Vary the type of parameters
Cannot just change return type (ambiguity in
invocation)
Useful to keep things simple
Squaring a number…
42
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Overloading Example
int Square (int i)
{
return i * i;
} Same function name
the different types of
parameters
float Square (float i)
{
return i * i;
}
static void Main()
{ 43
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Operator Overloading
class BMW_Z4
{
...
public BMW_Z4 ()
{
Initialize(0, 2004, false);
}
public BMW_Z4 (int my) Notice same method
{ name, different
Initialize(0, my, false); parameters
}
private void Initialize(int cs, int my, bool tu)
{
currentSpeed = cs; Place common
ModelYear = my; initialization in a
TopUp = tu; separate function
}
... 44
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Inheritance
ITEC5620 - Advance Web Programming::
Inheritance allows one class to take on the properties
of another
Super class-subclass relationship
Sometimes called parent-child relationship
Use the keyword extends to express this relationship
Subclass will “inherit” certain attributes and methods
Benefit: good design, reuse of code
45
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Class Hierarchy ITEC5620 - Advance Web Programming::
Mammal NOTE: Land-Mammal is
int weight a superclass to Dog, but
giveBirth( ) a subclass to Mammal
Land-Mammal
Answer: int numLegs
• Dog has three
attributes
• weight,
Dog Question: how many
numLegs, rabid
boolean rabid attributes does Dog have?
• Two from
inheritance, one it
declared itself
Chihuahua SheepDog
46
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Visibility and Inheritance
public ‟ allows all to see
private ‟ allows only class in which defined to see
protected ‟ allows class and all subclasses that inherit
to see
Consequently, we’ll now use protected instead of
private by default… (common)
47
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Class Access ITEC5620 - Advance Web Programming::
public Access not limited
Protected Access limited to the containing
class or types derived from the containing
class (i.e. subclasses)
internal Access limited to this program
protected internal Access limited to this program
or types derived from the
containing class
private Access limited to the containing type
48
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
C# Syntax Person
class Person
{
Student Professor
...
}
class Student : Person
{
...
}
Notice the use of “:”
class Professor : Person
{
...
}
49
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Polymorphism
Virtual methods must be declared as such or
they are statically bound at compile time.
The use of the virtual keyword differs from
C++.
virtual is used only in the base class.
Derived classes must use the override keyword.
50
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Polymorphism - contd.
public class Animal
{
virtual public bool canFly()
{ return false; }
}
public class Bird: Animal
{
override public bool canFly()
{ return true; }
}
public class Penguin: Bird
{
override public bool canFly()
{ return false; }
}
51
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
The Base Class: “Object”
ITEC5620 - Advance Web Programming::
Allclasses in C# inherit (sometimes
implicitly) from Object
Includes common set:
„ See next slide
Often useful to override (implement)
these virtual functions
“public override string ToString()…”
52
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Predefined Types
ITEC5620 - Advance Web Programming::
object Public Methods
public bool Equals(object)
protected void Finalize()
public int GetHashCode()
public System.Type GetType()
protected object
MemberwiseClone()
public void Object()
public string ToString()
53
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Abstract Classes
These work somewhat like they do in C++
A class with at least one abstract method can't
be instantiated.
Derivedclasses must implement the abstract
methods.
54
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Abstract Classes: Example ITEC5620 - Advance Web Programming::
abstract public class Shape { Shape
Shape
abstract public double area();
}
public class Rectangle: Shape { Rectangle Circle
private double sideX, sideY;
…
public override double area() {......}
}
public class Circle: Shape {
private double radius;
…
public override double area() {......}
}
------
Rectangle rec = new Rectangle(4.0, 3.5);
double aRec = rec.area(); //?
Circle cir = new Cirle(21.5);
double aCir = cir.area(); //?
55
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
More Features in C#
Boxing and Unboxing
ITEC5620 - Advance Web Programming::
int i = 123; i 123
object o = i; o System.Int32
123
int j = (int)o; j 123
Benefits of boxing
Enables polymorphism across all types
Collection classes work with all types
Eliminates need for wrapper classes
57
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Types: Enums
ITEC5620 - Advance Web Programming::
An enum defines a type name for a
related group of symbolic constants
Choices must be known at compile-time
Strongly typed
„ No implicit conversions to/from int
„ Can be explicitly converted
„ Operators: +, -, ++, --, &, |, ^, ~, …
Can specify underlying type
58
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Types: enum Example
ITEC5620 - Advance Web Programming::
enum Color: byte {
Red = 1,
Green = 2,
Blue = 4,
Black = 0,
White = Red | Green | Blue
}
Color c = Color.Black;
Console.WriteLine(c); // 0
Console.WriteLine(c.Format()); // Black
59
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Types: Enums
enums derive from System.Enum
All
Provides methods to
„ determine underlying type
„ test if a value is supported
„ initialize from string constant
„ retrieve all values in enum
„…
60
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Program Structure
ITEC5620 - Advance Web Programming::
Namespaces
Namespaces provide a way to
uniquely identify a type
Provides logical organization of types
Namespaces can span assemblies
Can nest namespaces
There is no relationship between
namespaces and file structure (unlike Java)
The fully qualified name of a type includes all
61
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Program Structure
Namespaces
namespace N1 { // N1
class C1 { // N1.C1
class C2 { // N1.C1.C2
}
}
namespace N2 { // N1.N2
class C2 { // N1.N2.C2
}
}
}
62
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Program Structure
ITEC5620 - Advance Web Programming::
Namespaces
The using statement lets you use types
without typing the fully qualified name
Can always use a fully qualified name
using N1;
C1 a; // The N1. is implicit
N1.C1 b; // Fully qualified name
C2 c; // Error! C2 is undefined
N1.N2.C2 d; // One of the C2 classes
C1.C2 e; // The other one
63
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Program Structure
ITEC5620 - Advance Web Programming::
Namespaces
The using statement also lets you create
aliases
using C1 = N1.N2.C1;
using N2 = N1.N2;
C1 a; // Refers to N1.N2.C1
N2.C1 b; // Refers to N1.N2.C1
64
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Program Structure
ITEC5620 - Advance Web Programming::
Namespaces
Best practice:
Put all of your types in a
unique namespace
Have a namespace for your company,
project, product, etc.
Look at how the .NET Framework
classes are organized
65
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Program Structure ITEC5620 - Advance Web Programming::
Main Method
Execution begins at the static Main()
method
Can have only one method with one of
the following signatures in an assembly
static void Main()
static int Main()
static void Main(string[] args)
static int Main(string[] args)
66
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
THE END
ITEC5620 - Advance Web Programming::
67
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming
Review ASP.NET using
Visual Studio.NET 2005
อ.ชไลเวท พิพฒพรรณวงศ์
ั
Chalaivate Pipatpannawong
Computer Instructor
Microsoft Certificate Professional
Web : www.9Expert.co.th ; Thaiwebdev.com
E-Mail: chalaivate@chalaivate.com
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ
Managing State
What is State Management?
ITEC5620 - Advance Web Programming::
Without State With State
Management Management
Login.aspx Login.aspx
Please enter your Please enter your logon
logon information: information:
First Name First Name
John John
Last Name Last Name
Chen Chen
Submit Submit Web Server
Web Server
Greetings.aspx Greetings.aspx
Hello
Hello John Chen
I forget who you
are!!
70
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Types of State Management
Server-Side State Client-Side State
Management Management
Application state Cookies
Information is available to all Text file stores information to
users of a Web application maintain state
Session state The ViewState property
Information is available only to Retains values between multiple
a user of a specific session requests for the same page
Database
Query strings
In some cases, use database
Information appended to the end
support to maintain state on
of a URL
your Web site
71
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Server-Side State Management
Application state is a global storage mechanism
accessible from all pages in the Web application
Session state is limited to the current browser
session
Values are preserved through the use of
application and session variables
Web Server
ClientScalability
Computer
Application and Session variables
ASP.NET session is identified by the SessionID
SessionID
string
72
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Client-Side State Management
Uses cookies to maintain state
Persistent cookies
Temporary/ Non-persistent cookies
Less reliable than server-side state management options
User can delete cookies
Less secure than server-side state management options
Limited amount of information Web Server
Client-side restrictions on file sizes
Client Computer
Cookies
73
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Initializing Application and Session
ITEC5620 - Advance Web Programming::
Variables
Variables are initialized in Global.asax
The Application object shares information among
all users of a Web application
Sub Application_Start(s As Object,e As EventArgs)
Application("NumberofVisitors") = 0
End Sub
protected void Application_Start(Object sender,EventArgs e)
{
Application["NumberofVisitors"] = 0;
}
74
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Using Application and Session Variables
Set session and application variables
Session("BackColor") = "blue"
Application.Lock()
Application("NumberOfVisitors") += 1
Application.UnLock()
Session["BackColor"] = "blue";
Application.Lock();
Application["NumberOfVisitors"] =
(int)Application["NumberOfVisitors"] + 1;
Application.UnLock();
Read session and application variables
strBgColor = Session("BackColor")
lblNbVisitor.Text = Application("NumberOfVisitors")
strBgColor = (string)Session["BackColor"];
lblNbVisitor.Text =
Application["NumberOfVisitors"].ToString();
75
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Application and Session Variable Duration
Session variables have a set duration after last
access
Default is 20 minutes
Sessionduration can be changed in
Web.config:
76
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Scalable Storage of Application and Session Variables
By default, the session state is managed in process
Disadvantage of in process storage:
Not Scalable
ASP.NET provides out of process storage of session
state
State can be stored
Web farm a server
in StateSQL Server database
or a state server Session and Application variables
-Or-
Advantages of out of process storage:
Scalable
SQL
Client
Session and Application variables
77
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Saving Application and Session Variables in a
Database the session state in Web.config
Configure
1
Mode is set to sqlserver or stateserver
Then, configure the SQL server
OSQL creates several stored procedures and temporary
2
databases for storing the variables
c:\> OSQL ‟S SQLServerName ‟E section of Web.config
Set cookieless = true
82
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Configuring, Optimizing,
and Deploying a Microsoft
ASP.NET Web Application
ITEC5620 - Advance Web Programming::
What Is the Cache Object?
An object used to store information
One Cache object per Web Application
An alternative to application variables
Not used to store information in session variables
Uses key-value pairs to store and retrieve items
Cache("myKey") = myValue
Cache["myKey"] = myValue;
84
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Advantages of Using the Cache
ITEC5620 - Advance Web Programming::
Object
Fasterthan creating a new object for each
request
Automatic cache resource management
Supports callback functions
Supports removal based on dependencies
85
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Use the Cache Object
ITEC5620 - Advance Web Programming::
Writing to the Cache object:
'Implicit method
Cache("myKey") = myValue
'Explicit method
Cache.Insert("myKey", myValue, Dependency,
AbsoluteExpiration, _
SlidingExpiration, CacheItemPriority,
CacheItemRemovedCallBack)
//Implicit method
Cache["myKey"] = myValue;
//Explicit method
Cache.Insert("myKey", myValue, Dependency,
AbsoluteExpiration,
SlidingExpiration, CacheItemPriority,
CacheItemRemovedCallBack);
86
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
How to Use the Cache Object
„ Retrieving values from the Cache
object:
myValue = Cache("myKey")
myValue = Cache["myKey"];
87
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Removing Items from the Cache Object
ITEC5620 - Advance Web Programming::
AbsoluteExpiration time
Cache.Insert(“myKey”, myValue,null,
DateTime.Now.AddMinute(5), null);
SlidingExpiration time
Cache.Insert(“myKey”, myValue, null,
null, TimeSpan.FromSeconds(20));
Dependent on a changed value
Cache.Insert(“myKey”, myValue,
new CacheDependency(Server.MapPath(“myDoc.xml”)));
88
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Removing Items from the Cache
Object(continued)
Cache item priority
Cache.Insert(“myKey”, myValue, null,
null, Cache.NoSlidingExpiration,
CacheItemPriority.High, onRemove);
89
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Use Page Output Caches
ITEC5620 - Advance Web Programming::
Cache content is generated from dynamic pages
Entire Web page is available in cache
Set cache duration in seconds
Set the VaryByParam property to control the
number of page variations in the cache
90
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
How to Use Fragment Caching
Convert the page fragment into a user control
Set the Duration and varyByParam properties
91
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Overview of Configuration Methods
Machine.config file
Machine-level settings
Web.config files
Application and directory-level settings
Both Machine.config and Web.config files are:
Well-formed XML
camelCase
92
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Storing and Retrieving Data in Web.config
Storing application settings in a Web.config
file
93
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Retrieving application settings from a
Web.config file
Dim strPubs As String = _
ConfigurationSettings.AppS
ettings("pubs")
AppSettingsReader App = new
AppSettingsReader();
string strPubs =
(string)App.GetValue("pubs",
typeof(string));
94
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Using Dynamic Properties
ITEC5620 - Advance Web Programming::
Store property values in
Web.config files rather
than in the application's
compiled code
Allows easy updates
without recompiling the
application
Enable and configure
through object properties
95
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Web Application Deployment
ITEC5620 - Advance Web Programming::
Copy files locally or FTP files remotely
Configure the target folder as a virtual directory
in IIS
Copy all necessary files, including the \bin
directory and content
No need to register components
96
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Preparing a Web Application for
ITEC5620 - Advance Web Programming::
Deployment
1. Build the Web application
2. Do not select unnecessary files
Visual Studio .NET solution files (.vbproj,
.vbproj.webinfo, .csproj, .csproj.webinfo, etc.)
Resource (.resx) files
Code-behind pages (.vb, .cs)
3. Copy or FTP necessary files to the production directory
97
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Updating Your Web Application
Copy or FTP files to update the Web application
Do not need to stop and restart IIS
.dll files can be updated while the site is still
running
Output cache protects existing users
98
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Consuming and Creating
XML Web Services
ITEC5620 - Advance Web Programming::
What is an XML Web Service?
Programmable logic accessible by standard
Web protocols
Allows applications to send and receive
information across the Internet
Language, protocol, and platform independent
Stateless architecture
Can be asynchronous
Based on an evolving W3C standard
100
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Why Use XML Web Services?
Northwind Traders Travel Site
Weather
Pick your destination: Redmond XML Web Service
The weather
Forecast calls for:
Rain
Internet Exchange Rate
The exchange rate is: $1.56 XML Web Service
We can fly you there for only:
$1,999.98 Airfare
XML Web Service
Airfare
Database
101
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Call an XML Web Service Using ITEC5620 - Advance Web Programming::
HTTP
1 Navigate to the XML Web service URL
2
Select and
XMLWeb
3
Servicemethod
4 Call the XML Web
service method
View the XML
response 102
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Using Proxies to Call XML Web Services
Appear the same as the original class, but do
not contain application logic
Use SOAP to interact with the XML Web Service
Created from the ServiceName.asmx.wsdl file
Add members to manage interactions with the
XML Web service and support asynchronous calls
Web
Form
Internet XML Web
Proxy Service
SOAP
103
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Use a Proxy to Call an XML Web ITEC5620 - Advance Web Programming::
Service
1.
1 Create a Web reference for the XML Web Service
2.
2
Create an instance of the XML Web Service
3.
3 Call the Web methods of the XML Web Service
4
4. Build the ASP.NET Web Application
private void Button1_Click(object sender, System.EventArgs e)
{
GetStocks.localhost.service1 ProxyGetStocks = new
GetStocks.localhost.Service1();
lblResults.Text = ProxyGetStocks.GetRating("Contoso");
}
104
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Create an XML Web Service
ITEC5620 - Advance Web Programming::
1.
1 Create a new XML Web Service project in Visual Studio
.NET
1.
2 Declare the WebMethod functions
1.
3 Build the XML Web Service project
1.
4
Test with
a browser
105
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
XML Web Service Code
ITEC5620 - Advance Web Programming::
.asmx page
using System;
.asmx.vb page
using System.Web.Services;
class Service1
{ [WebMethod] public type function1()
{
//function_here
}
}
106
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ADO.NET
ITEC5620 - Advance Web Programming::
What Is a Connected Environment?
A connected environment is one in which users are constantly
connected to a data source
Advantages:
Environment is easier to secure
Concurrency is more easily controlled
Data is more likely to be current than in other scenarios
Disadvantages:
Must have a constant network connection
Scalability
108
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
What Is a Disconnected Environment?
In a disconnected environment, a subset of data from
a central data store can be copied and modified
independently, and the changes merged back into the
central data store
Advantages
You can work at any time that is convenient for
you, and can connect to a data source at any
time to process requests ….continue Next Page
109
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Other users can use the connection
A disconnected environment improves the
scalability and performance of applications
Disadvantages
Data is not always up to date
Change conflicts can occur and must be
resolved
110
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
What Is ADO .NET?
ADO.NET is a set of classes for working
with data.
It provides:
An evolutionary, more flexible successor to
ADO
A system designed for disconnected
environments
A programming model with advanced XML
support
A set of classes, interfaces, structures, and
enumerations that manage data access from
within the .NET Framework
111
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
What Are the Data-Related
Namespaces?
The data-related namespaces include:
System.Data
System.Data.Common
System.Data.SqlClient
System.Data.OleDb
System.Data.SqlTypes
System.Xml
112
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
The ADO .NET Object Model
DataSet
SQL Server .NET OLE DB .NET
Data Provider Data Provider
SQL Server 7.0 OLEDB sources
(and later) (SQL Server 6.5)
113
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Using ADO.NET Classes in a Connected
ITEC5620 - Advance Web Programming::
Scenario
SqlDataReader In a connected
scenario, resources
SqlCommand are held on the server
until the connection is
SqlConnection
closed
1. Open connection
2. Execute command
SQL Server 7.0
(and later)
3. Process rows in
reader
114
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Using ADO.NET Classes in a Disconnected
Scenario
In a disconnected scenario,
DataSet
resources are not held on the
server while the data is
SqlDataAdapter
processed
SqlConnection 1. Open connection
2. Fill the DataSet
SQL Server 7.0 3. Close connection
(and later)
4. Process the DataSet
5.
115
Open connection
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Connecting to
Data Sources
What Are .NET Data Providers?
ITEC5620 - Advance Web Programming::
Definition
A .NET data provider is a set of classes
that you use to connect to a data
source, and retrieve and update data
Typesof .NET data providers
SQL Server .NET Data Provider
OLE DB .NET Data Provider
117
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
The .NET Data Provider Classes
ITEC5620 - Advance Web Programming::
XxxConnection ‟ for example,
SqlConnection
„ XxxTransaction ‟ for example, SqlTransaction
„ XxxException ‟ for example, SqlException
„ XxxError ‟ for example, SqlError
XxxCommand ‟ for example, SqlCommand
„ XxxParameter ‟ for example, SqlParameter
118
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
The .NET Data Provider Classes
XxxDataReader ‟ for example, SqlDataReader
XxxDataAdapter ‟ for example, SqlDataAdapter
XxxPermission ‟ for example, SqlClientPermission
119
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Which .NET Data Provider Should
ITEC5620 - Advance Web Programming::
You Use?
SQL Server .NET Data Provider
SQL Server version 7.0 or later
OLE DB .NET Data Provider
SQL Server 6.5, Microsoft Access, Oracle, other
data sources with OLE DB providers
120
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Database Security
Using SQL Server security
Windows Authentication
Mixed Mode (Windows Authentication and SQL
Server authentication)
121
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
What Is a Connection String?
A connection string defines the parameters required to make a
connection to a data source
Connection string parameters
Provider (OLE DB only)
Data Source
Initial Catalog
Integrated Security
User ID/Password
122
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Set a Connection String
ITEC5620 - Advance Web Programming::
You can set the ConnectionString property only when the connection
is closed
To reset a connection string, you must close and reopen the
connection
Microsoft Access connection
OleDb.OleDbConnection cnNorthwind =
new OleDb.OleDbConnection();
cnNorthwind.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data
Source=\Samples\Northwind.mdb;“;
123
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Opening and Closing a Connection
Opening and closing connections explicitly
Open and Close methods
Opening and closing connections implicitly
Data adapters can open and close connections
automatically when needed
124
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
How to Handle SqlExceptions
Write the code to execute inside a Try block
Write a Catch statement for each specific exception that you want to
catch
System.Data.SqlClient.SqlException
Errors collection
SqlError properties (Class, Number)
Write a generic Catch statement for all other exceptions
Write a Finally statement to run the code no matter what happens
125
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Performing Connected
Database Operations
Object Model for Connected
ITEC5620 - Advance Web Programming::
Applications
Classes in a Connected Application
Data Source
XxxDataReader
XxxCommand XxxConnection
XmlReader
XxxParameter
XxxParameter
XxxParameter
127
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
What Is a Command Object?
ITEC5620 - Advance Web Programming::
A command object is a reference to a SQL
statement or stored procedure
Properties
(Name), Connection, CommandType,
CommandText,
Parameters
Methods
ExecuteScalar, ExecuteReader, ExecuteNonQuery
128
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
How to Create a Stored Procedure
Server Explorer
On the View menu, click Server Explorer, or
press Ctrl+Alt+S
Create a data connection
Click New Stored Procedure
Insert SQL
129
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
How to Create a Command Object
Programmatically
Server Explorer
On the View menu, click Server Explorer, or press
Ctrl+Alt+S
Drag stored procedure onto form or component
Toolbox
Use SqlConnection or OleDbConnection
Use SqlCommand or OleDbCommand
130
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
What Are Command Parameters?
Introduction
SQL statements and stored procedures can have
input and output parameters, and a return value
Command parameters allow these parameters to be
set and retrieved
SqlParameter, OleDbParameter
Properties
ParameterName, DbType, Size, Direction
131
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Create Parameters for a ITEC5620 - Advance Web Programming::
Command Object
Programmatically
Code example
SqlParameter prmName = new SqlParameter
("@CatName", SqlDbType.NChar, 15);
prmName.Direction =
ParameterDirection.Output;
cmdCountProds.Parameters.Add(prmName)
Using the Visual Studio .NET graphical tools
132
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Why Return a Single Value in a
ITEC5620 - Advance Web Programming::
Command?
ADO.NET is more efficient than ADO, where a
complete record set is returned
Examples
Units in stock for a particular product
How many products?
COUNT, MAX, MIN, AVERAGE
133
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Execute a Command That
ITEC5620 - Advance Web Programming::
Returns a Single Value
Call the ExecuteScalar method
ExecuteScalar returns a value of the type Object
Use CType or a cast, to convert into appropriate type
Microsoft Visual Basic® code example:
cmProducts.Parameters("@ProdID").Value =
42;
cnNorthwind.Open();
int qty = (int)cmProducts.ExecuteScalar();
cnNorthwind.Close();
134
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
How to Retrieve Output and Return Values
How to get output parameters from a
command
cmProducts.Parameters("@CatName").Value;
How to get the return value from a stored
procedure
cmProducts.Parameters("@RETURN_VALUE").Value
135
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Returning Rows
ITEC5620 - Advance Web Programming::
DataReader
Read-only, forward-only, stream of rows
The ExecuteReader method
Returns a DataReader
For example, SqlDataReader, OleDbDataReader
136
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
DataReader Properties and Methods
Read method
Loads the next row
Returns true if a row exists, false if at end of rows
GetXxx methods ‟ for example, GetString, GetInt32
GetValues method
IsDbNull method
Close method
137
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
How to Use a DataReader to Process Rows
Usinga DataReader object to process a result set
Code example
SqlCommand cmProducts = New SqlCommand( _
"SELECT ProductName, UnitsInStock " & _
"FROM Products", cnNorthwind);
cnNorthwind.Open();
SqlDataReader rdrProducts;
rdrProducts = cmProducts.ExecuteReader();
while (rdrProducts.Read())
{
ListBox1.Items.Add(rdrProducts.GetString(0))
}
rdrProducts.Close();
138
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
What Are DDL and DCL Statements?
Definition
Automate database administration tasks
DDL and DCL statements
CREATE, ALTER, DROP, GRANT, DENY,
REVOKE
Code example
CREATE PROCEDURE
dbo.SummarizeProducts AS
CREATE TABLE ProductSummary
( ProductName nvarchar(40),
CategoryName nvarchar(15) )
139
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Execute DDL and DCL
ITEC5620 - Advance Web Programming::
Statements
ExecuteNonQuery method
Returns count of rows affected
Code example
cnNorthwind.Open();
int affected = _
cmSummarizeProducts.ExecuteNonQuery();
cnNorthwind.Close();
MessageBox.Show("Records affected: " +
affected);
140
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
What Are DML Modification
ITEC5620 - Advance Web Programming::
Statements?
Definition
Modify data in the database
DML Statements
INSERT, UPDATE, DELETE
Code example
CREATE PROCEDURE dbo.InsertRegion
( @RegID int, @RegName nchar(50) )
ASINSERT INTO Region VALUES
(@RegID, @RegName)
141
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Execute DML Modification
ITEC5620 - Advance Web Programming::
Statements
To execute a DML statement
ExecuteNonQuery method
Code example
cnNorthwind.Open();
int affected = cmPrices.ExecuteNonQuery();
cnNorthwind.Close();
MessageBox.Show("Records affected: " & _
affected);
142
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
Building DataSets
Disconnected Architecture
ITEC5620 - Advance Web Programming::
SQL Server 2000 SQL Server 6.5
Categories Products Customers Orders
SqlDataAdapter OleDbDataAdapter
DataSet Categories Products Employees Customers Orders
XML XmlDataDocument
File
XML Web
service
144
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
What Are DataSets, DataTables, and
DataColumns?
DataSet
DataTable
DataTable
Connecti Stored
on Procedur
Database e
Server Data Store
145
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
The DataSet Object Model
ITEC5620 - Advance Web Programming::
Common collections
Tables (collection of DataTable objects)
Relations (collection of DataRelation objects)
Data binding to Web and Windows controls
supported
Schema can be defined programmatically or using
XSD DataTable DataColumn
DataRow
DataRelation Constraints
146
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Create a DataSet, a
ITEC5620 - Advance Web Programming::
DataTable, and a DataColumn
Creating a DataSet
Drag and drop a DataSet control from the Toolbox
Creating a DataTable
Edit the Tables collection of a DataSet by using the
Properties window
Creating a DataColumn and adding it to a DataTable
Edit the Columns collection of a DataTable by using
the Properties window
147
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
Creating Custom Expressions
Definition
Custom expressions are columns derived from
calculations, rather than stored values
Using the DataColumn Expression property
Sum([Unit Price] * [Quantity])
Aggregate functions can use parent/child relationships
Avg, Count, Sum, Max, Min
148
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Bind Data to a Windows
ITEC5620 - Advance Web Programming::
Control
Simple data binding
Binding to a simple control that can only
display a single value ‟ for example, a text
box
Complex data binding
Binding to a more complex control that can
display multiple values ‟ for example, a data
grid
149
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
How to Bind a DataSet to a DataGrid
DataSourceproperty
Can be a DataSet, DataTable, or DataView
DataMember
Can be a DataTable if the DataSource is a
DataSet
150
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
How to Save and Open a DataSet
Two DataSet methods
ReadXml
WriteXml
Pass the path and filename as a string parameter
ReadXml raises an exception if the file does not exist
WriteXml overwrites existing files
151
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
What Is a DataView Object?
DataSet
DataTable
DataTable
Connecti Stored
DataView on Procedur
Database e
Windows and Web controls
Server Data Store
152
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Define a DataView
ITEC5620 - Advance Web Programming::
Creating a DataView by using form controls
Creating a DataView programmatically
DataView dvProducts = New
DataView(dsNorthwind.Tables("Products")
);
dvProducts.Sort = "UnitPrice“;
dvProducts.RowFilter = "CategoryID > 4“;
grdProducts.DataSource = dvProducts;
dvProducts.Table = dsNorthwind.Tables("Products") ;
Applying a DataView to a DataTable
153
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
What Is a DataAdapter?
DataSet
DataAdapter Data source
DataTable
Fill
Update
DataTable
DataAdapter
Fill
Update
154
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
The XxxDataAdapter Object Model
ITEC5620 - Advance Web Programming::
XxxDataAdapter
SelectCommand UpdateCommand InsertCommand DeleteCommand
XxxDataReader
XxxCommand XxxCommand XxxCommand XxxCommand
XxxConnection
sp_SELECT sp_UPDATE sp_INSERT sp_DELETE
155
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
DataAdapter Properties and
ITEC5620 - Advance Web Programming::
Methods
DataAdapter properties
SelectCommand
InsertCommand
UpdateCommand
DeleteCommand
Methods used by DataAdapters
Fill
Update
156
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Create a DataAdapter That Uses
ITEC5620 - Advance Web Programming::
a New SELECT Statement
You can create a DataAdapter to execute a new SELECT
statement
Read-only data access for disconnected applications
Two ways to create the DataAdapter
Use the Data Adapter Configuration Wizard
Write the code yourself
You must specify
A new or existing connection
The SELECT statement for the query
157
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Create a Data Adapter That Uses ITEC5620 - Advance Web Programming::
an Existing Stored Procedure
You can create a DataAdapter programmatically to execute an
existing stored procedure by:
Specifying a stored procedure for SelectCommand
Specifying stored procedures for InsertCommand,
UpdateCommand, and DeleteCommand if required
Create the DataAdapter by using the wizard, or in code
You must specify:
A new or existing connection
The stored procedure(s)
158
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
How to Fill a Dataset Efficiently
Define an explicit schema before you fill the DataSet
DataTables, DataColumns, and DataRelations
are known before the data is loaded
Enables the data to be loaded more efficiently
Visual C# Example
159
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
How to Create a Data Adapter That Uses an
ITEC5620 - Advance Web Programming::
Existing Stored Procedure
To define an explicit DataSet schema
Create a typed DataSet class:
dsCustomers.Customers.BeginLoadData();
daCustomers.Fill(dsCustomers.Customers);
dsCustomers.Customers.EndLoadData();
DataGrid1.DataSource =
dsCustomers.Customers.DefaultView;
Or, create the DataTables, DataColumns, and
DataRelations programmatically
160
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร
ITEC5620 - Advance Web Programming::
How to Fill a DataSet from Multiple DataAdapters
You can use multiple DataAdapters to fill a
DataSet
Each DataAdapter fills a separate table in
the DataSet
Callthe Fill method on each DataAdapter
daCustomers.Fill
Specify the table to fill in the DataSet
(dsCustomerOrders.Customers);
daOrders.Fill(dsCustomerOrders.Orders);
DataGrid1.DataSource = dsCustomerOrders.Customers;
161
ปริญญาโท สาขาวิชา เทคโนโลยีสารสนเทศ มหาวิทยาลัยเทคโนโลยีมหานคร