- •Table of Contents
- •Preface
- •What is ASP.NET?
- •Installing the Required Software
- •Installing the Web Server
- •Installing Internet Information Services (IIS)
- •Installing Cassini
- •Installing the .NET Framework and the SDK
- •Installing the .NET Framework
- •Installing the SDK
- •Configuring the Web Server
- •Configuring IIS
- •Configuring Cassini
- •Where do I Put my Files?
- •Using localhost
- •Virtual Directories
- •Using Cassini
- •Installing SQL Server 2005 Express Edition
- •Installing SQL Server Management Studio Express
- •Installing Visual Web Developer 2005
- •Writing your First ASP.NET Page
- •Getting Help
- •Summary
- •ASP.NET Basics
- •ASP.NET Page Structure
- •Directives
- •Code Declaration Blocks
- •Comments in VB and C# Code
- •Code Render Blocks
- •ASP.NET Server Controls
- •Server-side Comments
- •Literal Text and HTML Tags
- •View State
- •Working with Directives
- •ASP.NET Languages
- •Visual Basic
- •Summary
- •VB and C# Programming Basics
- •Programming Basics
- •Control Events and Subroutines
- •Page Events
- •Variables and Variable Declaration
- •Arrays
- •Functions
- •Operators
- •Breaking Long Lines of Code
- •Conditional Logic
- •Loops
- •Object Oriented Programming Concepts
- •Objects and Classes
- •Properties
- •Methods
- •Classes
- •Constructors
- •Scope
- •Events
- •Understanding Inheritance
- •Objects In .NET
- •Namespaces
- •Using Code-behind Files
- •Summary
- •Constructing ASP.NET Web Pages
- •Web Forms
- •HTML Server Controls
- •Using the HTML Server Controls
- •Web Server Controls
- •Standard Web Server Controls
- •Label
- •Literal
- •TextBox
- •HiddenField
- •Button
- •ImageButton
- •LinkButton
- •HyperLink
- •CheckBox
- •RadioButton
- •Image
- •ImageMap
- •PlaceHolder
- •Panel
- •List Controls
- •DropDownList
- •ListBox
- •RadioButtonList
- •CheckBoxList
- •BulletedList
- •Advanced Controls
- •Calendar
- •AdRotator
- •TreeView
- •SiteMapPath
- •Menu
- •MultiView
- •Wizard
- •FileUpload
- •Web User Controls
- •Creating a Web User Control
- •Using the Web User Control
- •Master Pages
- •Using Cascading Style Sheets (CSS)
- •Types of Styles and Style Sheets
- •Style Properties
- •The CssClass Property
- •Summary
- •Building Web Applications
- •Introducing the Dorknozzle Project
- •Using Visual Web Developer
- •Meeting the Features
- •The Solution Explorer
- •The Web Forms Designer
- •The Code Editor
- •IntelliSense
- •The Toolbox
- •The Properties Window
- •Executing your Project
- •Using Visual Web Developer’s Built-in Web Server
- •Using IIS
- •Using IIS with Visual Web Developer
- •Core Web Application Features
- •Web.config
- •Global.asax
- •Using Application State
- •Working with User Sessions
- •Using the Cache Object
- •Using Cookies
- •Starting the Dorknozzle Project
- •Preparing the Sitemap
- •Using Themes, Skins, and Styles
- •Creating a New Theme Folder
- •Creating a New Style Sheet
- •Styling Web Server Controls
- •Adding a Skin
- •Applying the Theme
- •Building the Master Page
- •Using the Master Page
- •Extending Dorknozzle
- •Debugging and Error Handling
- •Debugging with Visual Web Developer
- •Other Kinds of Errors
- •Custom Errors
- •Handling Exceptions Locally
- •Summary
- •Using the Validation Controls
- •Enforcing Validation on the Server
- •Using Validation Controls
- •RequiredFieldValidator
- •CompareValidator
- •RangeValidator
- •ValidationSummary
- •RegularExpressionValidator
- •Some Useful Regular Expressions
- •CustomValidator
- •Validation Groups
- •Updating Dorknozzle
- •Summary
- •What is a Database?
- •Creating your First Database
- •Creating a New Database Using Visual Web Developer
- •Creating Database Tables
- •Data Types
- •Column Properties
- •Primary Keys
- •Creating the Employees Table
- •Creating the Remaining Tables
- •Executing SQL Scripts
- •Populating the Data Tables
- •Relational Database Design Concepts
- •Foreign Keys
- •Using Database Diagrams
- •Diagrams and Table Relationships
- •One-to-one Relationships
- •One-to-many Relationships
- •Many-to-many Relationships
- •Summary
- •Speaking SQL
- •Reading Data from a Single Table
- •Using the SELECT Statement
- •Selecting Certain Fields
- •Selecting Unique Data with DISTINCT
- •Row Filtering with WHERE
- •Selecting Ranges of Values with BETWEEN
- •Matching Patterns with LIKE
- •Using the IN Operator
- •Sorting Results Using ORDER BY
- •Limiting the Number of Results with TOP
- •Reading Data from Multiple Tables
- •Subqueries
- •Table Joins
- •Expressions and Operators
- •Transact-SQL Functions
- •Arithmetic Functions
- •String Functions
- •Date and Time Functions
- •Working with Groups of Values
- •The COUNT Function
- •Grouping Records Using GROUP BY
- •Filtering Groups Using HAVING
- •The SUM, AVG, MIN, and MAX Functions
- •Updating Existing Data
- •The INSERT Statement
- •The UPDATE Statement
- •The DELETE Statement
- •Stored Procedures
- •Summary
- •Introducing ADO.NET
- •Importing the SqlClient Namespace
- •Defining the Database Connection
- •Preparing the Command
- •Executing the Command
- •Setting up Database Authentication
- •Reading the Data
- •Using Parameters with Queries
- •Bulletproofing Data Access Code
- •Using the Repeater Control
- •More Data Binding
- •Inserting Records
- •Updating Records
- •Deleting Records
- •Using Stored Procedures
- •Summary
- •DataList Basics
- •Handling DataList Events
- •Editing DataList Items and Using Templates
- •DataList and Visual Web Developer
- •Styling the DataList
- •Summary
- •Using the GridView Control
- •Customizing the GridView Columns
- •Styling the GridView with Templates, Skins, and CSS
- •Selecting Grid Records
- •Using the DetailsView Control
- •Styling the DetailsView
- •GridView and DetailsView Events
- •Entering Edit Mode
- •Using Templates
- •Updating DetailsView Records
- •Summary
- •Advanced Data Access
- •Using Data Source Controls
- •Binding the GridView to a SqlDataSource
- •Binding the DetailsView to a SqlDataSource
- •Displaying Lists in DetailsView
- •More on SqlDataSource
- •Working with Data Sets and Data Tables
- •What is a Data Set Made From?
- •Binding DataSets to Controls
- •Implementing Paging
- •Storing Data Sets in View State
- •Implementing Sorting
- •Filtering Data
- •Updating a Database from a Modified DataSet
- •Summary
- •Security and User Authentication
- •Basic Security Guidelines
- •Securing ASP.NET 2.0 Applications
- •Working with Forms Authentication
- •Authenticating Users
- •Working with Hard-coded User Accounts
- •Configuring Forms Authentication
- •Configuring Forms Authorization
- •Storing Users in Web.config
- •Hashing Passwords
- •Logging Users Out
- •ASP.NET 2.0 Memberships and Roles
- •Creating the Membership Data Structures
- •Using your Database to Store Membership Data
- •Using the ASP.NET Web Site Configuration Tool
- •Creating Users and Roles
- •Changing Password Strength Requirements
- •Securing your Web Application
- •Using the ASP.NET Login Controls
- •Authenticating Users
- •Customizing User Display
- •Summary
- •Working with Files and Email
- •Writing and Reading Text Files
- •Setting Up Security
- •Writing Content to a Text File
- •Reading Content from a Text File
- •Accessing Directories and Directory Information
- •Working with Directory and File Paths
- •Uploading Files
- •Sending Email with ASP.NET
- •Configuring the SMTP Server
- •Sending a Test Email
- •Creating the Company Newsletter Page
- •Summary
- •The WebControl Class
- •Properties
- •Methods
- •Standard Web Controls
- •AdRotator
- •Properties
- •Events
- •BulletedList
- •Properties
- •Events
- •Button
- •Properties
- •Events
- •Calendar
- •Properties
- •Events
- •CheckBox
- •Properties
- •Events
- •CheckBoxList
- •Properties
- •Events
- •DropDownList
- •Properties
- •Events
- •FileUpload
- •Properties
- •Methods
- •HiddenField
- •Properties
- •HyperLink
- •Properties
- •Image
- •Properties
- •ImageButton
- •Properties
- •Events
- •ImageMap
- •Properties
- •Events
- •Label
- •Properties
- •LinkButton
- •Properties
- •Events
- •ListBox
- •Properties
- •Events
- •Literal
- •Properties
- •MultiView
- •Properties
- •Methods
- •Events
- •Panel
- •Properties
- •PlaceHolder
- •Properties
- •RadioButton
- •Properties
- •Events
- •RadioButtonList
- •Properties
- •Events
- •TextBox
- •Properties
- •Events
- •Properties
- •Validation Controls
- •CompareValidator
- •Properties
- •Methods
- •CustomValidator
- •Methods
- •Events
- •RangeValidator
- •Properties
- •Methods
- •RegularExpressionValidator
- •Properties
- •Methods
- •RequiredFieldValidator
- •Properties
- •Methods
- •ValidationSummary
- •Properties
- •Navigation Web Controls
- •SiteMapPath
- •Properties
- •Methods
- •Events
- •Menu
- •Properties
- •Methods
- •Events
- •TreeView
- •Properties
- •Methods
- •Events
- •HTML Server Controls
- •HtmlAnchor Control
- •Properties
- •Events
- •HtmlButton Control
- •Properties
- •Events
- •HtmlForm Control
- •Properties
- •HtmlGeneric Control
- •Properties
- •HtmlImage Control
- •Properties
- •HtmlInputButton Control
- •Properties
- •Events
- •HtmlInputCheckBox Control
- •Properties
- •Events
- •HtmlInputFile Control
- •Properties
- •HtmlInputHidden Control
- •Properties
- •HtmlInputImage Control
- •Properties
- •Events
- •HtmlInputRadioButton Control
- •Properties
- •Events
- •HtmlInputText Control
- •Properties
- •Events
- •HtmlSelect Control
- •Properties
- •Events
- •HtmlTable Control
- •Properties
- •HtmlTableCell Control
- •Properties
- •HtmlTableRow Control
- •Properties
- •HtmlTextArea Control
- •Properties
- •Events
- •Index
Chapter 5: Building Web Applications
this effect could have been achieved just as easily through CSS, but in future, when you’re working on projects that utilize more complex controls and properties, skins might be your only choice. As such, it’s important that you know how to use them.
Debugging and Error Handling
Your work with Dorknozzle for this chapter is over, but now that we’ve started to create a real-world application, it’s time to consider the real-world problems that might occur as we’re developing that application. A constant truth in the life of any programmer is that programming mistakes do happen, and they happen no matter how experienced the programmer is. For this reason, it’s beneficial to know what you can do when you encounter an error, and to learn how ASP.NET and Visual Web Developer can help you analyze and debug your code.
Debugging with Visual Web Developer
Take a look at this code:
Visual Basic |
File: ErrorTest.aspx.vb (excerpt) |
Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load
Dim a(10) As Integer Dim i As Integer
For i = 1 To 11 a(i) = i
Next
End Sub
C# File: ErrorTest.aspx.cs (excerpt)
protected void Page_Load(object sender, EventArgs e)
{
int[] a = new int[10]; int i;
for (i = 0; i < 11; i++)
{
a[i] = i;
}
}
204
Debugging with Visual Web Developer
The code above creates an array of ten elements, then uses a For loop to assign values to them. The problem is that it doesn’t stop at the tenth element: it also tries to assign a value to the eleventh element, which doesn’t exist.
If you load this page directly in Internet Explorer without debugging it, you’ll see a page that specifies the error, like the one shown in Figure 5.46.
Figure 5.46. The error message isn’t very helpful without debug mode
You can obtain more details by enabling debug mode and, if you scroll down, you’ll see instructions that explain how to do just that. The easiest way to enable debug mode it to use Visual Web Developer. You’ll remember from earlier in this chapter that the first time you execute a page by pressing F5 (Start Debugging), Visual Web Developer asks you if you want it to enable debug mode for you. If you ask it to, it’ll modify (or create) the Web.config file accordingly.
205
Chapter 5: Building Web Applications
Figure 5.47. Debugging a run-time error
Executing the page once again—this time, with debugging enabled—takes you straight to the error in Visual Web Developer, as Figure 5.47 illustrates.
This interface tells you that the code has thrown an exception of type IndexOutOfRangeException. In .NET, exceptions are the standard means by which errors are generated and propagated. An exception is a .NET class (in this case, the IndexOutOfRangeException class) that contains the details of an error. As you’ll see a little later, you can catch the error in your code using the Try-Catch-Finally construct. If the error isn’t caught and handled, as in this case, it’s finally caught by the ASP.NET runtime, which generates an error message.
In Figure 5.47, the debugger has paused execution at the moment the exception was raised. Let’s see what your options are at this moment. One very useful window is the Watch window, which appears by default when your application is being debugged. If it’s not displayed, you can open it by accessing Debug > Windows > Watch. You can type the names of the objects in your code into the
206
Debugging with Visual Web Developer
Watch window; in response, it will display their values and types. Try typing a(5) (or a[5] if you’re using C#) in the Watch window; you should see a display like the one in Figure 5.48.
Figure 5.48. Inspecting values using the Watch window
You could even type just a, then explore its members via the display shown in Figure 5.49.
Figure 5.49. The Watch window showing the contents of an array
Arrays and VB
This example reveals an interesting aspect of this array. The Watch window reports that the array’s length is 11, yet we defined it as a(10). In all .NET languages, arrays are zero-based, which means that the first element of an array is a(0), the second is a(1), and so on. So an array called a that had ten elements would have as its first element a(0), and a(9) as its last.
However, VB offers extra assistance for developers who are experienced with pre-.NET versions of the language (which had one-based arrays in which the first element would have been a(1), and the last would have been a(10)): it adds an element for you. In other words, if you declare an array of ten elements in VB, you’ll get an array of 11 elements.
C# has always had zero-based arrays, so an array defined as a[10] will have ten elements.
207
Chapter 5: Building Web Applications
In more complex scenarios, if you enter the name of an object, the Watch window will let you explore its members as we just saw.
If you switch to the Locals window (Debug > Windows > Locals) shown in Figure 5.50, you can see the variables or objects that are visible from the line of code at which the execution was paused.
Figure 5.50. The Locals window
Another nice feature of Visual Web Developer is that when you hover your cursor over a variable, the editing window shows you at-a-glance information about that variable.
Sometimes, you’ll want to debug your application even if it doesn’t generate an exception. For example, you may find that your code isn’t generating the output you expected. In such cases, it makes sense to execute pieces of code line by line, and see in detail what happens at each step.
The most common way to get started with this kind of debugging is to set a breakpoint in the code. In Visual Web Developer, we do this by clicking on the gray bar on the left-hand side of the editing window. When we click there, a red bullet appears, and the line is highlighted with red to indicate that it’s a breakpoint, as Figure 5.51 illustrates.
Once the breakpoint is set, we execute the code. When the execution pointer reaches the line you selected, execution of the page will be paused and Visual Web Developer will open your page in debug mode. In debug mode, you can perform a number of tasks:
View the values of your variables or objects.
Step into any line of code by selecting Debug > Step Into. This executes the currently highlighted line, then pauses. If the selected line executes another local method, the execution pointer is moved to that method so that you can execute it line by line, too.
208
Debugging with Visual Web Developer
Figure 5.51. Setting a breakpoint
Step over any line of code by selecting Debug > Step Over. This makes the execution pointer move to the next line in the current method without stepping into any local methods that might be called by the current line.
Step out of any method by selecting Debug > Step Out. This causes the current method to complete and the execution to be paused on the next line of the method that called the current method.
Continue execution of the program normally by selecting Debug > Continue. Execution will stop again only if an exception is raised, or another breakpoint is met. If the execution is stopped as a result of an exception, choosing to continue the execution will allow the error to propagate to the ASP.NET runtime, which will cause the error message to display in the browser window.
Stop execution by selecting Debug > Stop Debugging.
Stop and restart the program by selecting Debug > Restart.
209