CIS 451: ASP.NET Debugging and Server-Side Includes Dr. Ralph D. Westfall February, 2009
Error Handling/Debugging client side vs. server scripts syntax errors logical errors good coding practices to minimize errors testing your code debugging statements
Need to Identify as Server Script code will run on client if doesn't have: <% [code] %> or [code] filetype needs to be.aspx, not.htm(l)
Syntax Errors spelling Reesponse.Write("something") EndIf '2 words: needs a space missing parts of control structures For but no Next If but no End If extra parts for control structures extra Next or extra End If at end
Syntax Errors - 2 using a method as a property Response.Write = "Welcome" don't use = with methods using a property as a method buffer("true") properties are values, so need to use =
Syntax Errors - 3 writing to a "read only" property Dim prices() As Double = {3.00, 2.00} prices.Length = 5 'read only property using Set to create an object Set objMail = _ Server.CreateObject("CDO.Message") 'remove Set (ASP 3.0 still needs Set)
Logical Errors division by a variable that has value of zero if Option Explicit or Strict is not On a = 2 c = a / b 'no value given for b type mismatch e.g. can't do arithmetic with text 5 / "hello" missing period can cause a type mismatch a=CInt(RequestQueryString("card")) 5 / a
Good Coding Practices indent code inside loops and conditionals "pretty listing" (automatic in VS.NET) For intI = 0 to 2 intJ = intI Next I use comments where appropriate 'single quote creates a comment
Good Coding Practices - 2 use Option Strict at start of file forces you to use Dim for all variables helps spot spelling mistakes forces you to handle all type conversions explicitly txtCount.Text=CStr(intCount)
Good Coding Practices - 3 use consistent object naming prefixes btnSubmit 'a button txtLastName 'a textbox for data capitalize function names e.g. conversions CInt(7.3) 'Convert to Integer CCur(9.9) 'Convert to Currency
Good Coding Practices - 4 type checking: is data what you expect? If IsNumeric(Request.Form("num")) Then intTot = intTot + intNum End If type conversion: change data so it is usable intValue = CInt(dblItem1 + dblItem2)
Good Coding Practices - 5 use subprocedures and/or functions for anything substantial (5 or more lines?) that gets done in more than 1 place advantages: shorter program easier to debug and maintain may be able to reuse them in other programs
Good Coding Practices - 6 use "server-side includes" in.aspx filesserver-side includes <% 'prices.aspx file Dim dblProdPrice(11) dblProdPrice(0)=29.99 'etc. %> advantages similar to subprocedures, etc.See Notes Page
Testing Your Code test the different ways a user could go through the application example: shopping cart with 2 catalog pagesshopping cart entry page to catalog page 1 to checkout entry page to catalog page 2 to checkout entry, catalog 1, catalog 2, checkout entry, catalog 2, catalog 1, checkout using browser Back button instead of form button? using link instead of button?
Testing Your Code - 2 test possible data values example: zip codes boundary values (included, or not?) and values in between boundaries to out-of-bounds values negative #s? 6 digit #s? text #s? (ninety thousand and one)
VS Debugging Capabilities break points and icons for stepping through code Step Into, Step Over, Step Out special windows Locals, Output, Watch, View Stack, etc.
Debugging Statements insert "trace" statements to determine values before code where problem is Response.Write([variable name]) use so you can see form input values in query string MsgBox to show specific values
Debugging with "Include" Files some programmers put tracing procedures in separate file add #include file=[trace file name] at top of file being debugged need to then insert calls to trace file procedures in code NET tracing capabilities
More on Server Side Includes special features for HTML files only? extension is.shtml or.shtm so server knows that special processing required #fsize - gives file size #flastmod - gives file last modified date include works in both ASP and HTML # include - includes text (code) from another file so it is processed as if it is in the current file
Server Side Include Issues data is text, so could use.txt as extension can hide file by using.aspx extension Microsoft recommends.inc extension to conveniently identify include files convenient way to keep data in programs up-to-date e.g., put shopping cart product prices data in an include, then update just that file
Using Server Side Includes in ASP.NET need to put "directive" in HTML comments 'path not needed if in current directory can use physical path (address on drive) can also use virtual path (server address)
SSI Exercise view Using Server Side Includes click "basic page," View>Source, edit URLs and Save As basic.shtmlbasic page note: save it as.shtml, not.html also save code for the other linked pages (includes) in same directory modify include files view effects on basic.shtml in browser