Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to ASP.NET, Second Edition2 Chapter Objectives.

Similar presentations


Presentation on theme: "Introduction to ASP.NET, Second Edition2 Chapter Objectives."— Presentation transcript:

1

2 Introduction to ASP.NET, Second Edition2 Chapter Objectives

3 Introduction to ASP.NET, Second Edition3 Error Handling Error handling –Collection of techniques to identify programming errors Many errors caused by incorrect syntax or spelling –Easily identified using IntelliSense Write code that during program execution, can –Interpret error messages –Execute when an error is detected –Generate custom error messages and global error handlers

4 Introduction to ASP.NET, Second Edition4 Creating a Custom Error Page Using the Web Configuration File ErrorPage attribute of the Page property –Identifies the generic error page –When an error occurs, the client is redirected to this error page http://localhost/Chapter10/ nogarden.aspx?aspxerrorpath=/chapter10/garden.aspx The custom error page can also be set in the customErrors node in the Web.config file Status message codes exposed by the HTTPStatusCode property of the System.NET namespace

5 Introduction to ASP.NET, Second Edition5 Common Error Status Messages

6 Introduction to ASP.NET, Second Edition6 Creating a Custom Error Page Using the Web Configuration File (continued)

7 Introduction to ASP.NET, Second Edition7 Creating a Custom Error Page Using the Web Configuration File (continued)

8 Introduction to ASP.NET, Second Edition8 Using the Exception Classes to Identify Errors An exception –An object thrown when a predefined error occurs.NET System class contains an Exception class that acts as the base class for all exceptions SystemException class –Base class for all predefined exceptions ApplicationException –Base class to create user-defined exception objects

9 Introduction to ASP.NET, Second Edition9 Using the Exception Classes to Identify Errors (continued) Common exceptions –Handled with the Try-Catch-Finally statement –SqlException, OleDbException, NullReferenceException, and IndexOutOfRangeException SqlException –Thrown when an error occurs from the SQL Server DataAdapter –Often thrown when the database server does not exist

10 Introduction to ASP.NET, Second Edition10 Using the Exception Classes to Identify Errors (continued) OleDbException –error occurs from the OleDbDataAdapter NullReferenceException –error occurs when a null object is referenced IndexOutOfRangeException –Array object is improperly indexed The ExternalException –other classes to indirectly inherit from the SystemException class

11 Introduction to ASP.NET, Second Edition11 Using the Exception Classes to Identify Errors (continued) Exception object is created from the SystemException class Properties exposed from the SystemException class –Message property- TargetSite property –Helplink property- StackTrace property –InnerException property- ToString method

12 Introduction to ASP.NET, Second Edition12 Application-Level Error Handling Global Application File –Text file that –Contains scripts executed when Web application starts and ends; Browser session starts and ends –Only one Global Application File –Located in the root directory –Contains only server-side scripts

13 Introduction to ASP.NET, Second Edition13 Application-Level Error Handling (continued) Subroutines available in the Global Application File –Application_OnStart –Application_OnEnd –Session_OnStart –Session_OnEnd HttpContext class –Allows you to tetrieve information about an exception

14 Introduction to ASP.NET, Second Edition14 Application-Level Error Handling (continued) ToString method – fully qualified name of the object and other properties GetLastError method from HttpServerUtility class –Retrieve the last exception thrown by the application ClearError method from the HttpContext class –Clear all errors from the application Error property of the HttpContext class –Redirect the client to a new URL when a general exception occurs

15 Introduction to ASP.NET, Second Edition15 Creating a Global Error Page Using the Global Application File Upsize the TaraStore.mdb file to Ch10TaraStoreSQL Run Ch10TaraStoreSQL.bat file to create stored procedures Add data connection to Web Form1.aspx Copy connection string to CS key in the Web.config Save and build the project View CatMenu.aspx to test the data connection

16 Introduction to ASP.NET, Second Edition16 Web.config

17 Introduction to ASP.NET, Second Edition17 CatMenu.aspx

18 Introduction to ASP.NET, Second Edition18 Creating a Global Error Page Using the Global Application File (continued) Modify the Global.asax file Modify the Ch10Products.vb to cause an error –Change CS to CS2 Save and build solution Modify CustomError.aspx page Save and build solution View CatMenu.aspx page. Remove the error, save and build the solution

19 Introduction to ASP.NET, Second Edition19 Global.asax

20 Introduction to ASP.NET, Second Edition20 CustomError.aspx

21 Introduction to ASP.NET, Second Edition21 CatMenu.aspx

22 Introduction to ASP.NET, Second Edition22 Using Try-Catch-Finally to Handle Specific Errors Try-Catch-Finally statement –Run a block of code when an error has occurred –Goal - to gracefully recover when an exception occurs Try statement attempts to run a block of code If an error exists –An exception object is created –Catch statement catches the error as an exception –Use the Catch multiple times to catch multiple errors Finally statement - execute a block of code

23 Introduction to ASP.NET, Second Edition23 Using Try-Catch-Finally to Handle Specific Errors (continued)

24 Introduction to ASP.NET, Second Edition24 Using Try-Catch-Finally to Handle Specific Errors (continued)

25 Introduction to ASP.NET, Second Edition25 Using the Visual Studio.NET Debugger Visual Studio.NET provides several debugging tools to debug.NET applications A debugger –Step through the application at breakpoints, Defined points in the program where processing stops so that you can view the variables, properties, and other application settings –This data can be helpful when trying to locate programming logic errors

26 Introduction to ASP.NET, Second Edition26 Using the Visual Studio.NET Debugger (continued) Turned on debugging at page level or in Web.config <compilation debug="true" explicit="true" strict="true" defaultLanguage="vb"> Debug menu commands to step through the code: –Step Over (F10) –Step Into (F11) –Step Out (Shift+F11) –Run to Cursor (Ctrl+F10)

27 Introduction to ASP.NET, Second Edition27 Using the Visual Studio.NET Debugger (continued) Modify Web.config to turn the debugger on. Set the breakpoint on line 34 in CatMenuList.aspx Click the Debug from the menu bar, and then click Start Click Debug on the menu bar then Step Into Continue to step through 7 more times Click the Locals tab to view the Locals window Exit the debugger and remove the breakpoint

28 Introduction to ASP.NET, Second Edition28 Using the Visual Studio.NET Debugger (continued)

29 Introduction to ASP.NET, Second Edition29 Using the Visual Studio.NET Debugger (continued)

30 Introduction to ASP.NET, Second Edition30 Using the Visual Studio.NET Debugger (continued)

31 Introduction to ASP.NET, Second Edition31 Documenting the Web Site Important to document Web applications –Programmers can read the documentation –Quickly make the appropriate changes Comment Code Web Report Most of the report features will only work with C#

32 Introduction to ASP.NET, Second Edition32 Documenting the Web Site (continued)

33 Introduction to ASP.NET, Second Edition33 Documenting the Web Site (continued)

34 Introduction to ASP.NET, Second Edition34 Documenting the Web Site (continued)

35 Introduction to ASP.NET, Second Edition35 Page Output Caching –Technique of caching ASP.NET pages When a Web page is compiled, the page is stored on the server in the cache –When another request is made for the same page, the page from the cache is returned to the client –Storing Web pages in the cache increases the performance of your Web application

36 Introduction to ASP.NET, Second Edition36 Page Output Caching (continued) Configure the Page Output Cache in the Web page OutPutCache directive Duration parameter –How long in seconds the document is left in cache VaryByParam parameter –Cache any parameters passed with the page request

37 Introduction to ASP.NET, Second Edition37 Page Output Caching (continued) Additional techniques to control how the page is cached VaryByCustom – create custom strings to determine if a page should be cached VaryByHeader – control cached settings based on the HTTP header that is sent with the request VaryByControl – use fragment caching to cache one or more user controls on the Web page

38 Introduction to ASP.NET, Second Edition38 Page Output Caching (continued) Modify CatMenuCache.aspx Change Label lblMsg.Text = DateTime.Now.ToString("D") & _ " " & DateTime.Now.ToString("T") Change OutputCache <%@ OutputCache Duration="60"  VaryByParam="CatID, selItem" %>

39 Introduction to ASP.NET, Second Edition39 Page Output Caching (continued)

40 Introduction to ASP.NET, Second Edition40 Deploying an ASP.NET Web Application Previous versions of ASP to install a component: –Stop the Web service –Log into the Web server locally –Copy the component software to the server –Register the component using RegServ32.exe –Restart the Web Service Today – no stopping of the server or registration

41 Introduction to ASP.NET, Second Edition41 Deploying an ASP.NET Web Application (continued) Local application cache –default location for the assemblies for the local application Global assembly cache –location for assemblies available to the entire server –can be shared by multiple applications

42 Introduction to ASP.NET, Second Edition42 Deploying an ASP.NET Web Application (continued) To install a compiled DLL –Copy the DLL file to the bin directory (contains all of the code behind the page compiled into the assembly) –Copy the Web pages (.aspx) and accessory files (css, images etc) to the server AppDomain class manages the application process

43 Introduction to ASP.NET, Second Edition43 Chapter Summary Error handling allows you to identify programming errors Status message codes provide information about some errors The most common error message is the File Not Found The SystemException class is the base ex The Global Application File is a text file that contains scripts, Try-Catch-Finally statement allows you to attempt to run a block of code that detects when an error has occurred

44 Introduction to ASP.NET, Second Edition44 Chapter Summary (continued) A debugger allows you to step through the application at defined points called breakpoints The Code Comment Web Report contains information about the objects in your application Page Output Caching allows you to cache pages on the server Local application cache is the location for assemblies for the local application


Download ppt "Introduction to ASP.NET, Second Edition2 Chapter Objectives."

Similar presentations


Ads by Google