This tutorial is the third part of the ASP.NET Web Site Debugging tutorial series. This tutorial will demonstrate how to use Breakpoints in Microsoft Visual Studio 2010 with ASP.NET 4.0 and C#.

One of the most useful debugging tools offered in Microsoft Visual Studio is the ability to add breakpoints to the project, which will pause the program at a given line of code. This allows you to let a portion of the program run, pause it and check what is going on, then continue to run the program. This can be used for many different scenarios.

Creating a Web Site

To demonstrate the breakpoints, we will need to make a simple website that allows us to add breakpoints to the code behind. At this point, I have created a new ASP.NET Empty Web Site. Next, we will add a Web Form. To do this:

  1. Right click the project in your Solution Explorer.
  2. Select Add New Item…
  3. Choose a Web Form.
  4. Name it Default.aspx.
  5. Click Add.

Next, we want to add a Button Control to the web form that we will use to add breakpoints to in code. To do this:

  1. Open Default.aspx to Design mode.
  2. Drag and drop a button onto the Web Form.

Next, we will add some code to our Default.aspx.cs code behind class. To do this, double click the Button we added to the Web Form to generate the Click event method for that button. Then, in the Page_Load event method of the class, add the following code:

This will simply set the Text of the button, but that’s not really important right now. What is important is how we are going to add a breakpoint to this code to stop the flow of the program when it reaches this point.

Adding Breakpoints

There are a few different ways to add breakpoints to code in Microsoft Visual Studio 2010. The first way we will do this is by click the small gray column to the left of the code area. Clicking on this column, to the left of a line of code will add a break point. (Note: There must be code on the line for a breakpoint to be added.) To do this, click the gray area to the left of our ‘Button1.Text = “Click Me”;’ line of code. A red circle will be displayed, indicating that a breakpoint has been added.

To demonstrate what is actually happening here, go ahead and load up the website in debug mode. Notice, that the web form we created is never loaded because the program halts when it reaches our line of code, before the page load event. If you want to continue running the program after the breakpoint, simply select from the top menu Debug -> Continue.

There are also other ways to add break points to the code, in order to demonstrate this we will need to be viewing the Breakpoints window. We can enable this from the top menu by navigating to Debug -> Windows -> BreakPoints.

Now we should be viewing the Breakpoints window. From this window, we can choose New -> Break at Function… to add a new breakpoint at a given method. In this case, let’s add one to the Button1_Click event method we generated earlier.

  1. In the Breakpoints window select New -> Break at Function.
  2. Enter ‘Button1_Click’ for the function and hit OK.

Diabling Breakpoints

It is important to understand that you can disable breakpoints without actually deleting them. When you want to stop a breakpoint from being active you can simply right click on it and select Disable Breakpoint. This will allow you to keep breakpoints in specific places that don’t always have to be active. Furthermore, you can delete them just as simply by right clicking and choosing Delete Breakpoint.

Adding Conditions to Breakpoints

Another great feature that breakpoints have is the ability to be bound to a condition. This means,that you can setup a breakpoint that will stop only under a given circumstance. Let’s test this out by adding a condition to the breakpoint we added to the ‘Button1.Text = “Click Me”;’ line of code. To do this:

  1. Right click the breakpoint and select Condition…
  2. In the Condition TextBox type in ‘Button1.Text == “ASDF”‘.
  3. Click OK.

To test this, load up the website with debugging and notice that the breakpoint no longer stops the program from executing fully. This is because the breakpoint will now only be triggered if the Button1.Text property was equal to “ASDF”. Breakpoints are a great debugging resource because they allow you to run only portions of code at a time which can be great for debugging large projects.

Download Source Files