/learn Table of Contents
ASP Quick Lessons - Table of Contents (/learn/index.asp) - Page 1
Credits (/learn/credits.asp) - Page 2
Core Ideas (/learn/core.asp) - Page 3
What is ASP? Obtaining The Software (/learn/whatis.asp) - Page 4
AspInstall listserver (/learn/aspinstall.asp) - Page 5
Simple ASP Page, Server Scripting (/learn/whatisexample.asp) - Page 6
MS Online Documentation (/learn/docs.asp) - Page 7
Response: Basics (/learn/res.asp) - Page 8
Response: Buffers, Redirect (/learn/res2.asp) - Page 9
Response: Redirection (/learn/res3.asp) - Page 10
Response: Quotes & Special Characters (/learn/res4.asp) - Page 11
Response: Encoding URLs, HTML (/learn/res5.asp) - Page 12
Include: Basics (/learn/inc.asp) - Page 13
Include: Dynamic FileName (/learn/includedynamic.asp) - Page 14
Includes: Other Sites, Dynamic FileNames (/learn/includeasphttp.asp) - Page 15
Include/Redirects: New Win2k Commands (/learn/incwin2k.asp) - Page 16
Include: Books Sample Exercise (/learn/booksample.asp) - Page 17
More Book Sample Exercises (/learn/booksample2.asp) - Page 18
Format: Numbers #1 (/learn/formatnumbers.asp) - Page 19
Format: Numbers #2 (/learn/formatnumbers2.asp) - Page 20
Format: Dates #1 (/learn/formatdates.asp) - Page 21
Date/Time on ASP Pages by Tony Arguelles (/learn/datetime.asp) - Page 22
Loops: DO WHILE/UNTIL #1 (/learn/DoLoop.asp) - Page 23
Loops: Timeouts #2 (/learn/DoLoop2.asp) - Page 24
Loops: Intercepting Timeouts #3 (/learn/DoLoop3.asp) - Page 25
Server Variables: Popular Ones (/learn/server.asp) - Page 26
Server Variables: Domain/Host Name (/learn/server2.asp) - Page 27
Server Variables: Displaying All (/learn/serverall.asp) - Page 28
Random Content/Rotating Info (/learn/randomadvice.asp) - Page 29
Browscap: Basics (/learn/bc.asp) - Page 30
Browscap: Intricate Details (/learn/bcdetails.asp) - Page 31
Listserver for Browser Problems (/learn/aspbrowserheck.asp) - Page 32
State Management (/learn/statemanagement.asp) - Page 33
State Management Introduction (/learn/stateintro.asp) - Page 34
What are ASP Sessions? (/learn/sessionswhat.asp) - Page 35
Application Data (/learn/sessionsapps.asp) - Page 36
Application Data: Worlds Fastest ListBox (/learn/speedappdata.asp) - Page 37
XML, Database Caches - Fast Retrieval (/learn/xmlfastlist.asp) - Page 38
Say No To Databases w/Sessions or Application scope (/learn/nodbsession.asp) - Page 39
Session Overview & Myths (/learn/sessionoverview.asp) - Page 40
Sessions: Global.asa and Scalability (/learn/globalproblems.asp) - Page 41
Sessions: Global.asa Events (/learn/global.asp) - Page 42
Global.asa, Sessions, Custom Stats Resources (/learn/statemore.asp) - Page 43
State Methods: Pros and Cons (/learn/stateproscons.asp) - Page 44
Pass Data w/Hidden Fields (/learn/hidden.asp) - Page 45
Pass Data w/Cookies (/learn/cookies.asp) - Page 46
Pass Data w/Session Vars (/learn/statesessions.asp) - Page 47
Pass Data w/ID tied to database (/learn/statedb.asp) - Page 48
[aspStateManagement] Listserver (/learn/aspstatemanagement.asp) - Page 49
Forms/Decisions (/learn/Form.asp) - Page 50
Forms: Introduction (/learn/formintro.asp) - Page 51
Forms: Text Box (/learn/formtextbox.asp) - Page 52
Forms: Text Area (/learn/formtextarea.asp) - Page 53
Forms: Check Box (/learn/formcheckbox.asp) - Page 54
Forms: Radio Buttons (/learn/formradio.asp) - Page 55
Forms: List Box (/learn/formlistbox.asp) - Page 56
Forms: CASE syntax #1 (/learn/case.asp) - Page 57
Forms: CASE syntax #2 (/learn/case2.asp) - Page 58
Forms: IF syntax #1 (/learn/if.asp) - Page 59
Forms: IF syntax #2 (/learn/if2.asp) - Page 60
Forms: IF syntax #3 (/learn/if3.asp) - Page 61
Forms: IF syntax #4 (/learn/if4.asp) - Page 62
Forms: For Each Iteration (/learn/formforeach.asp) - Page 63
Form - Submit To Self' (/learn/formsubmitself.asp) - Page 64
Form - Change Action on Fly (/learn/formactionchange.asp) - Page 65
Databases (/learn/database.asp) - Page 66
Displaying Table w/Simple Code (/learn/dbsimple.asp) - Page 67
List Box Displayed Generically (/learn/dblist.asp) - Page 68
Database to ListBox Online Resources (/learn/dblistmore.asp) - Page 69
DSNLess Connections (/learn/dbopen.asp) - Page 70
DSN Setup #1 by Rob Martinson (/learn/dsn1.asp) - Page 71
DSN Setup #2 by Rob Martinson (/learn/dsn2.asp) - Page 72
DSN Setup #3 by Rob Martinson (/learn/dsn3.asp) - Page 73
DSN Setup #4 by Rob Martinson (/learn/dsn4.asp) - Page 74
DSN Setup #5 by Rob Martinson (/learn/dsn5.asp) - Page 75
DSN Setup #6 by Rob Martinson (/learn/dsn6.asp) - Page 76
Full Cycle #1 Show/Edit/Update (/learn/dbfull1.asp) - Page 77
Full Cycle #2 Show/Edit/Update (/learn/dbfull2.asp) - Page 78
Full Cycle #3 Show/Edit/Update (/learn/dbfull3.asp) - Page 79
SQL Mistakes Everyone Makes (/learn/dbtroubleshoot2.asp) - Page 80
DB: Table Displayed Generically (/learn/dbtable.asp) - Page 81
Getstring to display database table (/learn/dbtablegetstring.asp) - Page 82
Getrows to display database table (/learn/dbtablegetrows.asp) - Page 83
GetRows w/no Numbers (/learn/dbtablegetrowsnonum.asp) - Page 84
Disconnected Recordsets, Display Table (/learn/dbtabledisconnected.asp) - Page 85
DB: More ways To Display Tables (/learn/dbtablemore.asp) - Page 86
DB: Generic DB by Eli Robillard (/learn/genericdb.asp) - Page 87
Generic DB Listserver (/learn/aspgenericdb.asp) - Page 88
DB: Converting a DB to a Comma-Delimited file (/learn/dbconvert.asp) - Page 89
DB: Deleting a Record w/SQL (/learn/dbSQLdelete.asp) - Page 90
DB: Access Scalability (/learn/accesstest.asp) - Page 91
ADO: Paging Records (/learn/dbtablepaged.asp) - Page 92
ADO: Limiting Number of Records (/learn/dbmaxrecs.asp) - Page 93
ADO: Count Records in Query (/learn/dbcount.asp) - Page 94
ADO: Cursor Types by Phil Paxton (/learn/adocursortypes.asp) - Page 95
ADO: Input Form (/learn/dbnewrec.asp) - Page 96
ADO: Input Form, added w/SQL (/learn/dbnewSQL.asp) - Page 97
ADO: Input Form, Added w/ADO .addnew (/learn/dbnewADO.asp) - Page 98
ADO: Tables within Databases (/learn/dbtablelists.asp) - Page 99
ADO: Schemas to access table lists (/learn/dbschemas.asp) - Page 100
ADO: Schemas to access All Data (/learn/dbschemasall.asp) - Page 101
ADO: Show Table,1 param (/learn/db1parm.asp) - Page 102
ADO: Update/edit Record (/learn/dbupdate.asp) - Page 103
DB: Troubleshooting Part 1 (/learn/dbtroubles.asp) - Page 104
DB: Troubleshooting Part 2 (/learn/dbtroubles2.asp) - Page 105
SQL Basics, Searching Databases (/learn/SQL.asp) - Page 106
SQL Troubles (/learn/SQLtroubles.asp) - Page 107
SQL: Example Tables (/learn/SQLexamples.asp) - Page 108
SQL: Where Clause Basics (/learn/SQLwhere.asp) - Page 109
SQL: Where Clause Examples (/learn/SQLwhere2.asp) - Page 110
SQL: Search Forms #1 (/learn/SQLwhereform1.asp) - Page 111
SQL: Search Forms #2 (/learn/SQLwhereform2.asp) - Page 112
SQL: Search Forms #3 (/learn/SQLwhereform3.asp) - Page 113
SQL: Search AND/OR Operators (/learn/SQLandor.asp) - Page 114
SQL: Search AND/OR Examples (/learn/SQLandor2.asp) - Page 115
SQL: COUNT, GROUPBY (/learn/SQLcount.asp) - Page 116
SQL: SUM, MIN, AVE, MAX (/learn/SQLaggregate.asp) - Page 117
SQL Joins by Aaron Alexander (/learn/dbjoin.asp) - Page 118
RSFAST: Lightning Fast Database Library (/learn/rsfast.asp) - Page 119
RSFast Library Introduction (/learn/rsfast-intro.asp) - Page 120
Table Display Fast (/learn/rsfast-table.asp) - Page 121
Table Display Fast + Caching (/learn/rsfast-table-cached.asp) - Page 122
Listbox Display Fast (/learn/rsfast-lists.asp) - Page 123
Listbox Display Fast + Caching (/learn/rsfast-lists-cached.asp) - Page 124
Templates for any look Fast (/learn/rsfast-templates.asp) - Page 125
Debug Info helps troubleshoot (/learn/rsfast-lists-debug.asp) - Page 126
Library Source Code (/learn/rsfast-lib.asp) - Page 127
caching Method Explained (/learn/rsfast-cache.asp) - Page 128
New Features for Future Versions (/learn/rsfast-newfeatures.asp) - Page 129
Editors Used With ASP (/learn/editors.asp) - Page 130
ASPExpress: HOT ASP Editor (/learn/aspexpress.asp) - Page 131
Visual Interdev + Admunsen Resources (/learn/admunsen.asp) - Page 132
Visual Interdev Listserver (/learn/aspvisualinterdev.asp) - Page 133
Homesite: HTML editor (/learn/homesite.asp) - Page 134
DreamWeaver: HTML and Script Editor (/learn/dreamweaver.asp) - Page 135
Essential Commercial Components (/learn/components.asp) - Page 136
ASPDB: Displaying Data (/learn/aspdb1.asp) - Page 137
ASPDB: Editing, Adding Data (/learn/aspdb2.asp) - Page 138
BrowserHawk: Determing Browser Type (/learn/bhbrowtype.asp) - Page 139
AOL detection w/BrowserHawk (/learn/bhaol.asp) - Page 140
MS-Wallet w/BrowserHawk (/learn/bhwallet.asp) - Page 141
Reverse DNS lookups w/BrowserHawk (/learn/bhresolveip.asp) - Page 142
BrowserHawk - Frame support (/learn/bhframes.asp) - Page 143
Flash Detection w/BrowserHawk (/learn/bhflash.asp) - Page 144
ServerObject Mail: Simple Example (/learn/serverobjectsmail.asp) - Page 145
ServerObject: Mailing Form w/ASPMail (/learn/formsendmail.asp) - Page 146
3rd Party Mail, CDO/CDONTS Listserver (/learn/aspmail.asp) - Page 147
SA: File Upload, Simple Example (/learn/uploadsimple.asp) - Page 148
SA: File Upload, Multi-part form (/learn/uploadmultipart.asp) - Page 149
SA: File Upload, Limit Size (/learn/uploadlimitsize.asp) - Page 150
SA: File Upload, Many Files (/learn/uploadmanyfiles.asp) - Page 151
Upload/Soft-Artisans Listserver (/learn/aspsoftartisans.asp) - Page 152
Perf Counters on ASP page (/learn/perfcounters.asp) - Page 153
Authentication & Security (/learn/authenticate.asp) - Page 154
Authenticate: Overview by Kevin Flick (/learn/authenticateoverview.asp) - Page 155
Authenticate: Comparison by Kevin Flick (/learn/authenticatecomparisons.asp) - Page 156
Authenticate: NT Challenge/Response by Kevin Flick (/learn/authenticatentcr.asp) - Page 157
Authenticate: Basic Authentication by Kevin Flick (/learn/authenticatebasic.asp) - Page 158
Authenticate: Cookies by Kevin Flick (/learn/authenticatecookies.asp) - Page 159
Authenticate: Certificates by Kevin Flick (/learn/authenticatecertificate.asp) - Page 160
Authenticate: Build Your Own by Kevin Flick (/learn/authenticatebuild.asp) - Page 161
Authenticate: Protect Pages via Login #1 (/learn/security.asp) - Page 162
Authenticate: Protect Pages via Login #2 (/learn/security2.asp) - Page 163
Authenticate: 3rd Party by Kevin Flick (/learn/authenticate3rdparty.asp) - Page 164
Authentix Flicks Support Listserver (/learn/aspflicks.asp) - Page 165
Troubleshooting, Error Trapping (/learn/troubles.asp) - Page 166
Errors: Basics (/learn/errors1.asp) - Page 167
Errors: More Ways To Trap (/learn/errors2.asp) - Page 168
Errors: Resources Online (/learn/errormore.asp) - Page 169
Errors: Trapping EVERY Error (/learn/dbtablewitherrortrap.asp) - Page 170
Debug variables Easy Way (/learn/debug1.asp) - Page 171
Errors: DB Error Information Trapping (/learn/dbtroubleshoot.asp) - Page 172
DBFAQ: Operation must use Updatable Query (/learn/FAQdbUpdate.asp) - Page 173
DBFAQ: User Entered ' in field (/learn/FAQdbSinglequote.asp) - Page 174
DBFAQ: LIKE operator * not working (/learn/FAQdbLIKE.asp) - Page 175
DBFAQ: retrieving MEMO/BLOBs generates error (/learn/FAQdbMEMO.asp) - Page 176
DBFAQ: Syntax Error in SQL Statement (/learn/FAQdbSQLSyntax.asp) - Page 177
SQL Debugging Made Easy (/learn/debug2.asp) - Page 178
Errors: Trapping Open Connections (/learn/dbtroubleshootopen.asp) - Page 179
Troubleshoot: Getting Help from Lists! (/learn/asptroubles.asp) - Page 180
Troubleshoot: Worldwide (/learn/asptroubles2.asp) - Page 181
Troubleshoot: Specialized (/learn/asptroubles3.asp) - Page 182
Troubleshoot: Version of ASP Sofware (/learn/versioncheck.asp) - Page 183
Troubleshoot: Registered Components (/learn/componentchecker.asp) - Page 184
Troubleshoot: DB Drivers by Christophe Wille (/learn/connectioninfo.asp) - Page 185
PWS: Personal Web Server Introduction (/learn/PWS.asp) - Page 186
Code w/all ASP Features. Quality, Re-usable Code (/learn/qualitycode.asp) - Page 187
Strings: Core Functions (/learn/strings.asp) - Page 188
Strings: SPLIT Function (/learn/stringsplit.asp) - Page 189
Strings: REPLACE Function (/learn/stringreplace.asp) - Page 190
Strings: JOIN Function (/learn/stringjoin.asp) - Page 191
Arrays: Basics (/learn/arrays.asp) - Page 192
Arrays: Variable Size (/learn/arrays2.asp) - Page 193
Arrays: Best Way To Load (/learn/arrays3.asp) - Page 194
Arrays: Resources Online (/learn/arraysmore.asp) - Page 195
Dictionary Objects (/learn/dictionary.asp) - Page 196
Getrows Ultimate! (/learn/getrowsultimate.asp) - Page 197
Subroutine: Working with Dates #1 (/learn/subdates.asp) - Page 198
Subroutine: Working with Dates #2 (/learn/subdates2.asp) - Page 199
Subroutine: Query2Table (/learn/subdbtable.asp) - Page 200
Subroutine: Query2List (/learn/subdblist.asp) - Page 201
Subroutine: Highly Reusable (/learn/subreusable.asp) - Page 202
Subroutines w/Dictionary Objects (/learn/subdictionary.asp) - Page 203
Getrows Ultimate! (/learn/getrowsultimate.asp) - Page 204
Subroutine: List Box w/optional params (/learn/subDBlistbest.asp) - Page 205
Subroutine: Abstract HTML by Phil Paxton (/learn/libhtml.asp) - Page 206
Function: Working Days (/learn/functionworkingdays.asp) - Page 207
New Features in VBScript version5 (/learn/vbs5.asp) - Page 208
Text Files: Reading Them off Server (/learn/txtread.asp) - Page 209
Text Files: Writing Them on Server (/learn/txtwrite.asp) - Page 210
Text Files: Meyers-Briggs parsing #1 (/learn/mb1.asp) - Page 211
Text Files: Meyers-Briggs parsing #2 (/learn/mb2.asp) - Page 212
Text Files: Meyers-Briggs parsing #3 (/learn/mb3.asp) - Page 213
XML/XLST Myers-Briggs example (/learn/xmlmb.asp) - Page 214
Content Linker: Prev/Next Page (/learn/cl.asp) - Page 215
Content Linker: Table of Contents (/learn/cl2.asp) - Page 216
Content Linker: Listbox of contents (/learn/cl3.asp) - Page 217
Content Linker Library (/learn/contentlinker.asp) - Page 218
File Objects: Read Directory (/learn/fileobjects.asp) - Page 219
File Objects: Display Directory as Links/Graphics (/learn/fileobjects2.asp) - Page 220
File Objects: Read Disk Drive by Steven Harper (/learn/fileobjects3.asp) - Page 221
File Objects: Show Dir List by Tim Foster (/learn/fileobjects4.asp) - Page 222
Graphic Size Detector (/learn/graphicdetect.asp) - Page 223
High Speed Code, Scalable Code... (/learn/speedscale.asp) - Page 224
Time Tasks with Millisecond Accuracy (/learn/speedtimer.asp) - Page 225
Speed: Coding Tips (/learn/speedtips.asp) - Page 226
Why Buffer? (/learn/whybuffer.asp) - Page 227
Why GetRows or Getstring to get Data (/learn/whygetrows.asp) - Page 228
Speed: Server Optimization (/learn/speedserver.asp) - Page 229
Speed/Optimize Resources (/learn/speedmore.asp) - Page 230
Speed: [aspfastcode] listserver (/learn/speedresearch.asp) - Page 231
Speed: Database Percieved Speed (/learn/speedtables.asp) - Page 232
Database Retrieval Speed (/learn/speedtablesall.asp) - Page 233
OLEDB & ODBC Drivers differences (/learn/speedtablesdrivers.asp) - Page 234
IsClientConnected & Stray Tasks (/learn/isclientconnected.asp) - Page 235
Scale: Virtues of Nothing (/learn/nothing.asp) - Page 236
Scale: Connection Pooling (/learn/dbpooling.asp) - Page 237
Thread Basics: What is a Thread? (/learn/threads.asp) - Page 238
Thread Safety Issues (/learn/threadsafe.asp) - Page 239
Round-Robin Code Execution (/learn/roundrobin.asp) - Page 240
ASP Scalability Listserver (/learn/aspscalability.asp) - Page 241
ASP Components Building (/learn/buildcomponents.asp) - Page 242
C++/ATL: Component Building (/learn/buildc.asp) - Page 243
Java ASP Components Building (/learn/buildjava.asp) - Page 244
VB: Simple Component (/learn/buildvbsimple.asp) - Page 245
VB: Registering Component (/learn/buildregister.asp) - Page 246
VB: DLL overwrite problems (/learn/FAQvbDLLoverwrite.asp) - Page 247
VB: ADO, Run It! (/learn/buildvbado.asp) - Page 248
VB: ADO, Build It! (/learn/buildvbado2.asp) - Page 249
VB: Warnings/Guidelines (/learn/buildvbguidelines.asp) - Page 250
VB: General Building Guidelines (/learn/buildvb.asp) - Page 251
VB: Installation Requirements (/learn/buildvb2.asp) - Page 252
VB: Threading Models (/learn/buildvbthreads.asp) - Page 253
MTS - Microsoft Transaction Server (/learn/buildmtx.asp) - Page 254
MTS: Overview (/learn/buildmtxoverview.asp) - Page 255
MTS: Essentials (/learn/buildmtx2.asp) - Page 256
MTS: Transactional ASP pages (/learn/buildmtxasp.asp) - Page 257
MTS: Book (/learn/booksmtx.asp) - Page 258
MTS: Book (/learn/booksmtx2.asp) - Page 259
MTS: Registering Components (/learn/buildmtxregister.asp) - Page 260
Advice For Better Coding! (/learn/advice.asp) - Page 261
Database in Session or App. Say NO! (/learn/dbsessionapp.asp) - Page 262
advice: Cache No More by Phil Paxton (/learn/cachenomore.asp) - Page 263
advice:Option Explicit (/learn/explicit.asp) - Page 264
advice: Encode with Redirects (/learn/encode.asp) - Page 265
advice: Write Your SQL (/learn/sqlwrite.asp) - Page 266
advice: Named constants for ADO are better (/learn/namedconstants.asp) - Page 267
advice: Clean Up Your Room, I mean Objects (/learn/cleanup.asp) - Page 268
advice: Server.MapPath is Good (/learn/pathmap.asp) - Page 269
advice: Just Say No to Session COM objects (/learn/nosessionobjects.asp) - Page 270
advice: Don't Read COM Properties Twice (/learn/propertyexpense.asp) - Page 271
advice: Secure Code and Data (/learn/securecode.asp) - Page 272
advice: Encaspulate Code! (/learn/encapsulate.asp) - Page 273
advice: CASE reads better than IF (/learn/caseisbetter.asp) - Page 274
advice: Error Trapping Strategies (/learn/errorstrategies.asp) - Page 275
advice: Error Trapping Secrets (/learn/errorsecrets.asp) - Page 276
advice: You Should... (/learn/shoulds.asp) - Page 277
Appendix A: Overview of ASP Objects (/learn/overview.asp) - Page 278
ASP Objects: Built In (/learn/aspobjects.asp) - Page 279
ASP Objects: Created when Needed (/learn/aspobjects2.asp) - Page 280
Appendix B: Related Web/Com Technologies (/learn/webcom.asp) - Page 281
Index Server via ADO (/learn/indexserver.asp) - Page 282
Commerce and ASP (/learn/commerce.asp) - Page 283
Server JavaScript: Resources (/learn/javascript.asp) - Page 284
Validation Resources (/learn/validationmore.asp) - Page 285
Listboxes: Linked Dynamically w/JavaScript (/learn/listdynamic.asp) - Page 286
Dynamic ListBox Online Examples (/learn/listdynamicmore.asp) - Page 287
Listboxes: Linked Dynamically from Database w/JavaScript (/learn/listdynamicdb.asp) - Page 288
Listboxes: Easy Choices by Bill Wilkinson (/learn/listdual.asp) - Page 289
Server Perlscript: Resources (/learn/perlscript.asp) - Page 290
Remote Scripting Simple Example (/learn/remotescripting.asp) - Page 291
Remote Scripting Listbox (/learn/remotescriptinglist.asp) - Page 292
Remote Scripting Microsoft Example (/learn/remotescriptingms.asp) - Page 293
[aspRemoteScript] list (/learn/aspremotescripting.asp) - Page 294
RDS: Remote Data Services Intro (/learn/rds.asp) - Page 295
RDS Resources by Carl Prothman (/learn/prothman.asp) - Page 296
ADSI: Active Directory Services Interface Intro (/learn/ADSI.asp) - Page 297
MSMQ: Overview (/learn/MSMQ.asp) - Page 298
Usability: Resources (/learn/usability.asp) - Page 299
Usability: Safe Color Pallete (/learn/safecolors.asp) - Page 300
Appendix C: Oracle and ASP (/learn/oracle.asp) - Page 301
Oracle: I can't connect (/learn/FAQOracleconnect.asp) - Page 302
Oracle: Getting Help from Listserver (/learn/asporacle.asp) - Page 303
Oracle: Calling Stored Procs (/learn/FAQOraclestoredproc.asp) - Page 304
Oracle: OLEDB Resource(Session) Pooling (/learn/oracleoledbpooling.asp) - Page 305
Oracle: Recordsets from Stored Procedures using REF CURSORs (/learn/oraclerecordsetsfromsp.asp) - Page 306
Oracle: Returning Recordsets via ADO (/learn/oraclerecordsetsado.asp) - Page 307
Oracle: Know any good books? (/learn/FAQOraclebooks.asp) - Page 308
Appendix D: ASP Books & Online Resources (/learn/research.asp) - Page 309
Must Buy Component Building Book (/learn/bookcomponents.asp) - Page 310
ASP101.com Scripts for your site (/learn/asp101.asp) - Page 311
4GuysFromRolla.com Tons of ASP Material (/learn/4guysfromrolla.asp) - Page 312
ASPToday.com from WROX (/learn/asptoday.asp) - Page 313
Appendix E: Frequently Asked Questions (/learn/faqs.asp) - Page 314
Commerce: certificates, https:// (/learn/FAQCommerceCertif.asp) - Page 315
Commerce: online charging (/learn/FAQCommerceCharge.asp) - Page 316
Commerce: components, shopping carts (/learn/FAQCommerceCarts.asp) - Page 317
Jscript: closing DB Connections (/learn/FAQJscriptCleanUp.asp) - Page 318
Jscript: online references (/learn/FAQJscriptRefs.asp) - Page 319
Jscript: display databases (/learn/FAQJscriptDB.asp) - Page 320
VB: Recommended books (/learn/FAQvbBooks.asp) - Page 321
Alphabetical Index (/learn/alphaindex.asp) - Page 322
Coming Soon/Very Rough Drafts! (/learn/comingsoon.asp) - Page 323
Data Types: VBScript (/learn/types.asp) - Page 324
Data Types: Conversion (/learn/convert.asp) - Page 325
Loops: FOR NEXT #1 (/learn/ForNext.asp) - Page 326
Loops: FOR NEXT #2 (/learn/ForNext2.asp) - Page 327
Ad Rotator (/learn/ad.asp) - Page 328
Content Rotator (/learn/cr.asp) - Page 329
DB: Command Object (/learn/command.asp) - Page 330
DB: Command Object/Queries (/learn/commandquery.asp) - Page 331
DB: Command Object/Create Tables (/learn/commandcreate.asp) - Page 332
Reporting: Simple Example (/learn/reportsimple.asp) - Page 333
Reporting: Powerful Example (/learn/reportpowerful.asp) - Page 334
Dictionaries: Different Approach #1 By Paul Rigor (/learn/dictionaryadvanced.asp) - Page 335
Dictionaries: Different Approach #2 by Paul Rigor (/learn/dictionaryadvanced2.asp) - Page 336
Validate data (/learn/validate.asp) - Page 337
3rd Party: WebJam (/learn/webjam.asp) - Page 338
Time Tasks: VB Component by Sunny Yu #1 (/learn/asptime.asp) - Page 339
Time Tasks: VB Component by Sunny Yu #2 (/learn/asptimer.asp) - Page 340
Cookies: Reading Them (/learn/cookiesform.asp) - Page 341
Cookies: Writing Them (/learn/cookiesformrespond.asp) - Page 342
Cookies: Deleting Them (/learn/cookiesforget.asp) - Page 343
Cookies: Simplified by Paul Rigor (/learn/cookiesub.asp) - Page 344
All material is ©1998-2000 by Charles Carroll. All rights reserved. May be used and printed for any single individual with no restriction. Cannot be reprinted, resold, or commercially made available without the written consent of Charles Carroll.
![]() |
Primary
Writer/Site Programming:
Business
Manager: Inspiration, Assistance,
Motivation: |
![]() |
Additional
Writers/Contributors:
Aaron
Alexander, Kevin Flick, Steve Genusa, Steven Harper, John Kauffman. Andrew Laken,
Juan Llibre, Rob Martinson, Phil Paxton, George Reilly, Paul Rigor,
Christophe Wille, David Wihl, and Sunny Yu.
thanks for feedback,
support:
Joao
Oliveira contato123@zipmail.com.br
Scott Mitchell www.4guysfromrolla.com
ASP Quick Lessons is a on-line
book published @
http://www.learnasp.com
Core Ideas
What is ASP? Obtaining The Software (whatis.asp) - Page 4
AspInstall listserver (aspinstall.asp) - Page 5
Simple ASP Page, Server Scripting (whatisexample.asp) - Page 6
MS Online Documentation (docs.asp) - Page 7
Response: Basics (res.asp) - Page 8
Response: Buffers, Redirect (res2.asp) - Page 9
Response: Redirection (res3.asp) - Page 10
Response: Quotes & Special Characters (res4.asp) - Page 11
Response: Encoding URLs, HTML (res5.asp) - Page 12
Include: Basics (inc.asp) - Page 13
Include: Dynamic FileName (includedynamic.asp) - Page 14
Includes: Other Sites, Dynamic FileNames (includeasphttp.asp) - Page 15
Include/Redirects: New Win2k Commands (incwin2k.asp) - Page 16
Include: Books Sample Exercise (booksample.asp) - Page 17
More Book Sample Exercises (booksample2.asp) - Page 18
Format: Numbers #1 (formatnumbers.asp) - Page 19
Format: Numbers #2 (formatnumbers2.asp) - Page 20
Format: Dates #1 (formatdates.asp) - Page 21
Date/Time on ASP Pages by Tony Arguelles (datetime.asp) - Page 22
Loops: DO WHILE/UNTIL #1 (DoLoop.asp) - Page 23
Loops: Timeouts #2 (DoLoop2.asp) - Page 24
Loops: Intercepting Timeouts #3 (DoLoop3.asp) - Page 25
Server Variables: Popular Ones (server.asp) - Page 26
Server Variables: Domain/Host Name (server2.asp) - Page 27
Server Variables: Displaying All (serverall.asp) - Page 28
Random Content/Rotating Info (randomadvice.asp) - Page 29
Browscap: Basics (bc.asp) - Page 30
Browscap: Intricate Details (bcdetails.asp) - Page 31
Listserver for Browser Problems (aspbrowserheck.asp) - Page 32
(surprisingly even though ASP was shipped in Feb 1996, most explanations are still HUGE in books and use quite scary technical terms) We will try to present this all in clear, concise terms and be complete as well. Hang on. Here we go.
ASP is:
Tip: Trouble/Errors installing PWS? Here is the answer(s):
http://www.acceleratedcomputers.com/pws4.htm
has the answer to several annoying error messages!http://rwebs.net/webdesign/pwsmore.htm
also has some excellent info about how and whether to install PWS.
Tip: Trouble/Errors installing IIS5?
When IIS5 does not start:
http://www.iisfaq.com/Articles/134/
More ASP facts:
Are you one of those unlucky folks who gets cryptic errors or has trouble Installing Asp? Just join http://www.asplists.com/asplists/aspinstall.asp and submit your problem by email. Others will help you!
Related Links:
Excellent info about how and whether to install PWS @
http://rwebs.net/webdesign/pwsmore.htm
The answer to several annoying error messages @
http://www.acceleratedcomputers.com/pws4.htm
PWS NT Otion Pack4 WILL Install on Win9x @
http://www.microsoft.com/ntserver/nts/downloads/recommended/NT4OptPk/default.asp
Recommended Books:
Related Lists:
Mobile lists by Asplists @
http://www.asplists.com/asplists/mobile.asp
Mobile lists by WROX @
http://p2p.wrox.com/mobile/
Rules:
Mailing List Manners (suggested by Bob Filipiak) @
http://db.tidbits.com/getbits.acgi?tbser=1141
Ken Shaffer's Advice on Asking For Help... @
http://www.adopenstatic.com/personal/help.asp
Now let us go over the essential mechanisms that are ASP:
| <html><head> <TITLE>hi.asp</TITLE> </head> <body bgcolor="#FFFFFF"> Today is <%=now%> and all is well<br> <%if hour(now())>12 THEN%> Good Evening <%ELSE%> Good Morning! <%END IF%> </body></html> |
The webserver file
<<<<<<< |
|
|
ASP
compiler grabs page Interprets all the <% %> markers before browser sees page! |
| <html><head> <TITLE>hi.asp</TITLE> </head> <body bgcolor="#FFFFFF"> Today is Tue 10:30am and all is well<br> Good Morning </body></html> |
Before 12pm
the user at the browser receives
<<<<<<< |
|
|
|
| <html><head> <TITLE>hi.asp</TITLE> </head> <body bgcolor="#FFFFFF"> Today is Tue 02:00pm and all is well<br> Good Day! </body></html> |
After 12pm
the user at the browser receives
<<<<<<< |
Microsoft Documentation
There is quite a bit of online documentation that comes with Microsoft Active Server Pages. Our tutorial was written with the express purpose of being a friendlier, easier to understand set of lessons than the free documentation Microsoft gives you below:
| IIS4 Docs | |
| NT4 Option Pak | /iishelp |
| ASP objects reference | /iishelp/iis/htm/asp/intr1orp.htm |
| Ex Air | /IISSamples/ExAir/default.asp |
| Installable Components for ASP | /iishelp/iis/htm/asp/comp275c.htm |
| JScript Language Reference | /iishelp/JScript/htm/JStoc.htm |
| VBScript Language Reference | /iishelp/VBScript/htm/VBStoc.htm |
| Server Side Include Reference | /iishelp/iis/htm/asp/iissiref.htm |
| ASP Quick reference card | /iishelp/iis/htm/asp/iiwaref.htm |
| ASP Tutorial* | click here |
| IIS3 Docs | |
| Roadmap / Official IIS3 Docs | /iasdocs/aspdocs/roadmap.asp |
| IIS3 Code Samples | /aspsamp/samples/samples.htm |
| AdventureWorks | /AdvWorks/default.asp |
* ASP Tutorial resides at this ridiculously
long URL:
/iishelp/iis/htm/asp/iiselect.asp?LessonFile=%2Fiishelp%2Fiis%2Fhtm%2Fasp%2Fiiatmd1%
Response Object
The response object is useful, feature rich, and subtle. We are going to focus on it's most fundamental capabilities -- the 20% you will use 80% of the time. The capabilities we think are vital include:
Here is a script utilizing response.write to send some information to the browser. It also uses dateadd, a built-in function documented at http://help.activeserverpages.com/iishelp/VBScript/htm/vbs90.htm.
1 <html><head>Here is a script utilizing response.end to prematurely end a page:
1 <html><head>
Response Object Part2 - Buffer Explanation
Does this error message plague you?The Simple Fix....Response object error 'ASP 0156 : 80004005'
Header Error
whatever.asp, line #
The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.
One drawback to <%response.buffer=true%> is
if a page takes a while to compose (i.e. a couple thousand records from a
database) people see nothing until the page is completely rendered. If a page
takes 20 seconds to render, the browser user sees nothing until the 20th second!
In that situation to avoid appearing as the page is dead, a well-placed
<%response.flush%> tell server to send HEADER + whatever
text so far so lets readers see the portions of the page being built. Increasing
percieved speed of database displays is explained at http://www.learnasp.com/learn/speedtables.asp
by judicous use of flushing buffer.
NT4 and Win 2000 differences
NT4 <%buffer=false%> by default which hurts overall server speed (http://www.learnasp.com/advice/whybuffer.asp). Win 2000 <%buffer=true%> by default. A clever administrator can change NT4 registry so buffer=true for all scripts and will see major server performance improvements (see http://www.learnasp.com/learn/speedserver.asp for other ones).
Here is a non-working page that will display the error:
1 <%response.buffer=false%>
2 <html><head>
3 <title>dailystuff.asp</title>
4 </head>
5 <body>
6 <%
7 whatweekday=Weekday(now())
8 select case whatweekday
9 case vbSunday
10 response.redirect "http://www.cnn.com"
11 case vbMonday
12 response.redirect "http://www.activeserverpages.com"
13 case vbTuesday
14 response.redirect "http://www.aspalliance.com"
15 case vbWednesday
16 response.redirect "http://www.aspconvention.com"
17 case vbThursday
18 response.redirect "http://www.aspmagazine.com"
19 case vbFriday
20 response.redirect "http://www.dilbert.com"
21 case vbSaturday
22 response.redirect "http://www.movielink.com"
23 end select
24 %>
25 </body>
26 </html>
Here is the fixed page that will NOT display the error:
1 <%response.buffer=true%>
2 <html><head>
3 <title>dailystuff.asp</title>
4 </head>
5 <body>
6 <%
7 whatweekday=Weekday(now())
8 select case whatweekday
9 case vbSunday
10 response.redirect "http://www.cnn.com"
11 case vbMonday
12 response.redirect "http://www.activeserverpages.com"
13 case vbTuesday
14 response.redirect "http://www.aspalliance.com"
15 case vbWednesday
16 response.redirect "http://www.aspconvention.com"
17 case vbThursday
18 response.redirect "http://www.aspmagazine.com"
19 case vbFriday
20 response.redirect "http://www.dilbert.com"
21 case vbSaturday
22 response.redirect "http://www.movielink.com"
23 end select
24 %>
25 </body>
26 </html>
Why? (The Tough Answer with Gory Details)
...first of all thanks to Ken Schaefer of Adopenstatic.com for pointing out huge flaws and misleading information in this page as it was written before 1/10/2001. Its revision on 1/10 was prompted by Ken. He wrote an article on this too at http://www.adopenstatic.com/faq/headererror.asp...
That line will do away
with all "headers are already sent" messages. It essentially tells the
server don't write
anything at all to browser until
a) response.end executes thus stopping the page dead in
tracks and sending to browser
b) response.flush executes
c) 100% of the page is executed and it finishes all the
ASP and HTML.
d) response.redirect is sent (provided no content or text has been
sent with response.flush)
An ASP page must be sent to browser in a specific strict order: first the header, and then the content. The header could contain cookies (see http://www.learnasp.com/learn/cookies.asp), redirects, other headers inserted by the response.addheader command and other header information. If your page mixes content (any text) and mixes in header info this is not following the strict rules. If the browser writes any data that is page content, and then headers cannot be sent -- it is "too late" -- it can't change "horses in midstream" -- that is header info cannot come inside or after data. Buffering does not send the page content and headers in exact order they appear in script. Even if headers and content is mixed the header and content are sent in correct order when buffer is flushed (either by explicit response.flush or script completes).
Response Object - Redirects
The response object can be used to decide what page to send a user to next. Specifically the response.redirect method will work in that capacity. We have made a script formjump.asp that takes advantage of this.
1 <html><head>
2 <TITLE>FormJump.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <form action="FormJumpRespond.asp" method="get">
5 <SELECT NAME="wheretogo">
6 <OPTION SELECTED VALUE="fun">Fun</OPTION>
7 <OPTION value="news">Daily News</OPTION>
8 <OPTION value="docs">ASP IIS3 Roadmap/Docs</OPTION>
9 <OPTION value="main">MainPage of ActiveServerPages.com</OPTION>
10 <OPTION value="sample">IIS 3 Sample ASP scripts</OPTION>
11 </SELECT>
12 <input type=submit value="Choose Destination">
13 </form>
14 </body></html>
The responder that reacts to this form is:
1 <%response.buffer=true%>
2 <html><head>
3 <title>formjumprespond.asp</title>&
4 <body bgcolor="#FFFFFF">
5 <%
6 ' My ASP program that redirects to URL
7 thisURL="http://www.activeserverpages.com"
8 where=Request.QueryString("Wheretogo")
9 Select Case where
10 case "main"
11 response.redirect thisURL & "/"
12 case "samples"
13 response.redirect thisURL & "/aspsamp/samples/samples.htm"
14 case "docs"
15 response.redirect thisURL & "/iasdocs/aspdocs/roadmap.asp"
16 case "news"
17 response.redirect "http://www.cnn.com"
18 case "fun"
19 response.redirect "http://www.dilbert.com"
20 End Select
21 response.write "All dressed up and I don't know where to go<br>"
22 response.write "I recommend --> " & "<br>"
23 response.write server.htmlencode(thisURL & "/learn/test/res2.asp?where=fun") & "<br>"
24 response.write "for a good laugh!" & "<P>"
25 %>
26 </body></html>
Response Object and Quotes
The response object is often used with a variety of syntax variations which we will detail here.
1 <html><head>
2 <title>res4.asp</title>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 ' The response object can be used to write text a variety of ways
6 ' depending on what style you personally prefer
7
8 ' Various permutations of writing to the browser
9 response.write "<form>"
10 response.write "Hello, Joe<br>"
11
12 who="Joe"
13 response.write "Hello, " & who & "<br>"
14 %>
15
16 Hello, <%=who%><br>
17
18 Which Book? <input type="TEXT" name="book" value="The Stand"><br>
19
20 <%
21 response.write "Which Book? <input type=""TEXT"" name=""book"" value=""The Stand""><br>"
22 %>
23
24 <%
25 response.write "Which Book? <input type='TEXT' name='book' value='The Stand'><br>"
26 %>
27
28 <%
29 quote=chr(34)
30 response.write "Which Book? <input type=" & quote & "TEXT" & quote & " name=" & quote & "book" & quote & " value=" & quote & "The Stand" & quote & "><br>"
31 %>
32
33
34 <%bookname="The Stand"%>
35 Which Book? <input type="TEXT" name="book" value="<%=bookname%>"><br>
36
37 <%
38 response.write "Which Book? <input type=""TEXT"" name=""book"" value=""" & bookname & """><br>"
39 %>
40 </form>
41 </body></html>
Response Object and HTML Encoding
The response object is often used in conjunction with various kinds of coding schemes. No discussion of response would be complete without a discussion of how to "handle" or "escape" special characters. This sample script demonstrates common conversion and transformation commands that make sense to use with the response.write command:
1 <html><head>
2 <title>res5.asp</title>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 ' The response object can be used to write text
6 ' but sometimes some functions must be used to transform
7 ' the text instead of sending as is to the browser
8
9 response.write "<B>Hyperion</b> by <I>Dan Simmons</i> is a great novel"
10 response.write "<p>"
11 response.write server.htmlencode("<B>Hyperion</b> by <I>Dan Simmons</i> is a great novel")
12 response.write "<p>"
13
14
15 response.write "Joe Smith & Hilda = a team"
16 response.write "<p>"
17 response.write server.URLencode("Joe Smith & Hilda = a team")
18 %>
19
20 </body></html>
Include Files
The include option is the heart of making efficient ASP files and re-usable chunks. It basically has two forms and now we will present the forms and their differences:
<!--#include virtual="/whatever.asp"-->
would include any file on your site (in this example, whatever.asp is in the web server's
root directory) but you must fully qualify the filename with a path.
<!--#include file="whatever.asp"-->
can include the whatever.asp file in the directory of the script that contains the
statement. It ASSUMES the current directory!
Example #1
<!--#include
virtual="/sally/filename.asp"-->
could include a file from sally's directory, even if the page with this statement
is (for example) in the /fred/finance folder.
Example #2:
<!--#include
file="/sally/filename.asp"-->
will fail from fred's directory.
Example #3:
<!--#include
file="../sally/filename.asp"-->
will succed from fred's directory but if the script that contains it is moved to a
different level in the tree structure it will fail to locate the file. INCLUDE VIRTUAL is
better if a script may be moved and is immune to relative path issues.
IMPORTANT: Include files are always processed and inserted before ASP scripts on the page are calculated. Thus a page with many IFs and SELECT CASEs that selectively include files in fact always include the file before the script begins executing.
Includes Files Dynamically
The include files are gathered and processed BEFORE any ASP code. Soif your code looks like this:
<%SELECT CASE
CASE 1 %>
<!--#include
virtual="whatever1.asp"-->
CASE 2 %>
<!--#include
virtual="whatever2.asp"-->
CASE 3 %>
<!--#include
virtual="whatever3.asp"-->
<%END SELECT%>
Three includes are performed before any ASP code is executed.
YOU CANNOT DO:
<%
whichfile="1"%>
<!--#include
virtual="whatever<%=whichfil%>.asp"-->
Though this is a reasonable idea.
<!--#include virtual="whatever.asp"-->
We however have coded a workaround that is FREE you may find useful. The workaround is:
1 <html><head>
2 <TITLE>includedynamic.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 whichfile="bookscifi.asp"
6 Call ReadDisplayFile(whichfile)
7 response.write "<hr>"
8
9 whichfile="bookhorror.asp"
10 Call ReadDisplayFile(whichfile)
11 response.write "<hr>"
12
13
14 whichfile="/learn/test/bookmarketing.asp"
15 Call ReadDisplayFile(whichfile)
16 response.write "<hr>"
17 %>
18
19 </body></html>
20 <%
21 SUB ReadDisplayFile(FileToRead)
22 whichfile=server.mappath(FileToRead)
23 Set fs = CreateObject("Scripting.FileSystemObject")
24 Set thisfile = fs.OpenTextFile(whichfile, 1, False)
25 tempSTR=thisfile.readall
26 response.write tempSTR
27 thisfile.Close
28 set thisfile=nothing
29 set fs=nothing
30 END SUB
31 %>
The only downside to this method is no ASP Code ( i.e.
anything in <% %> ) will be parsed or executed in the included file. If
you must execute ASP code the Win2k server.execute @
/learn/incwin2k.asp
or the 3rd party components like ASPHTTP must be employed @
/learn/include/asphttp.asp
Includes - Other Sites/Dynamic FileNames
3rd party components like ASPHTTP can grab the HTML contents of a specific URL into an ASP string. Supports GET/POST/HEAD documents via the HTTP protocol, examining response headers, transfering requests to a file (including binary transfers) and password authentication support. They can also be used on YOUR OWN SITE to make dynamic includes possible.
available at http://www.serverobjects.com/products.htm
Microsoft includes WinInet which seems to be
ideally suited for this EXCEPT is currently not thread safe, see:
http://www.learnasp.com/advice/threadsafe.asp
so Tools like ASPHTTP are a necessity.
Here is a sample where ASPHTTP is used to grab contents from another site.
1 <html><head>
2 <title>asphttpother.asp</title>
3 </head>
4 <body>
5 <%
6 Set HttpObj = Server.CreateObject("AspHTTP.Conn")
7
8 HttpObj.Url = "http://www.funinspace.com"
9 strResult = HttpObj.GetURL
10
11 STRresult=server.htmlencode(STRresult)
12 response.write STRresult
13
14 SET HTTPobj = nothing
15 %>
16 </body>
17 </html>
18
19
Here is a sample where ASPHTTP is used to allow a string to decide which page on our site is executed.
1 <html><head>Include/redirect Goodies from Win2000
Win 2000 introduces two new ways to deal with redirects and includes:
server.transfer
which unlike redirect, does not interact with the browser. The server switches
to a different page/site without any browser interaction or headers.
server.execute
which can take a dynamically generated string and execute the code in the asp
script named by the string.
Book Sample by Charles Carroll
The Recommend Book Sample Files provides you with several files that when created, prepare you for applying several powerful content management tools and session examples in the following pages. The features detailed will include:
all come together in this example.
Here is the code for bookheader.asp:
1 Recommended Books for <%=session("fname")%> <%=session("lname")%><br><hr>
Here is the code for bookfooter.asp:
1 <hr><br>
2 Recommended Books has <%=application("howmany")%> people reading it now!
Here is the code for bookfuture.asp:
1 <html><head>
2 <title>bookfuture.asp</title>&
3 <!--#include file="bookheader.asp"-->
4 <body>
5 <h1>Future Books</h1>
6 <ul>
7 <li><b><i>Visions</b></i><br>Michio Kaku</li>
8 <li><b><i>Future Magic</b></i><br>Robert Forward</li>
9 </ul>
10 <!--#include file="bookfooter.asp"-->
11 </body></html>
Here is the code for bookhorror.asp:
1 <html><head>
2 <title>bookhorror.asp</title>&
3 <!--#include file="bookheader.asp"-->
4 <body>
5 <h1>Horror Books</h1>
6 <ul>
7 <li><b><i>Carrion Comfort</b></i><br>Dan Simmons</li>
8 <li><b><i>The Stand</b></i><br>Steven King</li>
9 <li><b><i>Children of Darkness</b></i><br>Dan Simmons</li>
10 <li><b><i>Thinner</b></i><br>Steven King</li>
11 <li><b>Fires of Eden<i></b></i><br>Dan Simmons</li>
12 </ul>
13 <!--#include file="bookfooter.asp"-->
14 </body></html>
Here is the code for bookmarketing.asp:
1 <html><head>
2 <title>bookMarketing.asp</title>&
3 <!--#include file="bookheader.asp"-->
4 <body>
5 <h1>Marketing Books</h1>
6 <ul>
7 <li><b><i>22 Immutable Laws of Branding</b></i><br>Reiss and Reiss</li>
8 <li><b><i>22 Immutable Laws of Marketing</b></i><br>Reiss and Trout</li>
9 <li><b><i>Marketing Warfare</b></i><br>Reiss and Trout</li>
10 <li><b><i>Horse Sense</b></i><br>Reiss and Trout</li>
11 <li><b><i>Words That Sell</b></i><br>by ??</li>
12 </ul>
13 <!--#include file="bookfooter.asp"-->
14 </body></html>
Here is the code for booknovels.asp:
1 <html><head>
2 <title>booknovels.asp</title>&
3 <!--#include file="bookheader.asp"-->
4 <body>
5 <h1>Recommended Novels</h1>
6 <ul>
7 <li><b><i>A Prayer for Owen Meaney</b></i><br>John Irving</li>
8 <li><b><i>Cider House Rules</b></i><br>John Irving</li>
9 <li><b><i>Heart of the Country</b></i><br>Greg Mathhews</li>
10 <li><b><i>All That Remains</b></i><br>Patricia Cornwell</li>
11 <li><b><i>Presumed Innocent</b></i><br>Scott Turrow</li>
12 <li><b><i>Time to Kill</b></i><br>John Grisham</li>
13 <li><b><i>Disclosure</b></i><br>Michael Chrichton</li>
14 <li><b><i>Mount Dragon</b></i><br>Lincoln and Childs</li>
15 </ul>
16 <!--#include file="bookfooter.asp"-->
17 </body></html>
Here is the code for bookscifi.asp:
1 <html><head>
2 <title>bookscifi.asp</title>&
3 <!--#include file="bookheader.asp"-->
4 <body>
5 <h1>Science Fiction Recommended Books</h1>
6 <ul>
7 <li><b><i>Ender's Game</b></i><br>Orson Scott Card</li>
8 <li><b><i>Hyperion</b></i><br>Dan Simmons</li>
9 <li><b><i>Childhood's End</b></i><br>Arthur Clarke</li>
10 <li><b><i>TommyKnockers</b></i><br>Steven King</li>
11 </ul>
12 <!--#include file="bookfooter.asp"-->
13 </body></html>
Here is the code for bookselfhelp.asp:
1 <html><head>
2 <title>bookselfhelp.asp</title>&
3 <!--#include file="bookheader.asp"-->
4 <body>
5 <h1>Self Help Books</h1>
6 <ul>
7 <li><b><i>Road Less Travelled</b></i><br>Scott Peck</li>
8 <li><b><i>The Seven Habits of Highly Effective People</b></i><br>Steven Covey</li>
9 <li><b><i>First Things First</b></i><br>Steven Covey</li>
10 </ul>
11 <!--#include file="bookfooter.asp"-->
12 </body></html>
Book Sample Exercises
Not Ready Yet.
Will tie into the content linker and session login/abandon examples.
Format Numbers - Reference
Frequently you want a number to appear in a certain format. The most commands requests are for a set total number of digits and a set number of digits to the right of the decimal place. Less frequently there is a call for negative amounts displayed in parenthesis or that there should be leading zeros.
The FormatNumber function takes the contents of a number type variable and returns the contents in the specified format.
Syntax: FormatNumber(expression, iDigits, bleadingDigit, bParen, bGroupDigits)
| argument | meaning |
| expression | the variable holding the raw number |
| iDigits | number of digits to right of decimal point |
| bleadingDigit | 1 for leading zeros 0 for no leading zeros |
| bParen | 1 for parenthesis around negative numbers 0 for no parenthesis around negative numbers |
| bGroupDigits | 1 to display numbers as per regional settings in the Control
Panel 0 to over-ride settings in the Control Panel |
Format Numbers Part2 (by Charles Carroll)
The easiest way to demonstrate format numbers is just have some sample code that tries evry permutation of the command.
1 <html><head>
2 <TITLE>formatnumbers2.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 ' My ASP formatting number sample
6 mynumber=123.4567
7 response.write "<hr>" & mynumber & "<br>"
8 response.write "formatnumber(mynumber,0)" & "<br>"
9 response.write formatnumber(mynumber,0) & "<hr>"
10 response.write "formatnumber(mynumber,2)" & "<br>"
11 response.write formatnumber(mynumber,2) & "<hr>"
12 response.write "formatnumber(mynumber,6)" & "<br>"
13 response.write formatnumber(mynumber,6) & "<hr>"
14
15 mynumber=.4567
16 response.write mynumber & "<br>"
17 '0 means means no leading zeroes
18 response.write "formatnumber(mynumber,2,0)" & "<br>"
19 response.write formatnumber(mynumber,2,0) & "<hr>"
20 '1 means means pad with leading zeroes
21 'response.write "formatnumber(mynumber,2,1)" & "<br>"
22 'response.write formatnumber(mynumber,2,1) & "<hr>"
23
24 'mynumber=-123.4567
25 'response.write mynumber & "<br>"
26 '0 means means no parentheses for negative numbers
27 'response.write "formatnumber(mynumber,2,0,0)" & "<br>"
28 'response.write formatnumber(mynumber,2,0,0) & "<hr>"
29 '1 means means yes parentheses for negative numbers
30 'response.write "formatnumber(mynumber,2,0,1)" & "<br>"
31 'response.write formatnumber(mynumber,2,0,1) & "<hr>"
32 %>
33 </body></html>
Format Dates by Charles Carroll
The built-in date functions (described
in next paragraph) are pretty lame. The holy grail of data formatting is at:
http://www.4guysfromrolla.com/webtech/112098-2.shtml
http://www.learnasp.com/iishelp/VBScript/htm/vbs109.htm are the official online docs for the FormatDateTime function. The easiest way to demonstrate formatting dates is just have some sample code that tries every permutation of the command.
1 <html><head>
2 <title>formatdates.asp</title>
3 </head><body bgcolor="#FFFFFF"><html>
4 <%'My ASP program that formats dates
5 response.write "<hr>"
6 for counter=0 to 4
7 currentdate=now()
8 response.write "today is..." & "<br>"
9 response.write currentdate & "<P>"
10 select case counter
11 case 0
12 whichformat="vbgeneraldate"
13 case 1
14 whichformat="vblongdate"
15 case 2
16 whichformat="vbshortdate"
17 case 3
18 whichformat="vblongtime"
19 case 4
20 whichformat="vbshorttime"
21 end select
22 response.write "FormatDate(now()," & whichformat & ")="
23 response.write Formatdatetime(currentdate,counter) & "<P><HR>"
24 next%>
25 </body></html>
Including the date and/or time on a web page can be a subtle yet valuable addition when designing a web site. The addition of the date to the home page can create the impression that a site is constantly being updated with new content since each time a visitor loads the page, the current date will be displayed.
In this tutorial I'll teach you how to add the date and time to your ASP pages using the VBScript FormatDateTime() function. I'll explain how the function works, teach you how to integrate it into your ASP pages and illustrate the output you'll get depending on the arguments you pass. I'll round out the tutorial of the FormatDateTime() function by covering a few limitations that it has, which might or might not be a big deal depending on your specific needs.
Just FYI, this article assumes you know basic HTML and how to add ASP scripts to your web pages.
Microsoft provides a ton of predefined VBScript functions designed to reduce coding time. The FormatDateTime() function is one of those powerful functions and is really easy to use, too. This function uses the following format:
FormatDateTime(date, format)
There are two arguments the function accepts: date and format. Table 1-1 below describes these arguments in greater detail:
Table 1-1: The FormatDateTime() function and its arguments
| Argument | Argument Description | ||||||||||||||||||
| date | This argument is required and
can be any valid date expression such as Date or Now |
||||||||||||||||||
| format |
This format constant or format value specifies how the date and/or time will be displayed on your ASP page. When specifying the format argument, you
can either type the Visual Basic constant name (name in left column), or
the constant's corresponding value (0 - 4, from the middle column). They
do the same thing, it's just less typing if you use the value.
|
Table 1-1 is a good reference once you've got a
feel for how the FormatDateTime() function works or if you're an experienced
programmer. For those of you that aren't clear on how all the information in
table 1-1 relates to "real world" implementations, let's take a look
at some examples:
If you would like to display the current date, here are a few different ways to do it along with the results they produce:
<%= FormatDateTime(Date) %> returns: 3/11/2001
(You would get the same result by coding this: <%= FormatDateTime(Date, 0) %>)<%= FormatDateTime(Date, 1) %> returns: Sunday, March 11, 2001
<%= FormatDateTime(Date, 2)%> returns: 3/11/2001
If you would like to return the current time, here are a couple of ways to do that:
<%= FormatDateTime(Now, 3)%> returns: 10:39:25 AM
<%= FormatDateTime(Now, 4)%> returns: 10:39
If you would like to return the current date and time together, here's how to do just that:
<%= FormatDateTime(Now) %> returns: 3/11/2001 10:39:25 AM
If you're like me, you probably don't like the way the date and time displays above; it's not very cool looking, is it? In cases like this, you can actually include two FormatDateTime() functions next to each other, in order to get the date and time in a more desriptive format, like this:
<%= FormatDateTime(Date, 1) %> <%= FormatDateTime(Now, 3)%> returns:
Sunday, March 11, 2001 10:39:25 AM
Integrating the code into your ASP pages is really easy; here is how the code would look on a page with basic HTML to display the date:
<html>
<head>
<title>Here's the date</title>
</head>
<body>
Thank you for coming to this page. The current date is: <%= FormatDateTime(Date, 1) %>
</body>
</html>
The FormatDateTime() function is an extremely handy bit of code that can help you add a touch of flair almost instantly. I would like to mention four limitations that stick out in my mind, which may be an issue to you (or your clients) depending on the project at hand:
On the first through ninth days of a month the day shows up in the format of "Month 01, Year". I know it seems like a small thing but trust me, it's can be a big deal to some.
You are limited to basic formatting of the string that's returned by the FormatDateTime() function. Since the date and/or time function returns is a single string, you can bold, italicize and change the whole date/time by adding HTML or style sheet tags around it, but you can't change the display properties for a single part (e.g., the month).
With the FormatDateTime() function, you can do this:
<b><%= FormatDateTime(Date, 1) %></b> which would return this: Sunday, March 11, 2001
But you can't do this:
Wednesday, January 1, 1999
If limitations 1 or 2 are a major hang up for you, you'll need to use different ASP/VBScript techniques to add the date to your page. I'll cover those in my next article!
Limitation 3 is more of a by-product than a limitation, but I figured I would keep the naming conventions the same for this section. If you use the the FormatDateTime() function (or any other date related function) on the server side, the date/time returned will be whatever the server's date and time is, not your client's time from their system.
If you want to ensure that the date and/or time a visitor sees on your page is the date in their part of the coutry or world, then consider using client side VBScript as an Internet Explorer only solution, or switch to client side JavaScript for a universal browser solution.
Think of the displayed date or time as a "snap shot" of when the page was requested by the visitor. You cant use this function to display a "clock" that updates every second, or automatically update the date on the page when one day turns to the next.
If you wanted to display a dynamic clock on your page, you would need to use client side JavaScript, or VBScript (IE only) to handle that task.
I hope you've enjoyed this article on the FormatDateTime() function. I'll be back soon with more date and time related ASP fun! If you have any questions or comments, send me an Email at: tony@southbaywebdesigns.com
Do Loop Part #1 by Charles Carroll
To execute a code sequence more than once ASP provides:
Either of these statements can be followed by UNTIL or WHILE.
DO UNTIL
.....code to be repeated...
LOOP
DO
.....code to be repeated...
LOOP UNTIL
Do Loop and Timeouts by Charles Carroll
A loop that is infinite will not run forever. IIS will timeout the script (default is 90 seconds).
Here is an infinite loop that IIS will timeout:
1 <%response.buffer=true%>
2 <TITLE>doloop1.asp</TITLE>
3 <body bgcolor="#FFFFFF">
4 <HTML>
5 <%
6 DO
7 counter=counter+1
8 response.write counter & "<br>"
9 response.flush
10 LOOP
11 %>
12 </BODY>
13 </HTML>
14
Here is an infinite loop that we explicitly set a timeout for:
1 <%
2 response.buffer=true
3 server.scripttimeout=20
4 %>
5 <TITLE>loop2.asp</TITLE>
6 <body bgcolor="#FFFFFF">
7 <HTML>
8 <%
9 DO
10 counter=counter+1
11 response.write counter & "<br>"
12 response.flush
13 LOOP
14 %>
15 </BODY>
16 </HTML>
17
18
It has been assumed that a timed out script was impossible to intercept, but the next lesson shows how to use the transactional aspect of an ASP script to capture this elusive condition.
Do Loop Intercept Timeouts by Charles Carroll
The transactional nature of ASP pages can be used to intercept a script timeout.
loop3.asp traps a timeout:
loop4.asp succeeds and does not trigger the trap:
1 <%@ TRANSACTION=Required%>
2 <%
3 response.buffer=true
4 server.scripttimeout=40
5 %>
6 <HTML>
7 <TITLE>loop4.asp</TITLE>
8 <body bgcolor="#FFFFFF">
9 </BODY>
10 <%
11 DO UNTIL counter=400
12 counter=counter+1
13 response.write counter & "<br>"
14 LOOP
15 response.flush
16 response.write "Script Exexuted without incident!"
17 %>
18 </HTML>
19 <%
20 Sub OnTransactionAbort()
21 response.clear
22 Response.Write "The Script Timed Out"
23 end sub
24 %>
Server Variables by Charles Carroll
Available Server Variables are the result of a combination of the browser software and the server software. They are not always exactly the same on your server and with specific browsers as we document here. Server Variables are retrieved with request.servervariables("variablename"), for example:
| sn=request.servervariables("script_name") | name of script, i.e./learn/server.asp in this case |
| ref=request.servervariables("http_referer") | name of site page (unless they just typed the URL) they clicked on to get here. |
| br=request.servervariables("http_user_agent") | Identification string emitted by browser. |
| lan=request.servervariables("http_accept_language") | en for english. Basically indicates language the browser is targetted to. |
| user=request.servervariables("logon_user") | IE passes back NT logon in this variable! |
This script below demonstrates accessing a couple of these
variables:
1 <html><head>
2 <title>server.asp</title>&
3 <body>
4 <%
5 sn=request.servervariables("script_name")
6 response.write "Script Name=" & sn & "<br>"
7
8 ref=request.servervariables("http_referer")
9 response.write "Page thats links to this=" & ref & "<br>"
10
11 ua=request.servervariables("http_user_agent")
12 response.write "Browser String=" & ua & "<br>"
13
14 lan=request.servervariables("http_accept_language")
15 response.write "Browser Language=" & lan & "<br>"
16
17 user=request.servervariables("logon_user")
18 response.write "NT Logon Name=" & user & "<br>"
19 %>
20 </body></html>
Server Variables To Determine Domain
Server Variables have many uses. We will show you a popular one here. Web sites are typically attached to an IP address, but sometimes several domain names may point to the same IP. A clever ASP script could display the same page different ways depending on which domain name was typed utilizing the HTTP_HOST. Our site, for example has three domain names tied to the same IP ( activeserverpages.com, asptraining.com, learnasp.com, help.activeserverpages.com ) and the following script will provide different results depending on what domain name it is called from:
1 <html><head>
2 <title>server2.asp</title>&
3 <body>
4 <%
5 host=lcase(request.servervariables("HTTP_HOST"))
6 SELECT CASE host
7 CASE "www.asptraining.com"
8 response.write "Welcome Training Customer!"
9 CASE "www.activeserverpages.com"
10 response.write "Welcome To Our Reference Site!"
11 CASE "www.learnasp.com"
12 response.write "Welcome To Our Tutorial!"
13 CASE "www.aspeuro.com"
14 response.write "Welcome To Our European Site!"
15 CASE "www.asplists.com","www.asplist.com"
16 response.write "Welcome To Our ASP listservers!"
17 CASE "www.aspconventions.com","www.aspconvention.com"
18 response.write "Welcome To Our ASP convention site!"
19 CASE ELSE
20 response.write "Welcome!"
21 END SELECT
22 %>
23 </body></html>
Listing All Server Variables by Charles Carroll
The available Server Variables vary based on the result of a combination of the browser software and the server software. They are not always exactly the same on your server and with specific browsers as we document here. There is an easy way to obtain a list. If the script is executed on a given browser, the Server Variables displayed will reflect that browser plus your server.
1 <%
2 for each thing in request.servervariables
3 tempvalue=request.servervariables(thing)
4 response.write thing & "=" & tempvalue & "<br>"
5 next
6 %>
Since the above script appears in dozens of books and websites, we wanted to provide you with a better version. This script may prove useful as it will
1 <html><head>
2 <TITLE>serverall.asp</TITLE>&
3 <body bgcolor="#FFFFFF">
4 <%
5 Response.Write("<P><B>Server Variables</b><br>")
6 BlankVars="<P><B>Blank Server Variables</b><br>" & vbcrlf
7 quote=chr(34)
8 For Each Key in Request.ServerVariables
9 If instr(Key,"_ALL")+instr(key,"ALL_")=0 then
10 tempvalue=trim(request.servervariables(Key))
11 If len(tempvalue)=0 then
12 BlankVars=BlankVars & Key & ", "
13 Else
14 response.write "request.servervariables(" & quote
15 response.write Key & quote & ") "
16 response.write " =<br><B>" & tempvalue & "</b><p>" & vbcrlf
17 End If
18 end if
19 Next
20 response.write mid(BlankVars,1,len(BlankVars)-2)
21 %>
22 </body></html>
If the server has been secured with https://
then the following script will display some additional variables:
https://secure.activeserverpages.com/learn/test/serverall.asp
Random Advice / Rotating Information by Charles Carroll
This page demonstrates how to use several commands together to serve varying content based on a random number:
RND function
INT function
SELECT CASE
The script randomadvice.asp shows different advice every time the pge is refreshed:
1 <html><head>
2 <TITLE>randomadvice.asp</TITLE>
3 </head>
4 <body bgcolor="#FFFFFF">
5 <%
6 ' generate a random number 1-6
7 randomize
8 randomnum=int(rnd*6)+1
9 SELECT CASE randomnum
10 CASE 1,2,3%>
11 Plant your crops early this year<br>
12 No frost expected<br>
13 <%CASE 4%>
14 Never play cards<br>with a man named after a city<br>
15 <%CASE 5%>
16 You can never be too rich, too thin or backup too often<br>
17 <%CASE 6%>
18 A swallow keeps away the stork<br>
19 <%END SELECT%>
20 </body></html>
Browser Capabilites
The script below demonstrates the most commonly used property of the Browser Capabilites component.
1 <html><head>
2 <TITLE>bc.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <% Set bc = Server.CreateObject("MSWC.BrowserType") %>
5 Browser Name: <%=bc.browser %><p>
6 Browser Version: <%=bc.version%><p>
7 <% if (bc.frames = TRUE) then %>
8 I noticed you do frames<p>
9 <% else %>
10 I noticed you are frame challenged<p>
11 <% end if %>
12 <% if (bc.tables = TRUE) then %>
13 I noticed you do tables<p>
14 <% else %>
15 I noticed you can't do tables<p>
16 <% end if %>
17 <% if (bc.BackgroundSounds = TRUE)then %>
18 I noticed you allow me to play music<p>
19 <% else %>
20 I noticed you aren't a music listener<p>
21 <% end if %>
22
23 <% if (bc.vbscript = TRUE) then %>
24 I noticed you are VBscript capable<p>
25 <% else %>
26 I noticed you can't understand VB Script<p>
27 <% end if %>
28
29 <% if (bc.javascript = TRUE) then %>
30 I noticed you understand JScript<p>
31 <% else %>
32 I noticed you don't understand JScript<p>
33 <%
34 end if
35 set bc=nothing
36 %>
37 </body></html>
Browser Capability Details (by Charles Carroll)
Any ASP script attempting to detect a browser needs to realize the following:
Cyscape offer a downloadable latest, greatest BROWSCAP.INI and even offers e-mail subscriptions so you get the latest one sent to you. Visit http://www.cyscape.com/browscap to pick it up and/or subscribe. You can test in advance whether your browser is recognized by this file at http://www.cyscape.com/browtest.asp.
The latest, greatest BROWSCAP.ZIP from Juan Llibre is
available at http://www.asptracker.com.
Juan Libre's Detect Your Screen Res articles is another
sample making use of BROWSCAP.INI.
These two sources give you the means to correctly identify
the latest browser.
The
BrowserHawk Component at http://www.cyscape.com/browserhawk offers
accuracy far better than what any browscap file can provide. It also provides information
on more than twice as many properties, including FileUpload, MouseOver, SSL, DHTML,
StyleSheets, Authenticode, OSDetails, Language, and many more! It will even download and
install updated browser definition files for you automatically! Evaluation download
available at:
http://www.cyscape.com/browserhawk/download.asp
Related Links:
Validate HTML @
http://validator.w3.org
Obtaining Your Users Browser Information @
http://www.4guysfromrolla.com/webtech/121400-1.shtml
Detect Browser @
http://www.learnasp.com/learn/bc.asp
Fix HTML browser problems @
http://www.w3.org/People/Raggett/tidy/
HTML Validator @
http://www.htmlvalidator.com/
AOL Coding @
http://webmaster.info.aol.com/
the BEST browser detection component @
http://www.browserhawk.com
AOL Coding @
http://www.aolserver.com/
Update Browscap.ini @
http://www.learnasp.com/learn/bcdetails.asp
Dynamic Duo Cross-Browser DHTML/Jscript @
http://www.dansteinman.com/dynduo/
Recommended Books:
BrowserHawk detects:
- disabled cookies
- disabled JavaScript
- Flash / plugins
- screen size, more!
FREE download![]()
![]()
![]()
Related Lists:
Evolt.org lists @
http://lists.evolt.org/
Good Javascript List @
http://www.mountaindragon.com/javascript/
[aspClient] - DHTML, frames, active-x, etc. @
http://www.asplists.com/asplists/aspclient.asp
Flash + ASP @
http://www.asplists.com/asplists/aspflash.asp
Rules:
Mailing List Manners (suggested by Bob Filipiak) @
http://db.tidbits.com/getbits.acgi?tbser=1141
Ken Shaffer's Advice on Asking For Help... @
http://www.adopenstatic.com/personal/help.asp
State Management
State Management Introduction (stateintro.asp) - Page 34
What are ASP Sessions? (sessionswhat.asp) - Page 35
Application Data (sessionsapps.asp) - Page 36
Application Data: Worlds Fastest ListBox (speedappdata.asp) - Page 37
XML, Database Caches - Fast Retrieval (xmlfastlist.asp) - Page 38
Say No To Databases w/Sessions or Application scope (nodbsession.asp) - Page 39
Session Overview & Myths (sessionoverview.asp) - Page 40
Sessions: Global.asa and Scalability (globalproblems.asp) - Page 41
Sessions: Global.asa Events (global.asp) - Page 42
Global.asa, Sessions, Custom Stats Resources (statemore.asp) - Page 43
State Methods: Pros and Cons (stateproscons.asp) - Page 44
Pass Data w/Hidden Fields (hidden.asp) - Page 45
Pass Data w/Cookies (cookies.asp) - Page 46
Pass Data w/Session Vars (statesessions.asp) - Page 47
Pass Data w/ID tied to database (statedb.asp) - Page 48
[aspStateManagement] Listserver (aspstatemanagement.asp) - Page 49
State Management Intro by Charles Carroll
Anyone using a browser is accessing the web via HTTP. HTTP is a stateless protocol. What that means is every page fetched from the website has NO MEMORY of the last page fetched.
Whenever we devise some "tricks" so that a web page seems to remember some previous information we call that maintaining state with the user.
Keep in mind no matter how we do this there are limits. Each method may not work in some narrow situation.
A listserver exists where this is the ONLY topic
discussed, see:
http://www.asplists.com/asplists/aspstatemanagement.asp
What are Sessions?
Sessions are a very convenient ASP feature. When someone visits a web page on your site, ASP calls that a "session" and immediately can differentiate that user from all other users at a site. Anything stored in that user's session can be retrieved and manipulated from that page and the next pages they visit, and the data will be tied to that user.
Session data is generally attached to one user. When a user visits their first page of your site, that page and every page they visit is collectively called a session. Any data attached stored in that session object is private to the pages that user is visiting.
The code to store data in a session variable is simple. Here we will allow a user to flip a coin, i.e. flipcoin.asp and count their successes:
1 <%
2 response.write "Coin Tossed!<br>"
3 randomize
4 randomnum=int(rnd*2)+1
5 IF randomnum=1 THEN
6 session("heads")=session("heads")+1
7 ELSE
8 session("tails")=session("tails")+1
9 END IF
10 response.write "Heads= " & session("heads") & "<br>"
11 response.write "Tails= " & session("tails") & "<br>"
12 %>
Even though there are many people at the site they all have different scores for their "heads" and "tails" count. They each has a session and it co-ordinates and differentiates their values.
A much more practical example could
protect access to a page based on a session variable that indicated their
security level determined once upon login, see:
http://www.learnasp.com/learn/security.asp
Some basic things should be noted:
Session data is stored on the server, not in cookies. No user could examine the session cookie and determine the contents of any session variables.
A cookie is used to co-ordinate the user's session ID. Once again the cookie contains no data (just the session ID). This means if the user accepts no cookies, you can't use sessions as described here.
If you absolutely need sessions without client cookies, installing an ISAPI filter named "Cookie Munger" will solve your problem, but at a performance penalty.
http://msdn.microsoft.com/workshop/server/toolbox/cookie.asp
Application Variables by Charles Carroll
There are two kinds of data that your program can manipulate that can be used to "remember" data:
Session data
which is
"attached" to a person browsing your site. If the same page is
accessed by 12 different users each user may have totally different session
values.
Application data
which is
attached to the webserver and is the same no matter which user is accessing
the site.
Application values are visible to every user. But since, unlike session data, any web page could change the application's data there is a potential concurrency issue. The lock and unlock method of the application object eliminate concurrency issues. Once an application is locked, no other updates to the application object can occur until the unlock is executed.
One
use for an application variable would be to store variables most scripts on a
site needed to access or even HTML cached from databases like in
The Worlds Fastest Listbox Example @
http://www.learnasp.com/learn/speedappdata.asp
One example I use to illustrate the conceptual use for each type of data would be a website that simulated a casino. Player's individual winnings make perfect sense to maintain in session variables. However, the total number of players at each "virtual table" (blackjack, roulette, etc.) would be application data as the would be the same regardless of an individual player's status.
Here is a file called appblackjacklook.asp that displays how many people are at the table.
1 <%
2 response.write "Over at the BlackJack Table<br>"
3 response.write "There are " & application("bjplayers") & " players there!<br>"
4 %>
Here is a file called appblackjackarrive.asp that could be included in any script where someone arrived at the blackjack table!
1 <%
2 response.write "Welcome to the BlackJack Table<br>"
3 application.lock
4 application("bjplayers")=application("bjplayers")+1
5 application.unlock
6 response.write "There are " & application("bjplayers") & " players here!<br>"
7 %>
Here is a file called appblackjackleave.asp that could be included in any script where someone left the blackjack table!
1 <%
2 response.write "Thanks for playing BlackJack!<br>"
3 application.lock
4 application("bjplayers")=application("bjplayers")-1
5 IF application("bjplayers")<0 THEN
6 application("bjplayers")=0
7 END IF
8 application.unlock
9 response.write "There are " & application("bjplayers") & " players still at the table!<br>"
10 %>
Worlds Fastest Listbox
w/Application Data
by Charles Carroll
Sometimes data (like a HTML list box) is displayed on many pages of a website. In fact, the database generated list box is displayed thousands of times a day, and the database is queried every time, but it is unnecessary. The database it is drawn from is not changing thousands of times a day.
In the following example any page that displays the list boxes needs to only access the application variables, not hit the database. Very speedy. If the data changes or gains new records, a trigger mechanism could be added to sense data changes and only rebuild the list box if records were added or changed. Here is a demo script that displays the listboxes without re-querying the database.
ListMakedemo.asp is the main script. Simple enough.
1 <HTML>
2 <TITLE>listmakedemo.asp</TITLE>
3 <body bgcolor="#FFFFFF">
4 <br>
5 City: <!--#include virtual="/learn/test/listcity.asp"-->
6 <br>
7 State: <!--#include virtual="/learn/test/liststate.asp"-->
8 <br>
9 Zip: <!--#include virtual="/learn/test/listzip.asp"-->
10 </BODY></html>
11
12
13
14
ListCity.asp displays listbox of cities.
1 <!--#include virtual="/learn/test/lib_listmake.asp"-->
2 <%
3 IF application("list_city")="" THEN
4 myDSN="DSN=student;uid=student;pwd=magic"
5 mySQL="select distinct city from publishers"
6 application("list_city")=query2htmlist(mySQL,"cities",myDSN)
7 END IF
8 response.write application("list_city")
9 %>
ListState.asp displays listbox of states
1 <!--#include virtual="/learn/test/lib_listmake.asp"-->
2 <%
3 IF application("list_states")="" THEN
4 myDSN="DSN=student;uid=student;pwd=magic"
5 mySQL="select distinct state from publishers"
6 application("list_states")=query2htmlist(mySQL,"state",myDSN)
7 END IF
8 response.write application("list_states")
9 %>
ListZip.asp displays listbox ofzips.
1 <!--#include virtual="/learn/test/lib_listmake.asp"-->
2 <%
3 IF application("list_zips")="" THEN
4 myDSN="DSN=student;uid=student;pwd=magic"
5 mySQL="select distinct zip from publishers"
6 application("list_zips")=query2htmlist(mySQL,"zip",myDSN)
7 END IF
8 response.write application("list_zips")
9 %>
10
Lib_Listmake.asp is a library that makes it easier to make listboxes.
1 <%
2 function query2htmList(myquery,myname,myDSN)
3 dim conntemp, rstemp
4 set conntemp=server.createobject("adodb.connection")
5 conntemp.open myDSN
6 set rstemp=conntemp.execute(myquery)
7 query2HTMlist="<Select name='" & myname & "'>"
8 do until rstemp.eof
9 thisfield=trim(RStemp(0))
10 if isnull(thisfield) or thisfield="" then
11 ' ignore
12 else
13 query2HTMlist=query2HTMlist & "<option>" & thisfield & "</option>"
14 end if
15 rstemp.movenext
16 loop
17 query2HTMlist=query2HTMlist & "</select>"
18 rstemp.close
19 set rstemp=nothing
20 conntemp.close
21 set conntemp=nothing
22 end function
23 %>
ListMakeClear.asp is a crude mechanism to force all the listboxes to refresh. It could be invoked on a timed basis (every 15 minutes for example) or triggered by data-changes.
1 <%
2 application("list_city")=""
3 application("list_states")=""
4 application("list_zips")=""
5 %>
Worlds Fastest Listbox with XML,
Application Variables
by Jamiel Humayun Jhumayun@liveperson.com
and Ian Payne ivpayne@wagga.fsnet.co.uk
Sometimes data (like a HTML list box or table) is displayed on many pages of a website. In fact, the database generated list box is displayed thousands of times a day, and the database is queried every time, but it is unnecessary. The database it is drawn from is not changing thousands of times a day.
In the following example any page that displays the database needs to only transform the XML application variables, not hit the database. Very speedy. If the data changes or gains new records, a trigger mechanism could be added to sense data changes and only rebuild the list box if records were added or changed. Here is a demo script that displays the listboxes without re-querying the database.
DisplayCity.asp is the main script. Simple enough.
1 <!--#include file="XMLLibrary.asp"-->
2 <html>
3 <head>
4 <title>Display XMLized Listbox</title>
5 </head>
6 <body bgcolor="#FFFFFF">
7 <b>City Table:</b> <br>
8 <% DisplayCityTable() %><br><br>
9 <b>City List:</b> <br>
10 <% DisplayCityList() %><br><br>
11 </body>
12 </html>
ListBox.xsl is the style sheet to give it a list box look
1 <?xml version="1.0"?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
3 <xsl:template match="/">
4 <select>
5 <xsl:for-each select="xml/rs:data/z:row">
6 <option><xsl:value-of select="@city"/></option>
7 </xsl:for-each>
8 </select>
9 </xsl:template>
10 </xsl:stylesheet>
Table.xsl is the style sheet to give it a list box look
1 <?xml version="1.0"?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
3 <xsl:template match="/">
4 <table cellpadding="2" cellspacing="0" border="1" width="100%">
5 <xsl:for-each select="xml/rs:data/z:row">
6 <tr><td><xsl:value-of select="@city"/></td></tr>
7 </xsl:for-each>
8 </table>
9 </xsl:template>
10 </xsl:stylesheet>
XMLlibrary.asp is the library that does most of the work.
1 <%
2 adPersistXML=1
3
4 Function GenXML (objRs)
5 set stmXML = CreateObject("ADODB.Stream")
6
7 If Not objRs.EOF Then
8 objRs.Save stmXML, adPersistXML ' needs ADO 2.5
9 End If
10 GenXML = stmXML.ReadText
11 Set stmXML = Nothing
12
13 End Function
14
15 Function GetCityXML ()
16 ' Database connection - Usually kept in an include file, but presented for display purposes
17 strConnection = "DSN=student;UID=student;PWD=magic;"
18 Set objConn = Server.CreateObject ("ADODB.Connection")
19 objConn.Open strConnection
20 strSQL = "SELECT DISTINCT city FROM publishers"
21 Set objRs = objConn.Execute (strSQL)
22 GetCityXML = GenXML(objRs)
23 Set objConn = Nothing
24 Set objRs = Nothing
25 End Function
26
27 Function RenderXML (strXML,strStyleSheet)
28 Set objXML = CreateObject("MSXML.DOMDocument")
29 Set objXSL = CreateObject("MSXML.DOMDocument")
30 objXML.loadXML(strXML)
31 objXSL.load(Server.MapPath(strStyleSheet))
32 Response.Write objXML.transformNode (objXSL)
33 Set objXML = Nothing
34 Set objXSL = Nothing
35 End Function
36
37 Sub DisplayCityTable()
38 if Application("City") = "" then Application("City") = GetCityXML()
39 strStyleSheet = "table.xsl"
40 Call RenderXML (Application("City"),strStyleSheet)
41 End Sub
42
43 Sub DisplayCityList()
44 if Application("City") = "" then Application("City") = GetCityXML()
45 strStyleSheet = "listbox.xsl"
46 Call RenderXML (Application("City"),strStyleSheet)
47 End Sub
48
49 %>
XMLcacheclear.asp is called to force a fresh database display.
1 <%
2 Application("City") = ""
3 %>
Databases and Sessions....
Just Say No!
by Charles
Carroll
Many new ASP programmers and some who even call themselves experienced aspire or accomplish placing recordsets and/or applications in session and/or application variables. They do it to speed things up but actually ensure that the site will perform slower as it gets busier.
Putting a recordset or application into application or session scope leads to:
What works for few users will not for many
Imagine how fast and convenient a Porsche is when driving to Las Vegas with your significant other. Must be the best way to get there... Right? Now imagine a bunch of seniors want to go. 200 seniors x 100 trips in the porsche. Ouch! The tour bus or an airplane scales. The porsche does not. Developers should always design for scaling not minimal users.

Application Level Connections
Mechanically, creating one Application level variables with a database connection is simple.
SUB application_onstart
set application("theCONN")=server.createobject("adodb.connection")
END SUB
Then any code that wants to use that connection
might look like this:
<%
mySQL="select * from publishers where state='NY'
set rstemp=application("theconn").execute(mySQL)
%>
However this code will not scale well. The code is mechanically easy, but the
scripts using this code will suffer from Thread-Affinity and Serialization.
An application variable lives until a web server shuts down. It also lives on a specific thread; we would say the object has thread affinity. Any script utilizing that object must live on same thread, as that object or make an expensive marshalled call to talk to that object.
Thread Affinity
Thread affinity is bad. If a request comes in it would be fast if any thread could perform the task. Thread affinity guarantees only 1 thread can service the request and if that thread is busy the task waits in line for that thread, despite other threads who may be able to service the request.
http://www.learnasp.com/advice/threads.asp
explains threading fundamentals.
Serialization is an enemy of Scalability
If multiple tasks all have affinity to the same thread they become serialized, that is to say they all run in sequence. As a real-world analogy for serialization imagine you wanted to buy a meal at a fast-food cashier and the person in front of you ordered 150 sandwiches for their swim-team. You don't get your 1 sandwich until they finish their order. 6 request for 150 records from a database means the fourth request for 1 record comes after those 900 are retrieved. Without thread affinity the requests could be divided among available threads.
Conclusion: All recordsets and database access would have some level of thread affinity if one connection only is made in global.asa. The side-effect would be recordsets that were forced to specific threads and serialized requests effectively slowing a site's overall performance and for specific scripts.
Recordsets stored at Sessions Level...
Mechanically, this is easy to accomplish:
SUB session_onstart
set session("rstemp")=server.createobject("adodb.recordset")
END SUB
Then any code that wants to use that connection
might look like this:
<%
...
mySQL="select * from publishers where state='NY'
set session("rstemp")=conntemp.execute(mySQL)
%>
However this code will not scale well. The code is mechanically easy, but the
scripts using this code will suffer from Thread-Affinity, Serialization, and
there will always be more recordsets than users accessing a site. In fact a more
aggregious version of Thread Affinity occurs which I will nickname
Thread-Locking.
Thread Affinity Part #2
In IIS3 and IIS4 once a user is assigned a session object (recordset or VB5/VB6 component) with thread affinity every script run in that session is forced onto the thread the first script is assigned. It effectively handcuffs every user's script to the thread their first script was assigned. This occurs because recordsets and VB5 and VB6 need to store their data on a specific thread and until destroyed (unlike C++ components which specifically aggregate the Free Thread marshaller) the objects may never be moved to a different thread which hurts performance.
More Recordsets than Users??? Please explain...
If for example a mere 20 users who access 1 page each and no others at tour site, their session objects (in this case recordsets) persist for the default session timeout (20 minutes unless the registry is modified or a page explicitly sets it using the session.timeout command. If then 50 users access several scripts and 20 of them go lunch or go home for the day, the recordsets will waste memory until their session times out. In this example there would be 90 recordsets and 30 active users within minutes. How can any scenario where significantly more resources are allocated than are being used be efficient?
http://www.learnasp.com/learn/globalproblems.asp
explains a typical scenario.
Create and Destroy on Every Page
Even though every page creates and destroys connections, the newly created ones can execute on any thread. Also because of round-robin execution resources are used sparingly and connections are never open too long.
Make Sure Connection Pooling is in Effect
Connection Pooling can speed up sites. ADO invisibly can co-ordinate that any destroyed connections are not instantly destroyed, instead they are left ready to be grabbed by the next page who requests creating a connection. That page's code has a create request that gets intercepted at a low-level and given an already created connection ready to do database access. The programmers code then is not being executed literally; their create requests often get serviced by already created connections.
http://www.learnasp.com/learn/dbpooling.asp
has more information and details.
What is the fastest way then (if I want fast, scalable database displays)?
"The Worlds Fastest Listbox"
http://www.learnasp.com/learn/speedappdata.asp
shows a lightning fast way to display databases.
GetString to speed up Data Transfer
http://www.learnasp.com/learn/dbtablegetstring.asp
GetRows to speed up Data Transfer
http://www.learnasp.com/learn/dbtablegetrows.asp
All we discuss is fast code over at:
http://www.asplists.com/asplists/aspfastcode.asp
What If I am willing to pay the price?
Make sure you test the load using tools like the
Web Stress Tool @
http://homer.rte.microsoft.com
Much more information on scalability can be found
on
http://www.asplists.com/asplists/aspscalability.asp
which has links and a signup for an excellent 2 way listserver.
Session Overview & Myths by Charles Carroll
Session data is greatly misunderstood. Sessions themselves were a subtle and complex issue, but the subject was confused considerably by bad information people gathered from code others made that misused sessions. It is also confused by anecdotal performance evidence when a site is small, or the testing is only done within simple stress tests that don't reveal all the speed issues. We will clarify it all now.
A couple analogies may help. A Porsche seems really fast to get anywhere (of course we assume you have 2 passengers) until you have 3-10 passengers. Then a mini-van will beat it because you have less trips to make. In client-server terms the Porsche doesn't SCALE WELL for more than 2 passengers. On the other hand, when a group of 100 wants to go to Atlantic city for the weekend we recommend a Tour Bus. However, someone taking a Tour Bus to the grocery store has anecdotal evidence it is not as fast as a Porsche.
Fact #1: When a browser window closes, the session DOES NOT end.
Fact #2: <%session.abandon%> command can end a session.
Fact #3: Another way a
session ends is when a user has not visited any page within that
site/application with ___ minutes. The default is 20 minutes of
inactivity. The following script can show what the settings are on your server:
1 <html><head>
2 <TITLE>sessionsettings.asp</TITLE>
3 </head>
4 <body bgcolor="#FFFFFF">
5 <%
6 response.write "Session Timeout=" & session.timeout & "minutes <br>"
7 %>
8 </body></html>
Fact #4: Session ids are not guaranteed to be different anytime a new session is generated. If there are 1,000 sessions there will be 1,000 unique session ids. But if 200 people loose session (due to timeout or explicit .abandon) and 150 new sessions are begun ASP may and will certainly use the same session IDs it was using earlier.
Myth #1: Storing large objects (recordsets, database data, objects) users access in sessions saves memory*.
No. No. No. Since sessions start when users access one page and don't end until 20 minute after they access the last page. Think about it. If 200 news persons a minute hit your site for 5 minutes that is 1,000 sessions and appropriately 1000 x the memory consumed for session variables. If 500 people go away, you still have 1000 sessions and 500 users (twice as much memory is consumed as needed) until the server detects 20 minutes of inactivity for those 500 users who are not on the site.
Application variables (not
session variables) can be used this way without wasting memory. They could be to store variables most scripts on a
site needed to access or even HTML cached from databases like in The Worlds Fastest Listbox Example @
http://www.learnasp.com/learn/speedappdata.asp
Myth #2: Storing large objects (recordsets, database data, objects) users access in sessions speeds up access.
No. No. No. Objects in sessions have several potential speed barriers depending on their memory model.
Thread local storage is used internally in some objects (notably VB components). This means once you assign such a component to a session variable attached to that user all user request must remain on the thread they started on. If they for example were assigned Thread #3 upon their session start as they access many pages at the site all their activity will remain on thread #3. I nickname this phenomena Thread Locking.
Free-threaded objects have no speed barriers (though as Myth #1 states, you will always have too many in memory if you store objects as session variables).
Apartment-Threaded objects, notably VB components and the Microsoft Access databas driver which are Single-Threaded apartments (STA) have some performance limits. Simply put, every user request to a STA object must be serialized.
Serialization explained: If 100 users hit the site their use of that code is in sequence. If all the users retrieves 10-20 records from a database you might notice no effect. But if person 3 retrieves 2,000 records, person 4 retrieving 2 records will occur after the person 3 retrieves 2,000 records. OUCH!!!!! Person 4 will think the webserver is very slow only retrieving 2 records.
Free-Threading explained: Users execution is more round-robin like where the webserver does not have to finish each users request before moving to another user. The code may be able to move to User 4 and grab their records and then person 5 and later finish person 3's large request.
Threads explained: Your web server spawns threads (4 per CPU is the default in IIS4; it should be adjusted to 20 see http://www.learnasp.com/learn/speedserver.asp. If your webserver had for example 4 threads, then 1000 users might be 250 per thread or 700 on one thread and 100 each on the other threads. The latter is a severe imbalance as one thread is overworked (thus slower) while other threads are underworked.
Global.asa Overkill by Charles Carroll
A global.asa file is a used extraneously and quite wastefully, but can be a very useful tool. First let us bring out what is good and bad about it.
Application variables are good. Since there is only one application variable in memory, no matter how many users are on your site, they can be a convenient place to store central information and retrieve it fast. Since there is only one application variable in memory regardless of the number of users at your site, remember the following characteristics:
Session variables (particularly COM objects put in a session variable) can:
Session Variable Constraints
Session variables containing simple variables (texts, numbers, dates, NOT COM objects) are not so wasteful as to be prohibitive. BUT remember, if you write any script that depends on session variables you are working under the following assumptions (and if these restrictions are fine, then use sessions as much as you need to):
If the task could be accomplished with hidden fields instead of session variables, then those limitations are not in effect -- and most tasks can.
Good uses of global.asa and session data include:
Examples of Wasteful Code and Alternatives...
Code that is particularly wasteful is code that places database connection info in the global.asa, i.e.
session_onstart
session("dbname")="DSN=employees;"
session("dbuser")="whoever"
session("dbpass")="majic"
end sub
and the script that goes with it looks like this:
... code ....
set conntemp=server.createobject("adodb.connection")
conntemp.open session("dbname") & "uid=" &
session("dbuser") & ";pwd=" & session("dbpass")
... code ....
Why? Do 700 users hitting that site hit 700 different databases with 700 different userids and passwords at the database level. Just 700 wasted session variables...Session variables are the worst way to keep such data; application variables the best or other mechanisms. Unfortunately Visual Interdev grossly misuses sessions and creates this bizzare concept in people's brains.
If for example, 700 users connect to a page using session variables to store DSN info, etc. You have 700 DSN variables in memory all with the same value. The purpose of sessions is to have separate data for users not the same data replicated for every session. Plus it means the site is unusable to a user not accepting cookies unnecessarily. It is really necessary to require cookies to display a database page where the DSN to the database does NOT change over a multi-month/year period?
The better alternatives would be:
Include Files (cheap and easy)
lib_connection.asp
<%
dbname="DSN=employees;"
dbuser="whoever"
dbpass="majic"
%>
and the script that goes with it looks like this:
<!--#include virtual="/lib_connection.asp"-->
Application Data (involves a COM object, but is memory cheap)
application_onstart
application("dbname")="DSN=employees;"
application("dbuser")="whoever"
application("dbpass")="majic"
end sub
and the script that goes with it looks like this:
... code ....
set conntemp=server.createobject("adodb.connection")
conntemp.open application("dbname") & "uid=" &
application("dbuser") & ";pwd=" &
application("dbpass")
... code ....
In both approaches, if 700 users hit the site, no user specific variables are created. The same three variables are available to all users with no wasted memory.
Global.asa Events by Charles Carroll
This page demonstrates what a blank global.asa should look like:
1 <script language=vbscript runat=server>
2 SUB Application_OnStart
3 END SUB
4
5 SUB Application_OnEnd
6 END SUB
7
8 SUB Session_OnStart
9 END SUB
10
11 SUB Session_OnEnd
12 END SUB
13 </script>
A global.asa code is divided into four events, or segments:
session_onstart
if 100 users hit your website for example, 100 session starts are initiated and the code
in this event is fired for each user before their first page is fetched and displayed. But
those same 100 users can wander all over your site and another session start will not
occur as long as they are actively fetching pages
session_onend
Any user that does not fetch any page from your site for 20 minutes (can be adjusted in
the Registry/IIS3 or the metabase/IIS4) has ended their session. The code you put in this
event can not affect the users as they have already left your site but it can allow you to
place cleanup code or code that dumps for, example, session data into a database.
application_onstart
The webserver starts. dozens to hundreds to thousands of users hit the site. The minute
the first user fetches the first file in an application directory an application_onstart
occurs and never occurs again no matter how many people are visiting. But if a
webserver stops and starts and application_onstart fires. But code inside an
application_onstart is code that assumes the entire casino is a "blank slate"
not a busy place. If for example, application_onstarts fire often this is not a desired
behavior since it may have the role of bootsrapping an app and setting certain conditions
that don't make sense to set while activity is occuring.
application_onend
The web server stops, the application ends.
Global.asa, Sessions, Custom Stats Resources
The global.asa, application and sesion variables are certainly a more complex and controversial subject than most ASP topics. Other sites and my site has a lot to say on this:
Everything you wanted to know about global.asa
but were afraid to ask @
http://www.4guysfromrolla.com/webtech/113098-1.shtml
The lowdown on Global.asa, session vars and app.
vars
http://www.asp101.com/resources/apps_sessions_gasa.asp
An example of home-brewed stats:
http://www.asp101.com/resources/active_users.asp
Steve Smith's Stats examples:
http://www.aspalliance.com/stevesmith/samples/whosoncode.asp
http://www.aspalliance.com/stevesmith/samples/sitestats.asp
State Management Methods, Pros and Cons by Charles Carroll
Several methods exist to maintain state. We will present a high-level summary here with advantages and drawbacks.
Method #1 Cookies
Pros: can be set for long periods of time. Wastes no server memory.
Cons: set on a machine. If user visits another machine their info is not there. If someone sits down at users machine, sites they visit may read cookie and it is NOT same person. Users may disable cookies.
Method #2 ASP Sessions
Pros: requires less lines of code than cookies. Easy to program.
Cons: wastes server memory. Evaporates and must be placed in durable storage if user changed session values. User who disable cookies can't have sessions.
Method #3 Hidden Fields
Pros: Works if user has disabled cookies.
Cons: adds Lots of code to each page. If someone invents a page that is not in your website and studies the HTML (where hidden fields are visible) they could feed incorrect hidden data to your form submits (negative numbers into shopping carts, $0 prices into shopping carts, etc.)
Method #4 Hidden Fields + Database
Pros: Works if user has disabled cookies.
Cons: adds Lots of code to each page. Since the hidden field is used to do a database retrieve, each page must hit a database.
Method #5 Homebrewed Sessions
Pros: Solves the problems of traditional ASP sessions.
Cons: Must build component or buy 3rd Party solutions (SASession from www.AspStudio.com for example).
Every site uses one or more of these methods to maintain state.
Passing Data with Hidden Fields by Charles Carroll
This page demonstrates how to have several pages that are forms yet after all pages are filled out the final form has access to all inputs without session variables.
surveypage1.asp asks the user the first questions:
1 <html><head>
2 <TITLE>surveypage1.asp</TITLE>
3 </head>
4 <body bgcolor="#FFFFFF">
5 <form action="surveypage2.asp" method="post">
6 First Name<br>
7
8 <input type="text" name="first" size="20"><br>
9 Last Name<br>
10
11 <input type="text" name="last" size="20">
12 <p>
13 <input type="submit" value="Next Question ->"></p>
14 </form>
15 </body></html>
surveypage2.asp asks the user the next questions:
1 <html><head>
2 <TITLE>surveypage2.asp</TITLE>
3 </head>
4 <body bgcolor="#FFFFFF">
5 <form action="surveypage3.asp" method="post">
6 <%
7 first=request.form("first")
8 last=request.form("last")
9 %>
10 Hair Color<br>
11
12 <input type="text" name="haircolor" size="20"><br>
13 Favorite Color<br>
14
15 <input type="text" name="favoritecolor" size="20">
16 <p>
17 <input type="hidden" name="first" value="<%=first%>">
18 <input type="hidden" name="last" value="<%=last%>">
19 <input type="submit" value="Next Question ->"></p>
20 </form>
21 </body></html>
surveypage3.asp asks the user yet some more questions:
1 <html><head>
2 <TITLE>surveypage3.asp</TITLE>
3 </head>
4 <body bgcolor="#FFFFFF">
5 <%
6 first=request.form("first")
7 last=request.form("last")
8 haircolor=request.form("haircolor")
9 favoritecolor=request.form("favoritecolor")
10 %>
11 <form action="surveypage3respond.asp" method="post">
12 Street Address<br>
13
14 <input type="text" name="street" size="20">
15 <br>
16 City<br>
17
18 <input type="text" name="city" size="20"><br>
19 State<br>
20
21 <input type="text" name="state" size="20">
22 <br>
23 Zip<br>
24
25 <input type="text" name="zip" size="20">
26 <br>
27
28
29 <input type="hidden" name="first" value="<%=first%>">
30 <input type="hidden" name="last" value="<%=last%>">
31 <input type="hidden" name="haircolor" value="<%=haircolor%>">
32 <input type="hidden" name="favoritecolor" value="<%=favoritecolor%>">
33
34 <input type="submit" value="Final Step ->">
35 </form>
36 </body></html>
surveypage3respond.asp gathers all the answers and responds:
1 <html><head>
2 <TITLE>surveypage3respond.asp</TITLE>
3 </head>
4 <body bgcolor="#FFFFFF">
5 <%
6 first=request.form("first")
7 last=request.form("last")
8 haircolor=request.form("haircolor")
9 favoritecolor=request.form("favoritecolor")
10 street=request.form("street")
11 city=request.form("city")
12 state=request.form("state")
13 zip=request.form("zip")
14 %>
15 Thanks for all your information<br>
16 We are happy to meet you <%=first%> <%=last%><br>
17 We know your hair color is <%=haircolor%><br>
18 and your favorite color is <%=favoritecolor%><br>
19 and we will ship all items to<br>
20 <%=first%> <%=last%><br>
21 <%=street%><br>
22 <%=city%> <%=state%> <%=zip%>
23 </body></html>
Passing Data with Cookies by Charles Carroll
This page demonstrates how to have several pages that are forms yet after all pages are filled out the final form has access to all inputs even after the browser is opened and closed. The browser must accept cookies.
surveypage1c.asp asks the user the first questions:
1 <html><head>
2 <TITLE>surveypage1.asp</TITLE>
3 </head>
4 <body bgcolor="#FFFFFF">
5 <%
6 first=request.cookies("prefs")("first")
7 last=request.cookies("prefs")("last")
8 %>
9 <form action="surveypage2c.asp" method="post">
10 First Name<br>
11
12 <input type="text" name="first" size="20" value="<%=first%>"><br>
13 Last Name<br>
14
15 <input type="text" name="last" size="20" value="<%=last%>">
16 <p>
17 <input type="submit" value="Next Question ->"></p>
18 </form>
19 <%
20 For Each cookie in Response.Cookies
21 Response.Cookies(cookie).Expires = now()+365
22 Next
23 %>
24 </body></html>
surveypage2c.asp asks the user the next questions:
1 <%response.buffer=true%>
2 <html><head>
3 <TITLE>surveypage2c.asp</TITLE>
4 </head>
5 <body bgcolor="#FFFFFF">
6 <%
7 first=request("first")
8 last=request("last")
9 haircolor=request.cookies("prefs")("haircolor")
10 favoritecolor=request.cookies("prefs")("favoritecolor")
11 %>
12 <form action="surveypage3c.asp" method="post">
13 Hair Color<br>
14
15 <input type="text" name="haircolor" size="20" value="<%=haircolor%>"><br>
16 Favorite Color<br>
17
18 <input type="text" name="favoritecolor" size="20" value="<%=favoritecolor%>">
19 <p>
20 <%
21 response.cookies("prefs")("first")=first
22 response.cookies("prefs")("last")=last
23 For Each cookie in Response.Cookies
24 Response.Cookies(cookie).Expires = now()+365
25 Next
26 %>
27 <input type="submit" value="Next Question ->"></p>
28 </form>
29 </body></html>
surveypage3c.asp asks the user yet some more questions:
1 <%response.buffer=true%>
2 <html><head>
3 <TITLE>surveypage3c.asp</TITLE>
4 </head>
5 <body bgcolor="#FFFFFF">
6 <%
7 haircolor=request("haircolor")
8 favoritecolor=request("favoritecolor")
9 street=request.cookies("prefs")("street")
10 city=request.cookies("prefs")("city")
11 state=request.cookies("prefs")("state")
12 zip=request.cookies("prefs")("zip")
13 %>
14 <form action="surveypage3crespond.asp" method="post">
15 Street Address<br>
16
17 <input type="text" name="street" size="20" value="<%=street%>">
18 <br>
19 City<br>
20
21 <input type="text" name="city" size="20" value="<%=city%>"><br>
22 State<br>
23
24 <input type="text" name="state" size="20" value="<%=state%>">
25 <br>
26 Zip<br>
27
28 <input type="text" name="zip" size="20" value="<%=zip%>">
29 <br>
30 <%
31 response.cookies("prefs")("haircolor")=haircolor
32 response.cookies("prefs")("favoritecolor")=favoritecolor
33 For Each cookie in Response.Cookies
34 Response.Cookies(cookie).Expires = now()+365
35 Next
36 %>
37 <input type="submit" value="Final Step ->">
38 </form>
39 </body></html>
surveypage3crespond.asp gathers all the answers and responds:
1 <%response.buffer=true%>
2 <html><head>
3 <TITLE>surveypage3respond.asp</TITLE>
4 </head>
5 <body bgcolor="#FFFFFF">
6 <%
7 first=request.cookies("prefs")("first")
8 last=request.cookies("prefs")("last")
9 haircolor=request.cookies("prefs")("haircolor")
10 favoritecolor=request.cookies("prefs")("favoritecolor")
11 street=request("street")
12 city=request("city")
13 state=request("state")
14 zip=request("zip")
15 %>
16 Thanks for all your information<br>
17 We are happy to meet you <%=first%> <%=last%><br>
18 We know your hair color is <%=haircolor%><br>
19 and your favorite color is <%=favoritecolor%><br>
20 and we will ship all items to<br>
21 <%=first%> <%=last%><br>
22 <%=street%><br>
23 <%=city%> <%=state%> <%=zip%>
24 <%
25 response.cookies("prefs")("street")=street
26 response.cookies("prefs")("city")=city
27 response.cookies("prefs")("state")=state
28 response.cookies("prefs")("zip")=zip
29 For Each cookie in Response.Cookies
30 Response.Cookies(cookie).Expires = now()+365
31 Next
32 %>
33 </body></html>
Passing Data with Sessions by Charles Carroll
These demos are not written yet but show the survey implemented with ASP sessions.
Passing Data with Databases by Charles Carroll
These demos are not written yet but show the survey implemented with a GUID and a request to a database to read and write state values for a user.
Related Links:
Hidden Fields to Pass Data between pages @
http://www.learnasp.com/learn/hidden.asp
Sessions: what are they? @
http://www.learnasp.com/learn/sessionswhat.asp
Cache application level Recordset @
http://msdn.microsoft.com/workshop/server/feature/cache.asp
Disconnected Recordsets in Session Variables by Don Hayward @
http://www.insightgraphics.com/reference/ASPStateMgmt.htm
Global.asa Sclability problems @
http://www.learnasp.com/learn/globalproblems.asp
Session Management without cookies @
http://domaindlx.com/asms/
FREE Dictionary Component that is session/app safe @
http://www.caprockconsulting.com/comsoftware.asp
Databases and Session/App Vars? Say no! @
http://www.learnasp.com/advice/dbsessionapp.asp
4 Ways to Pass data from page to page @
http://www.4guysfromrolla.com/webtech/041399-1.shtml
Cookie Munger - Sessions with no cookies @
http://msdn.microsoft.com/workshop/server/toolbox/cookie.asp
Global.asa Basics @
http://www.learnasp.com/learn/global.asp
Recommended Books:
Related Lists:
Mobile lists by Asplists @
http://www.asplists.com/asplists/mobile.asp
Mobile lists by WROX @
http://p2p.wrox.com/mobile/
Rules:
Mailing List Manners (suggested by Bob Filipiak) @
http://db.tidbits.com/getbits.acgi?tbser=1141
Ken Shaffer's Advice on Asking For Help... @
http://www.adopenstatic.com/personal/help.asp
Forms/Decisions
Forms: Introduction (formintro.asp) - Page 51
Forms: Text Box (formtextbox.asp) - Page 52
Forms: Text Area (formtextarea.asp) - Page 53
Forms: Check Box (formcheckbox.asp) - Page 54
Forms: Radio Buttons (formradio.asp) - Page 55
Forms: List Box (formlistbox.asp) - Page 56
Forms: CASE syntax #1 (case.asp) - Page 57
Forms: CASE syntax #2 (case2.asp) - Page 58
Forms: IF syntax #1 (if.asp) - Page 59
Forms: IF syntax #2 (if2.asp) - Page 60
Forms: IF syntax #3 (if3.asp) - Page 61
Forms: IF syntax #4 (if4.asp) - Page 62
Forms: For Each Iteration (formforeach.asp) - Page 63
Form - Submit To Self' (formsubmitself.asp) - Page 64
Form - Change Action on Fly (formactionchange.asp) - Page 65
Forms Introduction by Charles Carroll
Forms are the primary way that a user feeds information into ASP. A form is a web page that contains tags that cause the browser to show fields that the user can fill in.
Form with GET
<form action="x.asp" name="whatever" method=get>
....
<input type=submit>
<input type=reset>
</form>
Form with POST
<form action="x.asp" name="whatever" method="post">
....
<input type=submit>
<input type=reset>
</form>
Tips:
Tip #1: If using response.redirect to simulate a GET don't forget to encode:
/learn/encode.aspTip #2: A text link can be used intead of a button to post above form would look like this:
<a href="javascript:document.yourformname.submit();">
send data</a>
Forms - Text Box (by John Kauffman & Charles Carroll)
| <INPUT NAME="NameLast"> | This will create an input box of a default size and the browser will pass the user input to ASP with the label (identifier) of NameLast. |
| <INPUT NAME="ZipCode" SIZE="10"> | This is not a limit to the number of characters that can be entered. Do not use size as a validation technique to limit verbose users. |
| <INPUT NAME="State" MaxLength="2"> | This controls the maximum number of characters that can be entered. |
| <INPUT NAME="NameLast" VALUE="Bertrand"> | The name of Bertrand will appear when the page is opened and will re-appear if the form is reset. |
1 <html><head>
2 <title>FormTextBox.asp</title>
3 </head><body bgcolor="#FFFFFF">
4 <Form action = "FormTextBoxRespond.asp" method="get">
5 Fill Out This Form For Us:<p>
6 Last Name -> <Input NAME="NameLast" size ="10"><br>
7 Country -> <Input NAME="Country" value="USA" size="10"><br>
8 State -> <Input NAME="State" MaxLength="2" size="2"><br>
9 <Input type="submit" value="Give me your data!">
10 <hr></form>
11 </body></html>
1 <html><head>
2 <title>FormTextBoxRespond.asp</title>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 lname=request.querystring("namelast")
6 cty=request.querystring("country")
7 st=request.querystring("state")
8
9 response.write lname & "<br>"
10 response.write cty & "<br>"
11 response.write st & "<br>"%>
12 </body></html>
Forms - Text Area by John Kauffman & Charles Carroll
Multi-line text boxes are created using the TEXTAREA command as follows:
<TEXTAREA NAME="UserComments" ROWS=5
COLS=50>
... default text is typed here
</TEXTAREA>
Notes: Windows browsers typically provide scroll bars
automatically.
Although the TEXT tag does not need to be closed, the TEXTAREA must have a
</TEXTAREA>
The TEXTAREA tag (like all of the user-input objects) must be within the form tags
discussed earlier.
1 <html><head>
2 <TITLE>formTextArea.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <form action="FormTextAreaRespond.asp" method="post">
5 <p>TextArea Example</p>
6 <p>Please type your special shipping comments:</p>
7 <TEXTAREA NAME="shippingComments" ROWS="5" COLS="50">
8 shipping comments go here
9 </textarea>
10 <p><input type=submit value="send in comments!">
11 </form>
12 </body></html>
The responder to the form will look like this:
1 <html><head>
2 <TITLE>formTextArearespond.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 comm=request.form("shippingcomments")
6 response.write comm
7 %>
8 <hr>
9 </body></html>
Tip: Want to retain Hard-Returns the User Types?
Then change the form responder:
comm=request.form("shippingcomments")
becomes
comm=request.form("shippingcomments")
comm=replace(comm,vbcrlf,"<br>")
Forms - Check Boxes (by John Kauffman & Charles Carroll)
The checkbox object is coded along the same lines as radio buttons, however each checkbox must get its own unique name since the state of "checked" or "not checked" will be passed to ASP for each box. Remember that you can set more than one option to be checked.
1 <html><head>
2 <TITLE>FormCheckBox.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <form action="FormCheckBoxRespond.asp" method="post">
5 <p>CheckBox Form Example</p>
6 <p>How do you want your order confirmed?</p>
7 <input TYPE="checkbox" NAME="USMail">confirmation sent by first class US Postal Service<br>
8 <input TYPE="checkbox" NAME="UPS">confirmation sent by UPS overnight letter service<br>
9 <input TYPE="checkbox" NAME="EMail" CHECKED>confirmation sent by EMail<br>
10 <input TYPE="checkbox" NAME="Fax">confirmation sent by Fax<br>
11 <input TYPE="checkbox" NAME="Tel" CHECKED>confirmation made by telephone call<br><br>
12 <input type="submit"><input type="reset">
13 </form><hr></body></html>
The responder looks like this:
1 <html><head>
2 <TITLE>formCheckBoxRespond.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 If request.form("USMail")="on" then
6 response.write "<br>We will confirm by US Mail"
7 end if
8 If request.form("UPS")="on" then
9 response.write "<br>We will confirm by UPS"
10 end if
11 If request.form("EMail")="on" then
12 response.write "<br>We will confirm by EMail"
13 end if
14 If request.form("fax")="on" then
15 response.write "<br>We will confirm by fax"
16 end if
17 If request.form("tel")="on" then
18 response.write "<br>We will confirm by tel"
19 end if%>
20 </body>
21 </html>
Forms - Radio Buttons (by John Kauffman)
All of your user input objects in a form must have their own unique name which will be the label that the browser will assign to them when passing the data to ASP. The exceptions are radio buttons. Since only one piece of information will be passed to ASP from a set of radio buttons, they all get the same name. HTML requires that one of the buttons is selected by default, the one indicated by the CHECKED command. It is important to understand that the browser will send back to ASP the name and the value of the one button which is selected by the user. The browser will NOT send back the text which is associated with the button. For the example above, if the user checked on the button with the text New York City, then ASP would receive City=NY.
1 <html><head>
2 <TITLE>formRadio.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <form action="FormRadiorespond.asp" method="post">
5 <p><b>Radio Buttons </b> example</p>
6 <p>Which Regional Office will you be visiting?</p>
7 <input TYPE="radio" NAME="City" VALUE="NY">New York City
8 <input TYPE="radio" NAME="City" VALUE="LA">Los Angeles
9 <input TYPE="radio" NAME="City" VALUE="SV" CHECKED>Savannah
10 <br><input type="submit" value="choose a city">
11 </form>
12 </body></html>
The responder looks like:
1 <html><head>
2 <TITLE>formradiorespond.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%myCity = request.form("City")
5 Select Case ucase(MyCity)
6 case "NY"
7 response.write "New York meeting is on Jan 3"
8 case "LA"
9 response.write "LA meeting meeting is on Jan 15"
10 case "SV"
11 response.write "Savannah meeting is on Jan 20"
12 End Select%>
13 </body>
14 </html>
Forms - List Boxes (by John Kauffman & Charles Carroll)
The listbox object is very popular since it makes data entry easy. They just pull down a list.
1 <html><head>
2 <TITLE>FormListBox.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <form action="FormListBoxRespond.asp" method="post">
5 <SELECT NAME="state">
6 <OPTION SELECTED VALUE="md">Maryland</OPTION>
7 <OPTION value="dc">District of Columbia</OPTION>
8 <OPTION value="va">Virginia</OPTION>
9 <OPTION value="whoknows">Somewhere Else!</OPTION>
10 </SELECT>
11 <input type=submit value="Choose State">
12 </form>
13 </body></html>
The responder looks like this:
1 <html><head>
2 <TITLE>formlistboxrespond.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%mystate = request.form("state")
5 Select Case ucase(mystate)
6 case "DC"
7 response.write "DC meeting on 15th"
8 case "MD"
9 response.write "MD meeting on 20th"
10 case else
11 response.write "No meetings for you to attend!"
12 End Select%>
13 </body>
14 </html>
Select Case (by John Kauffman & Charles Carroll)
Using IF-THEN can be cumbersome, prone to programmer errors and slower to execute. A more efficient construct is SELECT CASE. It is optimized for testing one variable against many conditions.
1 <html><head>
2 <TITLE>case.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <form action="caserespond.asp" method="get">
5 Your First Name<INPUT NAME="FirstName" MaxLength=20><p>
6 Your Last Name<INPUT NAME="LastName" MaxLength=20><p>
7 <INPUT TYPE=submit><p><INPUT TYPE=reset>
8 </form>
9 </body></html>
Here is the select case that will determine what the form input means.
1 <html><head>
2 <TITLE>caserespond.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 fname=request.querystring("Firstname")
6 lname=request.querystring("Lastname")
7 %>
8 Nice to Meet You <%=fname%> <%=lname%><p>
9 <%If fname="" then%>
10 Sorry we are not on a first name basis...<p>
11 <%end if
12 select case lcase(lname)
13 case "washington","adams"
14 response.write "The first president has same last name<p>"
15 case "jefferson"
16 response.write "The third president has same last name<p>"
17 case "lincoln"
18 response.write "The sixteenth president has same last name<p>"
19 end select%>
20 </body></html>
Select Case Part2 (by John Kauffman & Charles Carroll)
Using IF-THEN can be cumbersome, prone to programmer errors and slower to execute. A more efficient construct is SELECT CASE. It is optimized for testing one variable against many conditions.
1 <html><head>
2 <TITLE>case2.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <form action="case2respond.asp" method="get">
5 Your First Name<INPUT NAME="FirstName" MaxLength=20><p>
6 Your Last Name<INPUT NAME="LastName" MaxLength=20><p>
7 Your Title
8 <INPUT TYPE="Radio" name="Title" VALUE="employee">Entry Level
9 <INPUT TYPE="Radio" name="Title" VALUE="temp" CHECKED>Temporary Employee
10 <INPUT TYPE="Radio" name="Title" VALUE="manager">Management Candidate
11 <INPUT TYPE="Radio" name="Title" VALUE="executive">Executive
12 <INPUT TYPE="Radio" name="Title" VALUE="vice-prez">The Vice President of...
13 <INPUT TYPE="Radio" name="Title" VALUE="CEO">The Boss<p>
14 <INPUT TYPE=submit><p>
15 </form>
16 </body></html>
Here is the select case that will determine what the form input means.
1 <html><head>
2 <TITLE>case2respond.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 fname=request.querystring("Firstname")
6 lname=request.querystring("Lastname")
7 title=request.querystring("title")
8 response.write "Nice to Hire You " & fname & " " & lname & "<p>"
9 Select Case lcase(Title)
10 case "employee","temp"
11 response.write("The washroom is in the hall")
12 case "manager","executive"
13 response.write("Here is your key to the Executive washroom")
14 case "ceo", "vice-prez"
15 response.write("The maid will attend to your private washroom")
16 End Select%>
17 </body></html>
IF statement (by John Kauffman & Charles Carroll)
Very often you must determine what to do next based on user input. This is one of the roles of the IF statement. First we make a form that will ask a user for their first name and last name.
1 <html><head>
2 <TITLE>if.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <form action="ifrespond.asp" method=get>
5 Your First Name<INPUT NAME="FirstName" MaxLength=20><p>
6 Your Last Name<INPUT NAME="LastName" MaxLength=20><p>
7 <INPUT TYPE=submit><p><INPUT TYPE=reset>
8 </form></body></html>
Now we make a asp file that examines their first name and last name after the form is submitted.
1 <html><head>
2 <TITLE>ifrespond.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%fname=request.querystring("Firstname")
5 lname=request.querystring("Lastname")
6 If fname="George" and lname="Washington" then%>
7 Hi.<p>You must be the first president!
8 <%else%>
9 Hi!<p>Nice to Meet You
10 <%end if%>
11 </body></html>
IF statement Part2 (by John Kauffman & Charles Carroll)
Very often you must determine what to do next based on user input. This is one of the roles of the IF statement. First we make a form that will ask a user for their first name and last name.
1 <html><head>
2 <TITLE>if2.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <form action="if2respond.asp" method=get>
5 Your First Name<INPUT NAME="FirstName" MaxLength=20><p>
6 Your Last Name<INPUT NAME="LastName" MaxLength=20><p>
7 <INPUT TYPE=submit><p><INPUT TYPE=reset>
8 </form></body></html>
Now we make a asp file that examines their first name and last name after the form is submitted. As contrasted to the previous example this time we are checking multiple conditions utilizing elseif and we are dealing with entries no matter whether they were typed in upper or lower case.
1 <html><head>
2 <TITLE>if2respond.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 fname=lcase(request.querystring("Firstname"))
6 lname=lcase(request.querystring("Lastname"))
7 If fname="george" and lname="washington" then%>
8 Hi.<p>You must be the first president!
9 <%elseIf fname="ronald" and lname="reagan" then%>
10 Hi.<p>You must be the actor president!
11 <%elseIf fname="jimmy" and lname="carter" then%>
12 Hi.<p>You must be the peanut farmer president!
13 <%elseIf fname="naoko" or fname="charles" then%>
14 Hi.<p>Your name reminds me of someone<p>
15 but I am not sure who!
16 <%else%>
17 Hi!<p>Nice to Meet You
18 <%end if%>
19 </body></html>
IF statement Part3 (by John Kauffman & Charles Carroll)
Very often you must determine what to do next based on user input. This is one of the roles of the IF statement. First we make a form that will ask a user for their first name, last name and salary.
1 <html><head>
2 <TITLE>if3.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <form action="if3respond.asp" method=get>
5 Your First Name<INPUT NAME="FirstName" MaxLength=20><p>
6 Your Last Name<INPUT NAME="LastName" MaxLength=20><p>
7 Your Salary <INPUT NAME="Salary" MaxLength=7><p>
8 <INPUT TYPE=submit><p><INPUT TYPE=reset>
9 </form></body></html>
This example shows how IF can deal with ranges and use various other boolean operators.
1 <html><head>
2 <TITLE>if3respond.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 fname=request.querystring("Firstname")
6 lname=request.querystring("Lastname")
7 salary=request.querystring("Salary")
8 response.write "Nice to Meet You " & fname & " " & lname & "<p>"
9 if salary>100000 then%>
10 Would you like to loan me some money?<p>
11 <%
12 sstaxMarginal=15
13 else
14 SSTaxMarginal=15
15 end if
16 If salary>6700 and salary<30000 then
17 SSTaxMarginal=0%>
18 Would you like me to loan you some money?<p>
19 <%end if
20 If salary<6700 then
21 SSTaxMarginal=0
22 end if
23 %>
24 By the way your marginal tax rate is <%=sstaxmarginal%>
25 </body></html>
If Then Part4 (by John Kauffman & Charles Carroll)
Very often you must determine what to do next based on user input. This is one of the roles of the IF statement. First we make a form that will ask a user for their first name, last name and salary.
1 <html><head>
2 <TITLE>if4.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <form action="if4respond.asp" method=get>
5 Your First Name<INPUT NAME="FirstName" MaxLength="20"><p>
6 Your Last Name<INPUT NAME="LastName" MaxLength="20"><p>
7 Your Salary <INPUT NAME="Salary" MaxLength="7"><p>
8 <INPUT TYPE=submit><p>
9 </form></body></html>
This example shows how IF can deal with ranges but this example illustrates the critical factor of ordering. If you were to re-arrange these IFs they would not accurately report your salary grade.
1 <html><head>
2 <TITLE>if4respond.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%fname=request.querystring("Firstname")
5 lname=request.querystring("Lastname")
6 salary=request.querystring("Salary")
7 response.write "Nice to Meet You " & fname & " " & lname & "<p>"
8 if salary>80000 then
9 salarygrade=4
10 end if
11 if salary <=80000 then
12 salarygrade=3
13 end if
14 If salary <=60000 then
15 salarygrade=2
16 end if
17 if salary <=40000 then
18 salarygrade=1
19 end if
20 response.write ("Your Salary is $" & salary)
21 response.write (", your Grade is " & salarygrade & ".<p>")
22 %>
23 </body></html>
Forms - For Each/Iteration by Charles Carroll
Forms with many elements may be easier to process if a FOR EACH construct is used to loop through every value submitted from the source form. In this example, there are many checkboxes and instead of many IF statements we replace them with one FOR EACH loop.
1 <html><head>
2 <TITLE>monthlyForm.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <form action="monthlyFormRespond.asp" method="post">
5 <p>CheckBox Form Example</p>
6 <p><b>Check Off The Days You Worked</b></p>
7 <%
8 my_month=request.querystring("whichmonth")
9 ' response.write my_month & "<br>"
10 if my_month="" then
11 my_date=now()
12 else
13 my_month=month(request.querystring("whichmonth") & "/1/2000")
14 my_date=dateserial(year(now()),my_month,1)
15 end if
16 'response.write my_date & "<br>"
17
18 If my_month=12 then
19 my_day=1
20 my_month=1
21 my_year=my_year+1
22 else
23 my_day=1
24 my_month=month(my_date)+1
25 my_year=year(my_date)
26 end if
27
28 lastdayofmonth=day(DateSerial(Year(my_Date), my_Month,0))
29 'response.write lastdayofmonth & "<br>"
30
31 for counter=1 to lastdayofmonth%>
32 <input TYPE="checkbox" NAME="workingday<%=counter%>">day <%=counter%> Worked?<br>
33 <%next%>
34 <input type="submit" value="Submit Days Worked">
35 </form>
36 </body></html>
1 <html><head>
2 <TITLE>monthlyFormRespond.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 daysworked=0
6 for each whatever in request.form
7 thisvalue=request.form(whatever)
8 If thisvalue="on" then
9 daysworked=daysworked+1
10 end if
11 next
12 response.write "Thanks for working <b>" & daysworked & "</b> days"
13 %>
14 </body></html>
Form Tricks #2: Self-Submitting Forms
There are some clever tactics programmers can apply when dealing with forms. They have their drawbacks and should be used sparingly.
Some people prefer to combine their forms and actions in one file. I don't because of usability issues -- a user who refreshes a combined form will be presented with a confusing choice "repost form data (y/n)" which they are not presented with if the forms are separate. Such a form could be submitted twice accidentally when they just meant to redisplay the form.
1 <html><head>
2 <title>FormSubmitSelf.asp</title>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 SELECT CASE lcase(request.form("join"))
6 CASE "join"
7 response.write "Thanks for joining "
8 response.write request.form("namefirst") & " "
9 response.write request.form("namelast") & " "
10 CASE ELSE
11 %>
12 <Form method="post">
13 Fill Out This Form For Us:<p>
14 First Name <Input NAME="NameLast" size ="10"><br>
15 Last Name <Input NAME="NameLast" size ="10"><br>
16 <input type="submit" name="Join" value="Join">
17 </form>
18 <%END SELECT%>
19 </body></html>
20
Form Tricks #3: Forms that Dynamically Change Action
There are some clever tactics programmers can apply when dealing with forms. They have their drawbacks and should be used sparingly.
Some forms need to vary their action.
1 <html><head>
2 <title>FormTextBox.asp</title>
3 </head><body bgcolor="#FFFFFF">
4 <Form name="profile" action = "profilerespond.asp" method="post">
5 Fill Out This Form For Us:<p>
6 First Name <Input NAME="NameLast" size ="10"><br>
7 Last Name <Input NAME="NameLast" size ="10"><br>
8 <a href="Javascript:army()">Join Army</a>
9 <a href="Javascript:navy()">Join Navy</a>
10 <a href="Javascript:marines()">Join Marines</a>
11 </form>
12 </body></html>
13
14 <form name=myform>
15 </form>
16
17 <script language =javascript>
18 function army(form)
19 {
20 document.myform.action="joinarmy.asp"
21 document.myform.submit()
22 }
23 function navy(form)
24 {
25 document.myform.action="joinnavy.asp"
26 document.myform.submit()
27 }
28 function marines(form)
29 {
30 document.myform.action="joinmarines.asp"
31 document.myform.submit()
32 }
33 </script>
Databases
Displaying Table w/Simple Code (dbsimple.asp) - Page 67
List Box Displayed Generically (dblist.asp) - Page 68
Database to ListBox Online Resources (dblistmore.asp) - Page 69
DSNLess Connections (dbopen.asp) - Page 70
DSN Setup #1 by Rob Martinson (dsn1.asp) - Page 71
DSN Setup #2 by Rob Martinson (dsn2.asp) - Page 72
DSN Setup #3 by Rob Martinson (dsn3.asp) - Page 73
DSN Setup #4 by Rob Martinson (dsn4.asp) - Page 74
DSN Setup #5 by Rob Martinson (dsn5.asp) - Page 75
DSN Setup #6 by Rob Martinson (dsn6.asp) - Page 76
Full Cycle #1 Show/Edit/Update (dbfull1.asp) - Page 77
Full Cycle #2 Show/Edit/Update (dbfull2.asp) - Page 78
Full Cycle #3 Show/Edit/Update (dbfull3.asp) - Page 79
SQL Mistakes Everyone Makes (dbtroubleshoot2.asp) - Page 80
DB: Table Displayed Generically (dbtable.asp) - Page 81
Getstring to display database table (dbtablegetstring.asp) - Page 82
Getrows to display database table (dbtablegetrows.asp) - Page 83
GetRows w/no Numbers (dbtablegetrowsnonum.asp) - Page 84
Disconnected Recordsets, Display Table (dbtabledisconnected.asp) - Page 85
DB: More ways To Display Tables (dbtablemore.asp) - Page 86
DB: Generic DB by Eli Robillard (genericdb.asp) - Page 87
Generic DB Listserver (aspgenericdb.asp) - Page 88
DB: Converting a DB to a Comma-Delimited file (dbconvert.asp) - Page 89
DB: Deleting a Record w/SQL (dbSQLdelete.asp) - Page 90
DB: Access Scalability (accesstest.asp) - Page 91
ADO: Paging Records (dbtablepaged.asp) - Page 92
ADO: Limiting Number of Records (dbmaxrecs.asp) - Page 93
ADO: Count Records in Query (dbcount.asp) - Page 94
ADO: Cursor Types by Phil Paxton (adocursortypes.asp) - Page 95
ADO: Input Form (dbnewrec.asp) - Page 96
ADO: Input Form, added w/SQL (dbnewSQL.asp) - Page 97
ADO: Input Form, Added w/ADO .addnew (dbnewADO.asp) - Page 98
ADO: Tables within Databases (dbtablelists.asp) - Page 99
ADO: Schemas to access table lists (dbschemas.asp) - Page 100
ADO: Schemas to access All Data (dbschemasall.asp) - Page 101
ADO: Show Table,1 param (db1parm.asp) - Page 102
ADO: Update/edit Record (dbupdate.asp) - Page 103
DB: Troubleshooting Part 1 (dbtroubles.asp) - Page 104
DB: Troubleshooting Part 2 (dbtroubles2.asp) - Page 105
Database -- Simple Table Display by Charles Carroll
This page demonstrates the capabilities how to display a table from a SQL statement.
1 <html><head>
2 <TITLE>dbsimple.asp</TITLE>
3 </head>
4 <body bgcolor="#FFFFFF">
5 <%
6 ' this code opens the database
7 myDSN="DSN=Student;uid=student;pwd=magic"
8 set conntemp=server.createobject("adodb.connection")
9 conntemp.open myDSN
10
11 ' this code retrieves the data
12 mySQL="select * from publishers where state='NY'"
13 set rstemp=conntemp.execute(mySQL)
14
15 ' this code detects if data is empty
16 If rstemp.eof then
17 response.write "No records matched<br>"
18 response.write mySQL & "<br>So cannot make table..."
19 connection.close
20 set connection=nothing
21 response.end
22 end if
23 %>
24 <table border=1>
25 <%
26 ' This code puts fieldnames into column headings
27 response.write "<tr>"
28 for each whatever in rstemp.fields
29 response.write "<td><B>" & whatever.name & "</B></TD>"
30 next
31 response.write "</tr>"
32
33 ' Now lets grab all the records
34 DO UNTIL rstemp.eof
35 ' put fields into variables
36 pubid=rstemp("pubid")
37 name=rstemp("name")
38 company_name=rstemp("company name")
39 address=rstemp("address")
40 city=rstemp("city")
41 state=rstemp("state")
42 zip=rstemp("zip")
43 telephone=rstemp("telephone")
44 fax=rstemp("fax")
45 comments=rstemp("comments")
46
47 ' write the fields to browser
48 cellstart="<td align=""top"">"
49 response.write "<tr>"
50 response.write cellstart & pubid & "</td>"
51 response.write cellstart & name & "</td>"
52 response.write cellstart & company_name & "</td>"
53 response.write cellstart & address & "</td>"
54 response.write cellstart & city & "</td>"
55 response.write cellstart & state & "</td>"
56 response.write cellstart & zip & "</td>"
57 response.write cellstart & telephone & "</td>"
58 response.write cellstart & fax & "</td>"
59 response.write cellstart & comments & "</td>"
60
61 response.write "</tr>"
62 rstemp.movenext
63 LOOP
64 %>
65 </table>
66
67 <%
68 ' Now close and dispose of resources
69 rstemp.close
70 set rstemp=nothing
71 conntemp.close
72 set conntemp=nothing
73 %>
74 </body></html>
HTML List Box from Column
This page demonstrates the capabilities how to display a list box from a SQL statement. This is the simplest possible example. The script to display a list from a database is:
1 <html><head>
2 <TITLE>dblist.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 myDSN="DSN=Student;uid=student;pwd=magic"
6 mySQL="select author from authors where AU_ID<100"
7
8 ' displays a database field as a listbox
9 set conntemp=server.createobject("adodb.connection")
10 conntemp.open myDSN
11 set rstemp=conntemp.execute(mySQL)
12 if rstemp.eof then
13 response.write "no data for<br>"
14 response.write mySQL
15 conntemp.close
16 set conntemp=nothing
17 response.end
18 end if
19
20 %>
21 <form action="dblistrespond.asp" method="post">
22 <Select name="authorname">
23 <%
24 ' Now lets grab all the data
25 do until rstemp.eof %>
26 <option> <%=RStemp(0)%> </option>
27 <%
28 rstemp.movenext
29 loop
30
31 rstemp.close
32 set rstemp=nothing
33 conntemp.close
34 set conntemp=nothing
35 %>
36 <input type="submit" value="Choose Author">
37 </Select></form>
38 </body></html>
The form responder dblistrespond.asp
is:
1 <html><head>
2 <TITLE>dblistrespond.asp</TITLE>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 my_author=request.form("authorname")
6 %>
7 You choose <%=my_author%><br>Thanks!<br>
8 </body></html>
Database Listbox Resources
There are actually many approaches to creating listboxes from databases demonstrated on this site and other sites:
http://www.learnasp.com/learn/subdblist.asp
http://www.learnasp.com/learn/subdblistbest.asp
http://www.siteexperts.com/tips/elements/ts25/page1.asp
http://www.niblack.com/kbase/listbox_create_x.asp
http://www.niblack.com/kbase/listboxmgmt_x.asp
http://www.niblack.com/kbase/listbox_x.asp
And many people like dynamic list boxes where one listbox choice affects the values available in the other listbox:
http://www.learnasp.com/learn/listdynamic.asp
http://www.learnasp.com/learn/listdynamicdb.asp
http://www.learnasp.com/learn/listdynamicmore.asp
There are also some alternative approaches to building alternative, gimicky listboxes that are helpful:
http://www.learnasp.com/learn/listdual.asp
http://www.siteexperts.com/tips/elements/ts22/page1.asp
DSN, DSNless connections by Charles Carroll
OLEDB Good Connection String (recommended)
strconn="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="
strconn=strconn & server.mappath(accessDB)
& ";"
'strconn=strconn & "Password=whatever;"
ODBC Connection String
(not recommended)
strconn="DRIVER={Microsoft
Access Driver (*.mdb)};"
strconn=sourceDSN & "DBQ=" & server.mappath(accessdb)
strconn=sourceDSN & "Password=whatever;"
NOTE: An ODBC connection string to Access often results in the dreaded: Unable to open registry key or 'Unspecified Error' usually occurs with an ODBC connection string to Access, i.e.
Any ASP script that needs to connect to a database must open it on the server first. There are several ways:
SYSTEM DSN
which must be setup on the server, see:
/learn/dsn1.asp for
detailed instructions or
http://www.aspalliance.com/components/database.asp
for components that automate this task.
This is NOT the fastest way* since all the information resides
on the server and need only be validated when the DSN is setup.
FILE DSN
which is not recommended for high concurrency situations since all
users would be bottlenecked by how fast the ASII file that holds the DSN
could be accessed. The trouble with a File DSN is that every
connection.open must open, read, close an ASCII file
and present the data anew to the provider/driver
since the ASCII file may have changed since last connection.File DSNs are
bottlenecks on busy sites.
Most Connection strings are detailed @
http://www.able-consulting.com/ado_conn.htm?f=ado_conn.htm
& http://support.microsoft.com/support/kb/articles/q193/3/32.asp
http://support.microsoft.com/support/kb/articles/Q191/7/54.ASP
4GuyfromRolla (a superb ASP site of unparalleled depth) has a great set of DSN articles:
* System DSN or DSNless connection @
http://www.4guysfromrolla.com/webtech/070399-1.shtmlNifty way to make DSNless connections @
http://www.4guysfromrolla.com/webtech/070699-1.shtmlListing System DSNs with a FREE component:
http://www.4guysfromrolla.com/webtech/011900-1.shtml
Here is a sample nwind.asp connecting to an access database named
"nwind.mdb" in the root of the web site with a DSNless connection. Note that you must know the actual filepath on the server, i.e. nwind.mdb is not good
enough it needs to be "C:\thatserver\account17\nwind.mdb". Fortunately the
server.mappath function can turn a filename into the proper fully qualified filename with
path on the server.
1 <HTML><HEAD>
2 <TITLE>nwind.asp</TITLE>
3 <body bgcolor="#FFFFFF"></HEAD>
4 <%
5 accessdb=server.mappath("nwind.mdb")
6 strconn="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="
7 strconn=strconn & accessDB & ";"
8 'strconn=strconn & "USER ID=;PASSWORD=;"
9
10 mySQL="select * from customers"
11
12 call query2table(mySQL,strconn)
13 %>
14 <!--#include virtual="/learn/test/lib_dbtable.asp"-->
15 </BODY>
16 </HTML>
17
18
Here is a sample sqldsn.asp connecting to an access database named
"nwind.mdb" in the root of the web site with a DSNless connection.
1 <HTML><HEAD>
2 <TITLE>sqlserverdsn.asp</TITLE>
3 <body bgcolor="#FFFFFF"></HEAD>
4 <%
5 strconn="DSN=student;uid=student;pwd=magic"
6 mySQL="select * from publishers where state='NY'"
7
8 call query2table(mySQL,strconn)
9 %>
10 <!--#include virtual="/learn/test/lib_dbtable.asp"-->
11 </BODY>
12 </HTML>
13
14
Here is a sample sqldsnless.asp connecting to an access database named
"nwind.mdb" in the root of the web site with a DSNless connection.
1 <HTML><HEAD>
2 <TITLE>sqlDSNless.asp</TITLE>
3 <body bgcolor="#FFFFFF"></HEAD>
4 <%
5 strconn="PROVIDER=MSDASQL;DRIVER={SQL Server};"
6 strconn=strconn & "SERVER=sql2.datareturn.com;DATABASE=;"
7 strconn=strconn & "UID=student;PWD=magic;"
8
9 mySQL="select * from publishers where state='NY'"
10
11 call query2table(mySQL,strconn)
12 %>
13 <!--#include virtual="/learn/test/lib_dbtable.asp"-->
14 </BODY>
15 </HTML>
16
17
18
The Include file lib_dbtable.asp looks like
this:
1 <%
2 sub query2table(inputquery, inputDSN)
3 dim conntemp, rstemp
4 set conntemp=server.createobject("adodb.connection")
5 conntemp.open inputDSN
6 set rstemp=conntemp.execute(inputquery)
7 howmanyfields=rstemp.fields.count -1%>
8 <table border=1><tr>
9 <% 'Put Headings On The Table of Field Names
10 for i=0 to howmanyfields %>
11 <td><b><%=rstemp(i).name%></B></TD>
12 <% next %>
13 </tr>
14 <% ' Now lets grab all the records
15 do while not rstemp.eof %>
16 <tr>
17 <% for i = 0 to howmanyfields
18 thisvalue=rstemp(i)
19 If isnull(thisvalue) then
20 thisvalue=" "
21 end if%>
22 <td valign=top><%=thisvalue%></td>
23 <% next %>
24 </tr>
25 <%rstemp.movenext
26 loop%>
27 </table>
28 <%
29 rstemp.close
30 set rstemp=nothing
31 conntemp.close
32 set conntemp=nothing
33 end sub%>
The easiest way to begin learning ASP, is to install Microsoft PWS, and develop locally. To do this, you must also setup a DSN (Data Source Name) on your machine, if you wish to use any of ASP's database access abilities. This tutorial is meant to walk you through the process of setting up a system DSN on your Windows machine.


The first step, after you have PWS installed, is to open your control panel and select
the 32bit ODBC Data Source Administrator icon (shown below).
![]()
Setting up a system DSN on Windows 95/NT
Once the ODBC Data Source Administrator window is open, select the System DSN tab at the top (I'm doing a system DSN because that's what I generally use. See here for more info on the differences between system and file DSNs). This will bring you to a list of System Data Sources that are setup on your machine. It may vary from the list above. From this point, click the "Add" button and move on to the next screen.

Here we will name our DSN. For web development, I generally setup a single DSN (depending on the project of course) and then just modify the database as needed. Because of that, I almost always name mine "tables.dsn". Name it whatever you like, just remember the name. From this screen you are also able to setup various options with your DSN including default authorization (click advanced), page timeout, database location, etc. For now we will leave the default options and move on to the next screen by choosing "Ok".
That's it! Your System DSN is setup and running. All you need to do now is get ASP to use it (which is pretty easy).
Database Full Cycle #1 --
Display Table, Edit Record, Update Record
dbfull1.asp merely displays a table like we have in other examples, except every row of the table has a hyperlink to dbfull2.asp and passes the ID to that script in a variable called whichID. After the user edits that information and presses submit, dbfull3.asp is called. It's task is to gather all input fields and construct a SQL update statement. Here is a source printout of the script that displays the table and "starts the ball rolling":
1 <%
2 response.buffer=true
3 Response.ExpiresAbsolute = Now() - 1
4 Response.AddHeader "Cache-Control", "private"
5 %>
6 <html><head>
7 <title>authorshow.asp</title>
8 <meta http-equiv="pragma" content="no-cache">
9 </head><body bgcolor="#FFFFFF">
10 <%
11 myDSN="DSN=Student;uid=student;pwd=magic"
12 mySQL="select * from authors where AU_ID<100 order by author"
13 IDfield="AU_ID"
14 scriptresponder="authoredit.asp"
15
16 set conntemp=server.createobject("adodb.connection")
17 conntemp.open myDSN
18 set rstemp=conntemp.execute(mySQL)
19 howmanyfields=rstemp.fields.count -1
20 %>
21 <table border="1">
22 <tr>
23 <td valign="top">---</td>
24 <% 'Put Headings On The Table of Field Names
25 for i=0 to howmanyfields %>
26 <td><b><%=rstemp(i).name %></b></td>
27 <% next %>
28 </tr>
29 <% ' Now lets grab all the records
30 do while not rstemp.eof %>
31 <tr><td valign="top">
32 <%my_link=scriptresponder & "?which=" & rstemp(idfield)%>
33 <a HREF="<%=my_link%>">Edit</a></td>
34 <% for i = 0 to howmanyfields%>
35 <td valign="top"><%=rstemp(i)%></td>
36 <% next %>
37 </tr>
38 <%
39 rstemp.movenext
40 loop
41
42 rstemp.close
43 set rstemp=nothing
44 conntemp.close
45 set conntemp=nothing
46 %>
47 </table></body></html>
Database Full Cycle #2 --
Display Table, Edit Record, Update Record
Here is the script that shows one record based on being linked to from the table
1 <html><head>
2 <title>authoredit.asp</title>
3 </head><body bgcolor="#FFFFFF">
4 <%
5 ' My ASP program that given an AU_ID, allows editing a record
6
7 myDSN="DSN=Student;uid=student;pwd=magic"
8
9 set conntemp=server.createobject("adodb.connection")
10 conntemp.open myDSN
11 form_ID=request.querystring("which")
12
13 sqltemp="select * from authors "
14 sqltemp=sqltemp & " where AU_ID=" & form_id
15
16 set rstemp=conntemp.execute(sqltemp)
17
18
19 form_auID=rstemp("AU_ID")
20 form_author=rstemp("Author")
21 form_year_born=rstemp("Year_Born")
22
23 rstemp.close
24 set rstemp=nothing
25 conntemp.close
26 set conntemp=nothing
27 %>
28 <body>
29 <form name="myauthor" action="authoreditrespond.asp" method="POST">
30
31 <input type="hidden" name="id" value="<%=form_auid%>">
32
33 <p>Author ID: <%=form_auid%></p>
34
35 <p> Author Name:
36 <input type="TEXT" name="name" value="<%=form_author%>"></p>
37
38 <p> Year Born:
39 <input type="TEXT" name="year" value="<%=form_year_born%>"></p>
40
41 <p> <input type="SUBMIT"> </p>
42 </form>
43 </body>
Database Full Cycle #3 --
Display Table, Edit Record, Update Record
Here is the script that updates one record after the submit button is pushed on the previous script.
1 <HTML><HEAD>
2 <TITLE>authoreditrespond.asp</TITLE>
3 <body bgcolor="#FFFFFF"></HEAD>
4 <%
5 on error resume next
6 form_name=request.form("name")
7 form_year=request.form("year")
8 form_ID=request.form("ID")
9
10 Set Conn = Server.CreateObject("ADODB.Connection")
11 conn.open "DSN=Student;uid=student;pwd=magic"
12
13 ' change apostrophe to double apostrophe
14 form_name=Replace(form_name, "'", "''")
15 IF instr(lcase(form_name),"<img")>0 THEN
16 form_name=""
17 END IF
18
19 SQLstmt = "UPDATE authors SET "
20 SQLStmt = SQLstmt & "Author='" & form_name & "',"
21 SQLstmt = SQLstmt & "year_born=" & form_year
22 SQLStmt = SQLStmt & " WHERE AU_ID=" & form_id
23
24
25 Set RS = Conn.Execute(SQLStmt)
26
27 If err.number>0 then
28 response.write "VBScript Errors Occured:" & "<P>"
29 response.write "Error Number=" & err.number & "<P>"
30 response.write "Error Descr.=" & err.description & "<P>"
31 response.write "Help Context=" & err.helpcontext & "<P>"
32 response.write "Help Path=" & err.helppath & "<P>"
33 response.write "Native Error=" & err.nativeerror & "<P>"
34 response.write "Source=" & err.source & "<P>"
35 response.write "SQLState=" & err.sqlstate & "<P>"
36 end if
37 IF conn.errors.count> 0 then
38 response.write "Database Errors Occured" & "<P>"
39 response.write SQLstmt & "<P>"
40 for counter= 0 to conn.errors.count
41 response.write "Error #" & conn.errors(counter).number & "<P>"
42 response.write "Error desc. -> " & conn.errors(counter).description & "<P>"
43 next
44 else
45 response.write "<B>Everything Went Fine. Record is updated now!</b>"
46 response.write "<br>" & SQLstmt
47 end if
48
49 rs.close
50 set rs=nothing
51 Conn.Close
52 set conn=nothing
53 %>
54 </BODY>
55 </HTML>
56
Troubleshooting SQL Statements by Charles Carroll
Now we will show some SQL statements (below) with mistakes and the fixes indicated in red to show how to make the statements work. The kind of fixes we deploy include:
text fields must have single quotes around the values.
fields with spaces in their names must be surrounded with [ ]
set parameters must have commas between them
Text fields with single quote ' cannot be placed into SQL statements unmodified. Notice how the last example below uses the VBScript replace command to transform a string that may contain embedded ' .
| Statement with Flaws | Improved Statement (fixes in red) |
| UPDATE mytableSET LocID=0007,Material=13 1/4 Description=T-shirts ListPrice=35 WHERE CusID=97 |
added space before SET UPDATE mytable SET LocID='0007',Material='13 1/4', Description='T-shirts', [List Price]=35 WHERE CusID=97 |
| INSERT INTO authors (AU_ID, author, year_born) VALUES (7000, Joe Smith,1950) | INSERT INTO authors (AU_ID, author, year_born) VALUES (7000, 'Joe Smith',1950) |
| SELECT * from atable
where state = MD and year born<1955 |
SELECT * from atable
where state = 'MD' and and [year born]<1955 |
| <% key=request.querystring("id") au=request.querystring("author") birthyear=request.querystring("year") SQLstmt="INSERT INTO authors (AU_ID, author, year_born) VALUES (" SQLstmt= SQLstmt & key & "," SQLstmt= SQLstmt & author & "," SQLstmt= SQLstmt & birthyear & ")" %> |
<% key=request.querystring("id") au=request.querystring("author") au=Replace(au, "'", "''") birthyear=request.querystring("year") SQLstmt="INSERT INTO authors (AU_ID, author, year_born) VALUES (" SQLstmt= SQLstmt & key & ",'" SQLstmt= SQLstmt & author & "'," SQLstmt= SQLstmt & birthyear & ")" %> |
Display Table on Web Page by Charles Carroll
This page demonstrates the capabilities how to display a table from a SQL statement. It illustrates not only how to display the table, but also how to detect that no records were returned from a query, and how to detect null and blank values in the data.
1 <html><head>
2 <TITLE>dbtable.asp</TITLE>
3 </head>
4 <body bgcolor="#FFFFFF">
5 <%
6 ' ASP program that displays a database in table form
7 myDSN="DSN=Student;uid=student;pwd=magic"
8 mySQL="select * from publishers where state='NY'"
9 showblank=" "
10 shownull="-null-"
11
12 set conntemp=server.createobject("adodb.connection")
13 conntemp.open myDSN
14 set rstemp=conntemp.execute(mySQL)
15 If rstemp.eof then
16 response.write "No records matched<br>"
17 response.write mySQL & "<br>So cannot make table..."
18 conntemp.close
19 set conntemp=nothing
20 response.end
21 end if
22 %>
23 <table border=1><tr>
24
25 <% 'Put Headings On The Table of Field Names
26 for each whatever in rstemp.fields%>
27 <td><b><%=whatever.name%></B></TD>
28 <% next %>
29 </tr>
30
31 <% ' Now lets grab all the records
32 DO UNTIL rstemp.eof %>
33 <tr>
34 <% for each whatever in rstemp.fields
35 thisfield=whatever.value
36 if isnull(thisfield) then
37 thisfield=shownull
38 end if
39 if trim(thisfield)="" then
40 thisfield=showblank
41 end if%>
42 <td valign=top><%=thisfield%></td>
43 <% next %>
44 </tr>
45 <%rstemp.movenext
46 LOOP%>
47 </table>
48
49 <%
50 rstemp.close
51 set rstemp=nothing
52 conntemp.close
53 set conntemp=nothing
54 %>
55 </body></html>
Database Display via GetString by Charles Carroll
This page demonstrates the capabilities how to display a table from a SQL statement a very fast and scaleable way using a recordset method called GetString. GetString essentially asks the backend database to build a huge string instead of moving the data as rows and columns. It allows very limited specifications, basically what string to place between each column and row and how to display nulls. Notice the total absence of the traditional EOF loop.
Its speed and scalability advantages
are explained in:
Http://www.learnasp.com/advice/whygetrows.asp
This example does require ADO 2.0 or
greater which can be downloaded from:
http://www.microsoft.com/data
If you are unsure which ADO version
your server has
http://www.learnasp.com/learn/versioncheck.asp
will help you determine this.
Does this approach matter for small data sets for example 9 rows x 2 columns of data? YES!!!!!!!!
My site has SQLserver scripts that run like lightning. I once needed to fill a 9 item listbox from Access and got 90 sec script timeouts with movenext. Getstring never timed out. So in a real production situation it makes weak databases feasible and of course reduces the load on more industrial back-ends so maybe the SQLserver doesn't need as many indexes or RAM upgrades.
"The Worlds Fastest Listbox"
http://www.learnasp.com/learn/speedappdata.asp
shows the only faster way to display databases.
1 <TITLE>dbtablegetstring.asp</TITLE>
2 <body bgcolor="#FFFFFF">
3 <%
4 whichDSN="DSN=Student;uid=student;pwd=magic"
5 mySQL="select * from publishers where state='NY'"
6
7 set conntemp=server.createobject("adodb.connection")
8 conntemp.open whichDSN
9 set rstemp=conntemp.execute(mySQL)
10 If rstemp.eof then
11 response.write "No records matched<br>"
12 response.write mySQL & "<br>So cannot make table..."
13 Call CloseAll
14 response.end
15 end if
16
17 response.write "<table border='1'><tr>"
18 'Put Headings On The Table of Field Names
19 for each whatever in rstemp.fields
20 response.write "<td><b>" & whatever.name & "</B></TD>"
21 next
22 response.write "</tr><tr><td>"
23 response.write rstemp.getstring(,, "</td><td>", "</td></tr><TR><TD>", "-null-")
24 response.write "</td></tr></table>"
25 Call CloseAll
26
27
28 SUB CloseALL
29 rstemp.close
30 set rstemp=nothing
31 conntemp.close
32 set conntemp=nothing
33 END SUB
34 %>
35 </body></html>
36
37
38
39
40
41
42
43
44
Table Database Display via GetRows by Charles Carroll
This page demonstrates the capabilities how to display a table from a SQL statement a very fast and scaleable way using a recordset method called GetRows. GetRows that move many records and fields into a memory array. Once in the array it is accessed very fast. If you read the code closely you will notice it can free up the recordset and connection object earlier than the traditional loop thus freeing up those resources for other scripts.
The array fields are accessed by
number, a script at:
http://www.learnasp.com/learn/dbtablegetrowsnamed.asp
shows how to combine the speed of getrows and simulate named fields using
dictionary objects.
In terms of why this is faster and
reduces server resource consumption,
read:
http://www.learnasp.com/advice/whygetrows.asp
to see an in-depth explanation.
http://www.learnasp.com/learn/dbtablegetstring.asp
rips getrows to shreds speed-wise as the backend transfers one big string
instead of a complex array structure but formatting is SOOOOO limited (unless
you know Regexps like the back of your hand....)
"The Worlds Fastest Listbox"
http://www.learnasp.com/learn/speedappdata.asp
shows the only faster way to display databases.
Since this code relies heavily on arrays you may want to read up on them at:
1 <%@enablesessionstate=false%>
2 <%response.buffer=true%>
3 <html><head>
4 <TITLE>dbtablegetrows.asp</TITLE>
5 </head>
6 <body bgcolor="#FFFFFF">
7 <%
8 ' displays a database in table form via GetRows
9 myDSN="DSN=Student;uid=student;pwd=magic"
10 mySQL="select * from publishers where state='NY'"
11 showblank=" "
12 shownull="-null-"
13
14 set conntemp=server.createobject("adodb.connection")
15 conntemp.open myDSN
16 set rstemp=conntemp.execute(mySQL)
17 If rstemp.eof then
18 response.write "No records matched<br>"
19 response.write mySQL & "<br>So cannot make table..."
20 Call CloseAll
21 response.end
22 end if
23
24 response.write "<table border='1'><tr>" & vbcrlf
25 'Put Headings On The Table of Field Names
26 for each whatever in rstemp.fields
27 response.write "<td><b>" & whatever.name & "</B></TD>" & vbcrlf
28 next
29 response.write "</tr>" & vbcrlf
30
31 ' Now lets grab all the records
32 alldata=rstemp.getrows
33 Call CloseAll
34
35 numcols=ubound(alldata,1)
36 numrows=ubound(alldata,2)
37 FOR rowcounter= 0 TO numrows
38 response.write "<tr>" & vbcrlf
39 FOR colcounter=0 to numcols
40 thisfield=alldata(colcounter,rowcounter)
41 if isnull(thisfield) then
42 thisfield=shownull
43 end if
44 if trim(thisfield)="" then
45 thisfield=showblank
46 end if
47 response.write "<td valign=top>"
48 response.write thisfield
49 response.write "</td>" & vbcrlf
50 NEXT
51 response.write "</tr>" & vbcrlf
52 NEXT
53 response.write "</table>"
54 %>
55 </body></html>
56 <%
57 SUB CloseAll
58 rstemp.close
59 set rstemp=nothing
60 conntemp.close
61 set conntemp=nothing
62 END SUB
63 %>
64
GetRows NOT BY THE Numbers by Charles Carroll
Getrows Rocks! It is
lightning fast and much more scalable than the .MOVENEXT approach. See a
complete explanation @
http://www.learnasp.com/advice/whygetrows.asp
Tired of accessing a getrows array by number? There is an easier way we show here below*. But below is the most straightforward ways to use names instead of numbers with a Getrows array.
* and an easier ENCAPSULATED way (easy to use,
complex source code) at:
http://www.learnasp.com/learn/getrowsultimate.asp
1 <%@enablesessionstate=false%>
2 <%response.buffer=true%>
3 <html><head>
4 <TITLE>dbtablegetrowsnonum.asp</TITLE>
5 </head>
6 <body bgcolor="#FFFFFF">
7 <%
8 ' displays a database in table form via GetRows
9 myDSN="DSN=Student;uid=student;pwd=magic"
10 'mySQL="select PubID, [Company Name], Address, City, State, Zip, Telephone, Fax, Comments "
11 ' above statement fails because of [company name] still researching why....
12 mySQL="select * "
13 mySQL=mySQL & " from publishers"
14 mySQL=mySQL & " where state='NY'"
15 set conntemp=server.createobject("adodb.connection")
16 conntemp.open myDSN
17 set rstemp=conntemp.execute(mySQL)
18 response.write rstemp(2).name
19
20 If rstemp.eof then
21 response.write "No records matched<br>"
22 response.write mySQL & "<br>So cannot make table..."
23 Call CloseAll
24 response.end
25 end if
26
27 response.write "<table border='1'><tr>" & vbcrlf
28 'Put Headings On The Table of Field Names
29 for each whatever in rstemp.fields
30 response.write "<td><b>" & whatever.name & "</B></TD>" & vbcrlf
31 next
32 response.write "</tr>" & vbcrlf
33
34 ' Now lets grab all the records
35 alldata=rstemp.getrows
36 Call CloseAll
37
38 numrows=ubound(alldata,2)
39 fld_pubid=0
40 fld_Name=1
41 fld_companyname=2
42 fld_address=3
43 fld_city=4
44 fld_state=5
45 fld_zip=6
46 fld_telephone=7
47 fld_fax=8
48 fld_comments=9
49
50 FOR rowcounter= 0 TO numrows
51 response.write "<tr>" & vbcrlf
52 pubid=alldata(fld_pubid,rowcounter)
53 name=alldata(fld_name,rowcounter)
54 companyname=alldata(fld_companyname,rowcounter)
55 address=alldata(fld_address,rowcounter)
56 city=alldata(fld_city,rowcounter)
57 state=alldata(fld_state,rowcounter)
58 zip=alldata(fld_zip,rowcounter)
59 telephone=alldata(fld_telephone,rowcounter)
60 fax=alldata(fld_fax,rowcounter)
61 comments=alldata(fld_comments,rowcounter)
62
63 pubid=cleanfield(pubid)
64 response.write "<td valign=top>" & pubid & "</td>" & vbcrlf
65
66 name=cleanfield(name)
67 response.write "<td valign=top>" & name & "</td>" & vbcrlf
68
69 companyname=cleanfield(companyname)
70 response.write "<td valign=top>" & companyname & "</td>" & vbcrlf
71
72
73 companyname=cleanfield(address)
74 response.write "<td valign=top>" & address & "</td>" & vbcrlf
75
76 city=cleanfield(city)
77 response.write "<td valign=top>" & city & "</td>" & vbcrlf
78
79 state=cleanfield(state)
80 response.write "<td valign=top>" & state & "</td>" & vbcrlf
81
82 zip=cleanfield(zip)
83 response.write "<td valign=top>" & zip & "</td>" & vbcrlf
84
85 telephone=cleanfield(telephone)
86 response.write "<td valign=top>" & telephone & "</td>" & vbcrlf
87
88 fax=cleanfield(fax)
89 response.write "<td valign=top>" & fax & "</td>" & vbcrlf
90
91 comments=cleanfield(comments)
92 response.write "<td valign=top>" & comments & "</td>" & vbcrlf
93
94 response.write "</tr>" & vbcrlf
95 NEXT
96 response.write "</table>"
97 %>
98 </body></html>
99 <%
100 SUB CloseAll
101 rstemp.close
102 set rstemp=nothing
103 conntemp.close
104 set conntemp=nothing
105 END SUB
106
107 FUNCTION CleanField(parm1)
108 cleanfield=parm1
109 if isnull(parm1) then
110 cleanfield=" "
111 end if
112 if trim(parm1)="" then
113 cleanfield=" "
114 end if
115 END FUNCTION
116 %>
117
Display Table and Disconnect Recordset by Charles Carroll
This page demonstrates the capabilities how to display a table from a SQL statement. It illustrates not only how to display the table, but also how to detect that no records were returned from a query, and how to detect null and blank values in the data. This example disconnects the recordset so that the data will not be drawn from up to the millisecond database data BUT retrieval will be much faster.
1 <%@enablesessionstate=false%>
2 <%response.buffer=true%>
3 <!--#include virtual="/adovbs.inc"-->
4 <html><head>
5 <TITLE>dbtabledisconnected.asp</TITLE>
6 </head>
7 <body bgcolor="#FFFFFF">
8 <%
9 ' displays a database in table form via GetRows
10 myDSN="DSN=Student;uid=student;pwd=magic"
11 mySQL="select * from publishers where state='NY'"
12 showblank=" "
13 shownull="-null-"
14
15 set conntemp=server.createobject("adodb.connection")
16 conntemp.open myDSN
17 ' to disconnect a recordset it must be created explicitly
18 set rstemp=server.createobject("adodb.recordset")
19 rstemp.cursorlocation=aduseclient
20 rstemp.open mySQL,conntemp
21 ' this line below disconnects the recordset
22 set rstemp.activeconnection=nothing
23
24 If rstemp.eof then
25 response.write "No records matched<br>"
26 response.write mySQL & "<br>So cannot make table..."
27 rstemp.close
28 set rstemp=nothing
29 conntemp.close
30 set conntemp=nothing
31 response.end
32 end if
33
34 response.write "<table border='1'><tr>"
35 'Put Headings On The Table of Field Names
36 for each whatever in rstemp.fields
37 response.write "<td><b>" & whatever.name & "</B></TD>"
38 next
39 response.write "</tr>"
40 DO UNTIL rstemp.eof
41 response.write "<tr>"
42 for each whatever in rstemp.fields
43 thisfield=whatever.value
44 if isnull(thisfield) then
45 thisfield=shownull
46 end if
47 if trim(thisfield)="" then
48 thisfield=showblank
49 end if
50 response.write "<td valign=top>" & thisfield & "</td>"
51 next
52 response.write "</tr>"
53 rstemp.movenext
54 LOOP
55 response.write "</table>"
56 %>
57 </body></html>
Display database table on Web
Page
Many Approaches by Charles Carroll
There are many ways to display a table on a web page. So many that we feel that a brief description of the variations and some URLs will help. A good programmer knows many ways to tackle the same problem!
Paging through arbitrary groups of records (i.e. record __ of __ ) at:Displaying a table without using a DSN, i.e. a DSNLess
connection at:
/learn/dbopen.asp
Use the faster Getrows function to
retrieve a recordset into an array:
/learn/dbtablegetrows.asp
Use
the lightning fast Getstring function to retrieve a recordset into a
string:
/learn/dbtablegetstring.asp
Using a very nice generic routine from Eli
Robillard at:
/learn/genericdb.asp
Use an excellent 3rd party component (ASPDB):
http://www.learnasp.com/learn/aspdb.asp
Displaying a Table with many records as quick as
possible is documented in depth at:
/learn/speedtables.asp
/learn/speedtablesall.asp
Displaying a Table using simple subroutines & includes at:
/learn/subdbtable.asp
Generic Database Display Made Easy
When you want a quick easy generic database
display, go on over to:
http://www.genericdb.com
Here Eli Robillard has done a lot of work for you. You modify one ASP file that specifies your database and query specs and his ASP scripts magically do the rest.
Here is an example where I make a pubs.asp designed to plug into his ASP scripts.
1 <%
2 ' Generic interface to the Northwinds Employee table.
3 Session("dbGenericPath") = "/learn/test/genericdb/"
4 Session("dbExitPage") = "http://www.activeserverpages.com"
5 Session("dbTitle") = "Pubs"
6 Session("dbType") = "SQL"
7 Session("dbConn") = "DSN=student;uid=student;pwd=magic"
8 Session("dbRs") = "Publishers"
9 Session("dbKey") = 1
10 Session("dbOrder") = 2
11 Session("dbRecsPerPage") = 10
12 Session("dbFooter") = 1
13 Sessio