web/database connectivity with asp.net david henson [email protected]

103
Web/Database Connectivity with ASP.NET David Henson [email protected]

Upload: dorcas-richard

Post on 23-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Web/Database Connectivity with ASP.NET

David Henson

[email protected]

Page 2: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Class Logistics

• Class Hours

• Classroom Setup

• Class Format

• https://www.certifiednetworks.com

Page 3: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Course Outline

Module 1 - Web Overview

Module 2 - ASP.NET Overview

Module 3 - ASP.NET Webform

Module 4 – Debugging/Tracing

Module 5 - .NET Controls

Module 6 – ADO.NET

Page 4: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Course Outline, Contd.

Module 7 – Advanced Data Topics

Module 8 - Security

Module 9 – ASP.NET Email/File Integration

Module 10 – XML and Web Services

Page 5: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Module 1 – Web Overview

Page 6: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Definitions

• ASP• ASP.NET• HTTP• IIS• Virtual Directory• Browser• MSIL• CLR

• HTML

Page 7: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Web Server/Client Interaction

• Disconnected Paradigm

• “Cookies” used to emulate state

Page 8: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

HTTP Protocol

• Header

• Body

Page 9: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Lab 1A – Viewing HTTP Traffic

• Things to look for:– Header– Body– Server Response Code

Page 10: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Methods For Dynamic Server-Based Content

• CGI – Opens separate executable

• ASP – Interpreted each page load, Windows Only

• JSP – Portable, fast familiar to Java Programmers

• PHP – Cross Platform, C & Perl Like

• ASP.NET – Compiled, integrated into framework

Page 11: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

HTML Protocol

• End result of any dynamic server side product- dictates rendering of page

• Tags:<html><body><table><tr><td><form><a href=“…”><br><input type=“…”>

Page 12: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Lab 01B – Creating an E-Commerce Website Framework

Page 13: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Module 2 – ASP.NET Overview

Page 14: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

.NET Framework

Page 15: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

What is ASP.NET

• Not a programming language but a delivery mechanism

• Can leverage any .NET programming language, like built-in VB.NET, C#, and third party like Delphi

Page 16: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

.NET Features

• Multiple Language Support• Increased Performance

– Compiled code– Cache

• Server Controls• Web Services• Improved Security• Greater Scalability• Cookie-less Sessions• Easy Configuration and Deployment

Page 17: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Classic ASP Programming Model

• Procedural, Top to Bottom Approach

• HTML/ASP Code Mixed

Page 18: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Classic ASP Example

Page 19: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Classic Windows Programming Model

• Create a Form

• Drop Controls On Form

• Write Event Handlers

• User activity generates messages which are handled by the event handlers

Page 20: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Demonstration – Spy++

• What to look for:– Event Driven

Model of

Windows

Page 21: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

ASP.NET Programming Model

• Process Same as Windows Programming

• Connectivity/State is emulated

Page 22: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

ASP.NET Example

Page 23: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

ASP.NET Configuration Files

• Web.config

• IIS Settings

• Application Defined

Page 24: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Solution Files

• Solution contains 1 or more projects

• .sln file created in: My documents\Visual Studio Projects\*.*

• To Change Defaults: Tools/Options/Environment/Projects & Solutions

• New Solution Created With New Project

Page 25: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Project Files

• Project maps to web application

• Web Application Created in: c:\inetpub\wwwroot\projectname

• Project Configuration File: (.vbproj or .csproj)

• New Virtual Directory Created W/ Project

• Each Application can have its own web.config

Page 26: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Other Files

• Webforms: .aspx• Web Services: .asmx• Styles.cssIIS Prevents Execution of Pre-

Defined Extensions• Code Behind Files: .vb & .cs• Discovery Files: .disco, .vsdisco• ASP.NET Application File:

Global.asax• Resource Files: .resx• Web.Config

Page 27: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Demonstration – Visual Studio .NET Quick Tour

Page 28: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Lab 2A – ASP.NET Redirection

Page 29: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Module 3 – ASP.NET Webforms

Page 30: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Webforms Defined

• aspx extension• @Page Directive

• Framework Is an Object Model• Denoted by the runat="server" Attribute

• Contain Client-side and Server-side Code • Contain HTML and .NET Server Controls

<%@ Page Language="vb" %> <%@ Page Language="vb" %>

<Form runat="server"></Form>

<Form runat="server"></Form>

Page 31: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Events

• User requests dbdemo.aspx

• Page_Load event detected

• Sub Page_Load() executed

• All event handlers are called in a single flow of motion, before the user ever gets to see the page.

Page 32: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

WebForm Main Events

• Events Occur Each Time the Page is Accessed

• Page_Init

• Page_Load

• Controls(only when form is posted back…Button1_Click for example)

• Page_Unload

Page 33: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Other Supported Webform Events

• Error – whenever the page generates one

• CommitTransaction

• AbortTransaction

Page 34: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Defining Server Control Event Handlers

<asp:button id=“Button1” onclick=“MyHandler”/> <asp:button id=“Button1” onclick=“MyHandler”/>

You may also double-click the control in design view to get into the “default” event handler for that control with a pre-defined event handler name.

Page 35: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Webform Validation

• Page.IsValid will be false if any control has failed validation

• Asp:ValidationSummary Control• Set Display property of any validation control to None• ValidationSummary Control will display msg from all controls

• Client Side Validation Setting:Set BaseValidator.EnableClientSideScript for any contol

Page 36: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Manual Validation

• Add form submit button with CausesValidation set to False, then call Page.Validate yourself.

• Take action based upon page.IsValid property

Page 37: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Lab 03A – Working with Web Forms

Page 38: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Module 4 – Debugging/Tracing

• Debugging

• Tracing

• Error/Exception Handling

Page 39: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

To Compile in Debug Mode

• 1. Add a "Debug=true" directive at the top of the file that generated the error. Example:

  <%@ Page Language="C#" Debug="true" %>

or:

2) Add the following section to the configuration file of your application:

<configuration>   <system.web>       <compilation debug="true"/>   </system.web></configuration>

Page 40: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Using the Debugger

• Setting Breakpoints

• Debug shortcut keys:

Page 41: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Logging Exceptions

Try …some codeCatch err AS Exception Dim log as New EventLog() Log.Source = “MyPage” Log.WriteEntry(err.Message, EventLogEntryType.Error)End Try

Page 42: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Viewing Eventlogs

Page 43: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Display of Errors

• Error Modes:– RemoteOnly – Default, hides details if not

local client– Off – Shows all details and source…good for

development, not production– On – Displays custom error page if any, or

generic error message

Page 44: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Custom Error Pages

<configuration>

<system.web><customerErrors defaultRedirect=“MyError.aspx”>

<error statusCode=“404” redirect=“My404.aspx”>

</customErrors>

</system.web>

</configuration>

Only works if ASP.NET is handling the request(.aspx extension)

Page 45: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Lab 04A – Debugging/Tracing

• Enabling Tracing

• Exception Handling

• Debugging

• Writing to the Windows Eventlog

• Custom Error Messages

Page 46: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Module 5 – .NET Controls

Page 47: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

The ASP.NET Server Controls

• HTML Server Controls

• ASP.NET Web Form Controls

• ASP.NET List Controls

• ASP.NET Templated Controls

• ASP.NET Rich Controls

• ASP.NET Validation Controls

• ASP.NET Mobile Controls

Page 48: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

User Controls

• .ascx file

• Replaces the #include file from ASP

• Provides re-usuable code within your application

• Contained within a Webform

Page 49: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Custom Server Controls

• Based on System.Web.UI.Control

• Generates its own HTML (unlike a business logic assembly)

Page 50: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Key Concept - Rendering

• Controls are responsible for rendering themselves

• You can override the Render() function on your own controls

Page 51: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

HTML Controls

• Best for “Converting” from ASP to ASP.NET

• Add the runat=“server” attribute to existing controls

Page 52: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

ASP.NET Controls

• Mirror HTML Controls

• More Consistent Properties/Naming Across Control Types

Page 53: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Common ASP.NET Webform Control Properties

• ID

• Text

Page 54: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Control Validation

Page 55: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Data Binding Controls

• Easily bound to a datasource (database, file, XML doc)

• Default HTML easily modified

Page 56: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Templated Controls

• You supply the HTML

Page 57: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Lab 05A – Working with .NET Controls

Page 58: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Module 6 – ADO.NET

Page 59: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Components of ADO.NET

• SqlDataAdapter

• SqlDataSet

• SqlDataTable

• SqlDataReader

• SqlCommand

Page 60: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Required Namespaces

• Namespace Defined• Two In Use This Class:

– System.Data – Provider Independent Like DataSet

– System.Data.SqlClient – Provider Dependent Like SqlConnection

• ASP.NET Syntax:<%@ Import Namespace=“System.Data” %>

Page 61: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Providers

• Providers Available:– SQL Server .NET Provider– OleDB .NET Provider

• Example-AllRecordsBasicOleDB.aspx

– ODBC .NET Provider– SQL XML .NET Provider

Page 62: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Connections

• Connection Defined

• Where to Store the Connection String

• Connection Syntax Examples

• Connection Pooling

• Security

• Close Your Connections!

• Monitoring Connections

Page 63: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Where to Store the Connection String

• Options Available:– Front End App (.aspx file)– Web.Config– UDL File (OleDB Only)– Registry– Custom File– COM+ Catalog Using Connection Strings

• Evaluation Terms: Security, Convenience, Performance

Page 64: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Two Connection String Syntax Examples

• In the .aspx file:ConnString = “server=10.0.0.1;UID=sa;PWD=;”Dim Conn As New SqlConnection(ConnString)

• In Web.Config XML file:<configuration> <appSettings> <add key=“ConnString” value=“server=10.0.0.1;UID=sa;PWD=;”/>

</appSettings></configuration>

Page 65: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Connection Pooling

• Defined• Controlling Min/Max-

Example6ShowConnectionStatePoolControl.aspx

• Importance of “Exact String Match”

• Pooling for SqlClient vs. OleDBClient

• Effects of pooling on SQL security

• Close Your Connections!

Page 66: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Performance Issues

• Choose Providers Wisely• DataReader vs. DataAdapter• Repeater Control vs. DataGrid Control• Connection Pooling• Embedding SQL vs. Stored Procedures• Controlling The HTML• Typed Accessor Methods-

Example7AdapterVsReaderUsingTypedAccessorMethods.asp

Page 67: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

DataReader Vs. DataAdapter

• DataReader– Forward Only– Only One Record At A Time In Memory– “Firehose” Functionality– Typed Accessor Methods Avoid Conversions

• DataAdapter– More Overhead– More Flexible

Page 68: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Repeater Control vs. DataGrid(or DataList) Control

• Repeat Control Simply Repeats – Low overhead– You Have To Do Everything– You Can Do It Better Than Microsoft Did!

• DataGrid – Default HTML Behaviour– Higher Overhead, Most Functionality

Page 69: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Embedding SQL vs. Stored Procedures

• Stored Proc Advantages:– Procedure Cache– Separate Security Model– Potentially Less Network Traffic– Output Params, Error Code & Result Set– Can Do Anything Server Side– Abstracts the Front End from Changes –

Possible Disadvantage with Xcopy Deployment

Page 70: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Controlling the HTML

• Use Stylesheets if Possible!

• Make Sure The Page Fails Gracefully If Needed

• With DataGrids, Use TemplateColumns

Page 71: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Final Recommendations

• Use DataGrids Only When Updates Are Needed

• Embed Connection In Code Behind File

• Only “Select” What You Need

• Call StoredProcs For Ultimate Performance When “Paging”

Page 73: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Comparing ADO/ADO.NET

Page 74: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Lab 6A – Exploring ADO.NET

Page 75: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Module 7 – Advanced Database Topics

• Paging

• Inserts/Updates/Deletes

• Using Stored Procedures

• Dynamic SQL Server HTML Generation

• Index Tuning Wizard

Page 76: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Paging

• Methods for paging:– Data Grid– Manual– Stored Procedures

Page 77: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Inserts

INSERT table1 VALUES(‘Smith’,’Joe’,)

Page 78: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Updates

UPDATE Table1

SET Unitprice = Unitprice * 1.1

Page 79: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Deletes

DELETE Table1

WHERE CustomerID = 10

Page 80: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Using Stored Procedures

CREATE PROC FinalSales

@Region int

AS

BEGIN

SELECT * FROM sales

WHERE RegionID = @Region

END

Page 81: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Dynamic HTML Generation

• Triggers are procedures that fire on insert, update or delete

• Through triggers, you can create HTML files when data changes

Page 82: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Index Tuning Wizard

• Used to optimized SQL Server through indexing

Page 83: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Lab 7A – Advanced Database Topics

• Add Paging to a Data Grid

• Controlling Output-Templated Repeater

• Using Stored Procedures

Page 84: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Module 8 – Security

Page 85: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

IIS Security Configuration

Page 86: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

ASP.Net Security Related Configuration

Page 87: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Authentication/Encryption

Page 88: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Setup of SSL

Page 89: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Lab 8A – Setup of SSL

Page 90: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Module 9 – ASP.NET Email and File Integration

Page 91: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

SMTP Protocol

Page 92: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Sending Email

• SMTP is now built into .NET

System.Web.Mail.SmtpMail.Send(

from,

to,

subject,

body

)

Page 93: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Reading/Writing Files

Dim w As IO.StreamWriter

w = IO.File.CreateText("C:\somefile.txt")

w.WriteLine(Request("textbox1"))

w.Close()

Page 94: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Lab 9A – Email and Files

Page 95: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Module 10 – XML

Page 96: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

What is XML?

Page 97: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

XML Document

• Well formed if:Doc has a root element

Every tag has an end(case sensitive)

No special characters are used

• Valid if:Doc format agrees with specification

Page 98: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

XML Document Example

Page 99: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Formatting Issues

• Use .css to separate data from the formatting

• <?xml:stylesheet href="test.css" type="text/css" ?>

Page 100: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Web Services

• .asmx file

• Inherits from System.Web.Services.WebService

• Web Service method marked with the WebMethodAttribute

• Allows client to discover its methods

• Interacts w/ client through SOAP

Page 101: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Web Service Example

• TimeServer web service returns the current time

Page 102: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Implementation

• Plumbing is built into the classes…you focus on the business needs

• Web Service consumer adds the web reference, then uses the class like any other

Page 103: Web/Database Connectivity with ASP.NET David Henson dhenson@certifiednetworks.com

Lab 10A – XML