Link to home
Start Free TrialLog in
Avatar of DanGettel
DanGettel

asked on

How to End Protected Sub within Try Catch

I have two Try....Catches inside of a Protected Sub.

Inside the second Try....Catch I would like to End the Protected Sub if an error occurs.

How would I be able to do this?

Thanks in advance for the help.
Try
                smtpClient.Send(message)
 
                Server.Transfer("confirm.aspx")
            Catch ex As Exception
                ' Display error message
                EmailProblemsLabel.Visible = True
                EmailProblemsLabel.Text = "You have successfully made change, but there was a problem sending the email."
            End Try

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of rettiseert
rettiseert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanGettel
DanGettel

ASKER

Below you will see the entire Protected Sub.

I have tried using End Sub and Return and nothing seems to help.

What is happening is that when it hits the second Catch "Catch ex As System.Net.Mail.SmtpException" it loops back through the Protected Sub doing a second update. Then after the database is updated for a second time it does what I am asking it to do in the second catch.

Thanks for any words of wisdom.
    Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
 
 
            Dim GettelDataSource As New SqlDataSource()
            GettelDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("MyConnectionString").ToString()
 
 
            GettelDataSource.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure
            GettelDataSource.UpdateCommand = "UpdateInfo"
 
            GettelDataSource.UpdateParameters.Add("FirstName", FirstNameTextBox.Text)
            GettelDataSource.UpdateParameters.Add("LastName", LastNameTextBox.Text)
            GettelDataSource.UpdateParameters.Add("Email", EmailAddressTextBox.Text)
            GettelDataSource.UpdateParameters.Add("PhoneNumber", PhoneNumberTextBox.Text)
            GettelDataSource.UpdateParameters.Add("Comments", CommentsTextBox.Text)
            GettelDataSource.UpdateParameters.Add("UserName", UserName)
            GettelDataSource.UpdateParameters.Add("UserId", UserID)
 
            Dim rowsAffected As Integer = 0
 
            Try
                rowsAffected = GettelDataSource.Update()
 
            Catch ex As Exception
 
                Server.Transfer("update_problems.aspx")
 
            Finally
                GettelDataSource = Nothing
 
            End Try
 
            If rowsAffected <> 1 Then
                Server.Transfer("update_problems.aspx")
            Else
 
                UpdateButton.Visible = False
 
                Dim appointmentDate As Label = _
                FormView1.FindControl("DateTextShortLabel")
                Dim appointmentTime As Label = _
                FormView1.FindControl("TimeLabel")
                Dim smtpClient As SmtpClient = New SmtpClient()
                Dim message As MailMessage = New MailMessage()
 
                Dim toAddress As New MailAddress( _
                "Confirmations@mydomain.com")
                Dim fromAddress As New MailAddress( _
                "info@mydomain.com", "My Organization")
 
                message.From = fromAddress
                message.To.Add(toAddress)
 
                Dim CCAddress As TextBox
                CCAddress = EmailAddressTextBox
                If Not String.IsNullOrEmpty(EmailAddressTextBox.Text) Then
                    message.CC.Add(CCAddress.Text)
                End If
 
                message.Subject = "Confirmation Update"
                message.IsBodyHtml = True
                message.Body = _
                "<html><head><title>Confirmation of Update</title></head><body>" & _
                "<p>Hello " & _
                HttpUtility.HtmlEncode(FirstNameTextBox.Text) & " " & _
                HttpUtility.HtmlEncode(LastNameTextBox.Text) & ",</p><br /><p>This email was sent to confirm your update <strong>on " & _
                HttpUtility.HtmlEncode(appointmentDate.Text) & " at " & _
                HttpUtility.HtmlEncode(appointmentTime.Text) & ".</p></strong><br /><p>Regards,</p><br /><p>Name</p>" & _
                "</body></html>"
 
                message.Headers.Add("X-MimeOLE", "Produced mydomain.com")
                ' Set server details
                smtpClient.Host = "mail.mydomain.com"
                ' Send the email
                smtpClient.ServicePoint.MaxIdleTime = 1
        Try
                smtpClient.Send(message)
 
                Server.Transfer("confirm.aspx")
 
 
 
        Catch ex As System.Net.Mail.SmtpException
            ' Display error message
            EmailProblemsLabel.Visible = True
            EmailProblemsLabel.Text = "You have successfully made change, but there was a problem sending the email."" & ex.Message
            Exit Sub
        End Try
 
        End If
 
    End Sub

Open in new window

both of your suggestions should be correct.  There must be something weird going on somewhere in the sub.