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

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.