How to End Protected Sub within Try Catch

Posted on 2009-04-07
Last Modified: 2013-11-07
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.



            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

Question by:DanGettel
  • 2
LVL 13

Accepted Solution

rettiseert earned 250 total points
ID: 24090360
Exit Sub
in the Catch block
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 250 total points
ID: 24090935
use this:

            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

Author Comment

ID: 24099169
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


                rowsAffected = GettelDataSource.Update()

            Catch ex As Exception



                GettelDataSource = Nothing

            End Try

            If rowsAffected <> 1 Then



                UpdateButton.Visible = False

                Dim appointmentDate As Label = _


                Dim appointmentTime As Label = _


                Dim smtpClient As SmtpClient = New SmtpClient()

                Dim message As MailMessage = New MailMessage()

                Dim toAddress As New MailAddress( _


                Dim fromAddress As New MailAddress( _

                "", "My Organization")

                message.From = fromAddress


                Dim CCAddress As TextBox

                CCAddress = EmailAddressTextBox

                If Not String.IsNullOrEmpty(EmailAddressTextBox.Text) Then


                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>" & _


                message.Headers.Add("X-MimeOLE", "Produced")

                ' Set server details

                smtpClient.Host = ""

                ' Send the email

                smtpClient.ServicePoint.MaxIdleTime = 1




        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


Author Comment

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


Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now