You are on page 1of 16

Debugging

Errors
• Types
• Syntax Errors
• Semantic Errors
• VS.NET automatically gives us syntax
notifications while we edit the code and marks
out the syntax errors. This support extends not
only to the programming language but also XML.
• VS.NET also has TaskList that displays a list of
errors.
What is Debugging?
• Debugging is the process of tracking and removing
logical or runtime errors in the program.
• Debugging is achieved through Tracing.
• Tracing is the process of collecting information about a
program’s execution.

• Of course easiest way to debug is by using writing using


Console.WriteLine or Response.Write. But there
is a serious disadvantage with this approach. Can you
figure out what?
Tracing
• Two different ways of implementing
tracing in .NET
• using System.Web.TraceContext class
• Using System.Diagnostics.Trace and
System.Diagnostics.Debug classes
System.Web.TraceContext
• This class is responsible for colecting the
execution details of a web request.
• The TraceContext object for the current request
can be accessed through the Trace property of
the Page class.
• By default, tracing is not enabled. To enable at
the page level set the Trace property to true.
• <% @Page Language=“C#" Trace="True" %>
• Using the TraceContext’s write method
debugging messages can be added.
• Create a web form and the debug mode to true.
<%@ Page Language="C#" Trace="true"
AutoEventWireup="true" CodeFile="Default4.aspx.cs"
Inherits="Default4" %>

• Writing debug info
protected void Page_Load(object sender, EventArgs e)
{
Trace.Write("Page_Load method called");
Trace.Warn("Only initilaization should be
done here");
}
• On execution:
Note that if debug mode is
switched off you don’t see these
messages!
To categorize:
Trace.Write("[category]"
,"[message]")
Setting Trace for the entire
application
• In web.config

<system.web>
<trace enabled="true" pageOutput="true"/>
</system.web>
Trace information listing
• As we see in the output, trace lists
• Request details
• Trace information: Four column output: Category,
Message, Time from the first trace item, Duration of
this trace item.
• Control tree: the entire collection of controls in the
ASP.NET page displayed hierarchically.
• Collection information: any defined members of the
following collections will also be displayed:
• Session state
• Cookies
• Headers
• Form
• Querystring
• Server variables
System.Diagnostics
• Debug class and Trace class
• VS.NET provide two basic configurations for a project :
• Debug (the default)
• Release
• When a program is compiled using the Debug
configuration both the TRACE and DEBUG conditional
compilation symbols are defined
• In the Release configuration only trace information is
compiled.
• These modes are configurable via the property pages of
the VS.NETsolution.
• Examples:
• Debug.Assert(value<0, "Invalid
value","negative value employed in
debug mode");
Trace.Writeline(value,"Program trace,
value of value");
Starting a VS.NET Debugger
• By default when .aspx files are compiled the
debugging symbols are automatically inserted
into the compiled page.
• This is because the in web.config file by default
the debug is set to true for compilation.
<system.web>
...
<compilation debug="true"/>
...
</system.web>
• Because this affects performance, set this value
to true only during development.
To correct errors at F5 debug mode
• To execute the application from VS.NET, we click ‘Start
Debugging’. When application encounters a runtime
error, automatically a dialog box pops up and the line
which caused the error is indicated.
• Drag a textbox, button and a label. Lets us calculate
square of a number on clicking the button.
<script runat="server">
protected void Button1_Click(object sender,
EventArgs e){
int i =Int32.Parse( TextBox1.Text);
int sqr=i*i;
Label1.Text = " Square of the number is
" + sqr;
}
</script>
• Execute with the right inputs first.
• Now type incorrect value and see what happens.

• Either click continue or to stop further


execution click Stop Debugging.
Setting Breakpoints
• A debugger is a program allows you to control
the execution of another program and examine
and change the program’s state (change the
variable values).
• Breakpoint is the point you set in your code so
that program executes up to that point and then
halts.
• You could examine the program state etc. at that
point and then continue the execution until the
next break point or until the program finishes its
execution.
Click on the bar provided
to insert/delete break point
Execute with the break point set

Debugging icons
F5
Continue Stop Restart
Debugging Stored Procedures
• Database projects and SQL Debugging
facilities are available in VS professional
or Team Editions.
• Step Into facility can be used with VS to
debug stored procedures defined in SQL
server 2005.
• Break points can also be set in the stored
procedure and F5 could be used to
execute up to that point.
IIS Vs ASP.NET Development server
• The VS.NET come with a minimal server which is just a
subset of IIS. This sever is called ASP.NET Development
server.
• When F5 is pressed, if web.config is not present in the
web application, a dialog box automatically appears which
prompts to add web.config with debugging mode
enabled.
• As soon as this happens, the ASP.NET Development
Server starts and the browser window opens.
• As soon as the browser is close the application server
shuts down.
• Note that in case the application requires security context
such as anonymous users or a specific domain user IIS
must be use.
We will encounter this in security section

You might also like