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>
<ASPataList 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") %>
|
<%# DataBinder.Eval(Container.DataItem,
"CategoryName") %></DIV>
</ItemTemplate>
</ASPataList>
<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 -->
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>
<ASPataList 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") %>
|
<%# DataBinder.Eval(Container.DataItem,
"CategoryName") %></DIV>
</ItemTemplate>
</ASPataList>
<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 -->