.netCoders Contact Us
Search:

Debugging

After a few weeks of coding, you'll enter the inevitable task of debugging and resolving defects in your code. For this stage in the software lifecycle to be successful, you need to know how to debug and test your code, and how to resolve errors. For more information about unit testing in general, see our section on Unit Test Plans. For optimizing your application and testing it's performance under load, see the section on Optimization.

Configuring the Debugging Environment

Configuring your debugging environment is straightforward using Visual Studio.NET. Much like Visual C++ and Visual Basic, you can set breakpoints, enter debug mode, and the code will halt execution at your breakpoint, allowing you to step through your code line by line, and view variable values.

Unlike Visual Studio 6, which often required opening up multiple IDEs to debug your application, you can debug your application from within a single Visual Studio.NET instance, even when components are written in different languages.

When debugging, you want to verify that you are using the Debug build configuration. This will instruct the compiler to produce an additional file, with a .PDB extension, that contains the debug symbols for the assembly. On the toolbar, notice the dropdown that has options for Debug, Release, and Configuration Manager.

Be sure this is set to Debug, or your breakpoints will not work. A clear sign is a breakpoint with a question mark in it when in debug mode. If you see this, change the build configuration to Debug.

Applying Debugging Code

To add a breakpoint in your code, right-click a line of code, and select Insert Breakpoint. Alternatively, you can click in the left margin next to your code. A red circle will appear, and the line will be highlighted in red, indicating a breakpoint has been set.

Now select Debug->Start, or press F5. Visual Studio.NET will load your application and stop at your breakpoint. You can then select Debug->Step Into (F11) or Debug->Step Over (F10) to go through your source code line-by-line. Using the Locals window, you can see the values of all local variables. The Watch window allows you to indicate a specific variable to watch.

Debugging Windows

Within Visual Studio.NET, there are several windows that are used in the debugging process.

1. Autos
The Autos window shows variables used in the current statement, and in previous statements. It includes the variable name, data type, and value.

2. Locals
The Locals window displays variables currently in scope. It includes the variable name, data type, and value.

3. Watch
The Watch window displays variables that you declare watches for. You can add a variable to the Watch window while in Debug mode by dragging the variable name and dropping it on the Watch window, or right-clicking the variable name and selecting Add Watch. Like the other debugging windows, the Watch window displays the variable, data type, and value.

Task List

When you build your application, you may run into compile-time errors. These errors are translated into tasks in the Task List window in the Visual Studio.NET IDE. If you double-click the error in the task list, Visual Studio.NET will open the file and take you to the line of code where the error occurred.

Debugging Stored Procedures

Using Visual Studio.NET, you can also debug stored procedures. To do so, open Visual Studio.NET, and open the Server Explorer pane. Select your database server (or Right-click and Add Server... if it's not already listed), and navigate to Sql Servers tree menu item. Under there, select the database instance, database, and stored procedure you are interested in debugging. Right-click, and select Step Into Stored Procedure.

A dialog box will come up allowing you to enter parameter information, and then Visual Studio.NET will enter debug mode for the stored procedure. You can use the watch window and the other debugging tools to track SQL variables and step through the code.

Additional Resources