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

The Watch window allows developers to keep track of certain specified data during runtime.

Creating a Web Site

To demonstrate the Watch window we will create a simple web site with a label on it and add some code to modify it. At this point, I have created a new ASP.NET Empty Web Site. Next, we will want to add a Web Form and populate that with a label. To do this:

  1. Right click the project in your solution explorer.
  2. Select add new item… 
  3. Select a web form.
  4. Name it ‘Default.aspx’.
  5. Click add.
  6. Open Default.aspx up to design mode.
  7. Drag and drop a label onto the web form.

Next, we want to add some simple C# code that we can apply a breakpoint to and debug using the Watch window. To do this:

  1. Open Default.aspx.cs up for editing.
  2. Add in the following code to the Page_Load event method:
  3. Add in a breakpoint at the ‘Label1.Text = strText;’ line of code.

Using the Watch Window

To begin using the Watch window, we need to load up the web site with debugging. Once the web site stops at the breakpoint we added we can now use the Watch window to see our variables during runtime. To do this:

  1. From the top menu select Debug -> Windows -> Watch -> Watch1 to view the Watch window.
  2. In the name column, type in ‘strText’ and hit enter. This will now show you the value and type of the strText variable.
  3. Under this, type in ‘Label1’ to watch our label. Notice that the value column is populated with its data.
  4. To demonstrate what happens when you attempt to watch a variable that does not exist, under Label1 type in ASDF. Notice it gives you an error.

This demonstrates how to use the Watch window to keep track of your variables and data during runtime.

Download Source Files