Stop Debugging doesn't stop in ASP.NET

M

Matt Theule

While stepping through an ASP.NET project, I found that data was being
inserted into my database even though I was not stepping through the
code that inserted the data.

I have a single page with inline code. The page has a Datagrid, a
textbox and a button. When the button is clicked, the value of the
textbox is inserted into the table whose contents are displayed on the
page.

The problem occurs when I set a breakpoint on a line *IN* the Button1
click event. The line with the breakpoint is *IN* the event, but before
any database code is called. When the breakpoint is hit, I click the
'Stop' button, to end the debugging session. Because I have not yet
stepped through any database insertion code, I expect that no values
will be inserted in the database.

However, it appears that once the event is fired (the breakpoint is
*INSIDE* the click event), the whole event code is executed, even though
the Stop button was pressed.

Is there a way to force the ASP.NET debugging session to actually end
when I stop debugging?

Thanks

MATT

Code to reproduce the behavior included below.

<!-- BEGIN CODE -->

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<HTML>
<script language="VB" runat="server">
private msConn as string

Sub Page_Load(Sender As Object, E As EventArgs)
#If DEBUG Then
Stop
'Check the contents of the Categories table in the Northwind
database so that
' you will know when a new value has been inserted.
#End If
BindData()
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
#IF DEBUG THEN
STOP

'Notice that we have not stepped through any code that will
insert
' values into the database.
'Use SQL Enterprise Manager to examine the contents of the
Categories
' table of the Northwind database. There should not be
any new values
'To test the db insert, click Debug | Stop Debugging to stop
the project.
' Then hit F5 to restart the project and view the contents
of the
' drop down list.
'You will see that the value in the text box is now included
in the
' data grid. The value from the text box was written to
the db
' even though we did not call any code that would insert
to the db.
#END IF

Dim Cmd As SqlClient.SqlCommand
Dim sSQL As String
Try
Cmd = New SqlClient.SqlCommand()
sSQL = "INSERT INTO Categories(CategoryName) VALUES('" &
Me.txtCategory.Text & "')"
With Cmd
.CommandText = sSQL
.CommandType = CommandType.Text
.Connection = New SqlClient.SqlConnection(msConn)
.Connection.Open()

.ExecuteNonQuery()
.Connection.Close()
End With

'Refresh
BindData()

Catch exp As Exception
Throw exp
Finally
Cmd.Connection.Close()
End Try
End Sub

Private Sub BindData()
Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter
msConn = "server=(local);database=Northwind;user id = sa;"
MyConnection = New SqlConnection(msConn)
MyCommand = New SqlDataAdapter("select * from Categories",
MyConnection)

DS = New DataSet()
MyCommand.Fill(DS, "Categories")

MyDataList.DataSource = DS.Tables("Categories").DefaultView
MyDataList.DataBind()
End Sub
</script>
<body>
<form id="Form1" method="post" runat="server">
<b>Category ID | Category </b>
<br>
<ASP:DataList id="MyDataList" RepeatColumns="1" runat="server"
BorderColor="#CC9966" BorderStyle="None" BackColor="White"
CellPadding="4" GridLines="Both" BorderWidth="1px">
<ItemTemplate>
<DIV style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; FONT-SIZE: 10pt;
PADDING-BOTTOM: 5px; PADDING-TOP: 5px; FONT-FAMILY: Verdana">
<%# DataBinder.Eval(Container.DataItem, "categoryid") %>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |
&nbsp;&nbsp;&nbsp;&nbsp;<%# DataBinder.Eval(Container.DataItem,
"CategoryName") %></DIV>
</ItemTemplate>
</ASP:DataList>
<asp:TextBox id="txtCategory" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"
OnClick="Button1_Click"></asp:Button>
</form>
</body>
</HTML>

<!-- END CODE -->
 
J

John Saunders

Matt,

I would be very surprised if the debugger is continuing after you click
"Stop".

I suggest you add something like this:

throw new Exception("Try getting past this!");

on the line after your breakpoint. Hit "Stop" and see what happens.

Also, be _really_ sure that the "added" record is actually being added when
you think it is. For instance, if your table uses an identity column for the
primary key, check the highest key before and after your test.

I might also try commenting out the database insert code entirely. If it's
not there, and if the record got inserted, then it wasn't inserted by the
(non-existant) database insert code.
 
A

Alvin Bruney

i can kinda see his point, most times if you stop on a line before an
assignment in the debugger, the debugger has already made the assignment and
is showing it in the watch window. go figure. something is strange indeed.
 
J

John Saunders

i can kinda see his point, most times if you stop on a
line before an assignment in the debugger, the debugger
has already made the assignment and is showing it in the
watch window.

Interesting. I've never noticed that.

I have noticed the debugger displaying the old value of a variable in
the Autos window when you break before the assignment, and the new value
when you single-step past it. But I've never noticed it showing the new
value before it's set.


John Saunders
(e-mail address removed)
 
M

Matt Theule

I would be very surprised if the debugger is
continuing after you click "Stop".

Yes, I am too, but that is what is happening.

I added the throw new exception code you suggested to the code I posted
in my original message. When I hit the break point *INSIDE* the event
handler before the exception throwing, I clicked the "Stop" button. I
very briefly saw the web page change to the yellow, white and red of an
exception. At the top of the page was "Try to get past this". Then the
browser closed as the project finished stopping.

I emphasize that the break point must be inside the event handling
method because if you put the break point on the 'Public Sub
Button1_Click (byval ...' line, and click the stop button, the project
stops as expected, and nothing surprising happens. It is as if once the
event handler code has been engaged to any degree, Visual Studio is
committed to completing the method, even after the stop button is
pressed.

Please run the demo that I posted in my original message. I have
included STOP commands with notes to check the db at various points.
The code uses the Categories table of the Northwind database. The code
is very simple and straight forward so there is no possibility that the
data could be inserted from a different method. The Categories table
does in fact use an identity column, so it is easy to see that there is
a new, higher identity category in the table.

Thanks

MATT
 
M

Matt Theule

You are right, of course, the database access does add a layer of
complexity. However, the database access also allows for persistence of
results.

Below you will find a stripped down version that includes only the
throwing of a new exception.

I only very briefly see the exception displayed in the browser, and I
would imagine on a faster machine (I am running PIII - 1 GHz, 512 RAM)
that it would be even harder to see the display of the exception, making
the exception seem not to appear.

On my co-worker's P4 1.5 Mhz 256 RAM, the exception was visible, again,
only briefly. 2 monitors made it easier to see.

Thanks

MATT

<!-- BEGIN CODE -->
<HTML>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
#IF DEBUG THEN
STOP
'Click the "Stop Debugging" button and watch the browser display
' the exception below.
#END IF
throw new exception("Try to get past this!")
End Sub
</script>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Button"
OnClick="Button1_Click"></asp:Button>
</form>
</body>
</HTML>
<!-- END CODE -->
 
J

John Saunders

Matt, does the same problem happen if you don't use STOP, but instead set a
breakpoint in the debugger?
 
M

Matt Theule

does the same problem happen if you don't use STOP, but
instead set a breakpoint in the debugger?

Yes. Using a breakpoint was what initially brought this to my
attention. I added the STOP statement to emphasize that the break in
code had to happen *INSIDE* the event handler. If the breakpoint is set
on the event handler declaration (Public sub Button1_Click (byval...)
and you don't actually step into the method, but stop the project
debugging before the event handler code, the problem is not exhibited.

As posted earlier, it seems like once you step into the event handler,
Visual Studio is committed to completing the event code.

BTW, I am using VS 2002, and in the Processes dialog box (Debug |
Processes) when debugging is stopped, I detach from aspnet_wp.exe and
terminate iexplore.exe (both the default settings). I tried setting the
aspnet_wp.exe process to terminate when debugging is stopped, but it did
not affect the behavior I was examining. Also, this behavior does not
occur in Windows forms and *IS* reproducible in C# ASP.NET projects.
However, it is *much* harder to see the exception thrown in the C#
project. I initially doubted it was happening, but the database access
proved it.

I have included both the db access and the throw exception C# code
below.

MATT

C# Exception Only code below:
<!-- BEGIN CODE -->
<HTML>
<script language="C#" runat="server">

private void Page_Load(object sender, System.EventArgs e)
{}
private void Button1_Click(object sender, System.EventArgs e)
{
throw new System.Exception("CSharp Get past this!");
}

</script>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Button"
OnClick="Button1_Click"></asp:Button>
</form>
</body>
</HTML>
<!-- END CODE -->

C# Database Access code below:
<!-- BEGIN CODE -->
<HTML>
<script language="C#" runat="server">

private void Page_Load(object sender, System.EventArgs e)
{}
private void Button1_Click(object sender, System.EventArgs e)
{

System.Data.SqlClient.SqlCommand oCMD = new
System.Data.SqlClient.SqlCommand("INSERT INTO Categories(CategoryName)
VALUES('TestC#')");
oCMD.Connection = new System.Data.SqlClient.SqlConnection("network
address=localhost; initial catalog=Northwind; integrated
security=sspi;");
oCMD.Connection.Open();
oCMD.CommandType= System.Data.CommandType.Text;
oCMD.ExecuteNonQuery();
oCMD.Connection.Close();
}

</script>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Button"
OnClick="Button1_Click"></asp:Button>
</form>
</body>
</HTML>
<!-- END CODE -->
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top