0

The Catch section is always executed even if the Try section has been executed. I am trying to send the user to one page if the delete worked properly and a different page if the delete did not work because of relationships between tables:

Protected Sub btnDeleteBuilding_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDeleteBuilding.Click
        Session("sesBuilding")=txtBuilding.Text
        Dim objConnection As New SqlConnection(WebConfigurationManager.ConnectionStrings("TonerRequest").ConnectionString)
        Try
            Dim strSQL As String
            strSQL = "DELETE FROM Building WHERE BuildingID = " & Session("sesBuildingID")
            objConnection.Open()
            Dim objCommand As New SqlCommand(strSQL, objConnection)
            objCommand.ExecuteNonQuery()
            Response.Redirect("Building_Deleted.aspx")
        Exit Try
        Catch
            Response.Redirect("Building_Not_Deleted.aspx")
        Finally
            objConnection.Close()
        End Try
    End Sub
1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by danjoe15
0

I have solved my issue. This is what the code should look like:

Protected Sub btnDeleteBuilding_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDeleteBuilding.Click
        Session("sesBuilding")=txtBuilding.Text
        Dim objConnection As New SqlConnection(WebConfigurationManager.ConnectionStrings("TonerRequest").ConnectionString)
        Dim strSQL As String
        strSQL = "DELETE FROM Building WHERE BuildingID = " & Session("sesBuildingID")
        objConnection.Open()
        Dim objCommand As New SqlCommand(strSQL, objConnection)
        Dim URL As String
        Try
            objCommand.ExecuteNonQuery()
            URL="Building_Deleted.aspx"
        Exit Try
        Catch ex As Exception
            URL="Building_Not_Deleted.aspx"
        Finally
            objConnection.Close()
        End Try
        Response.Redirect(URL)
    End Sub
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.