VB.NET End Sub语句导致代码向后跳转

时间:2016-10-15 15:28:13

标签: asp.net vb.net

我有一个asp.net Web应用程序,它将Web表单中的信息插入到MS SQL数据库中,并返回为该行创建的条目ID。我在后面的vb.net代码中完成了这个。当代码到达" End Sub"命令,代码执行将跳回到更早的行并再次运行行。这会导致两行输入数据库。到达" End Sub"第二次,sub确实结束了。

    Protected Sub SubmitForm() Handles SubmitButton.ServerClick
    'Declare Variables
    'i have deleted this part from this post as i am over the char limit

    'Set values on variables
    'i have deleted this part from this post as i am over the char limit


    'Set connection string
    Dim setCmd As New SqlCommand
    sConnectionString = "Password=passwordhere;User ID=userhere;" & _
                        "Initial Catalog=databasehere;" & _
                        "Data Source=serverhere"
    Dim objConn As New SqlConnection(sConnectionString)

    'Connect to database
    Try
        objConn.Open()
    Catch Ex As Exception
        Select Case Ex.ToString
            Case "InvalidOperationException"
                Exit Try
            Case "SqlException"
                MsgBox("The database is currently down. Please try again in a few minute. " & vbNewLine & "If the issue persists please alert your supervisor.")
                Exit Sub
        End Select
    End Try

    'Set the stored proc
    sStoredProcName = "storedprocnamehere"
    setCmd = New SqlCommand(sStoredProcName, objConn)
    setCmd.CommandType = CommandType.StoredProcedure

    'Set the parameters that cannot be null
    'I do set all of the needed parameters, however i have deleted them from this post as i am over the char limit

    lReportNum = setCmd.ExecuteScalar()

End Sub

此时它会跳转到lReportNum = setCmd.ExecuteScalar()之前的那一行并再次穿过该行。

即使我部署代码并访问内部网上的页面,也会发生这种情况。

我正在使用Visual Studios 2010和.net 3.5

请帮忙!我一直在努力解决这个问题,并在网上搜索了两天的答案。

肖恩

1 个答案:

答案 0 :(得分:0)

谢谢David Tansey和Shadow。你的评论引导我得到答案。它本身并不是一个回发问题,但结合David的请求,看到调用sub的代码让我看到我实际上正在调用该过程两次。一旦进入我的按钮的onserverclick事件,再次包括' Handles SubmitButton.ServerClick'在VB代码隐藏中。

我在按钮上删除了onserverclick事件声明,现在一切正常。

虽然它似乎只是向后跳了几行,但它真的再次运行整个潜艇并再次停在我的断点处。

再次非常感谢你的帮助

肖恩