Solved

How to End Protected Sub within Try Catch

Posted on 2009-04-07
4
200 Views
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.
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

0
Comment
Question by:DanGettel
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
rettiseert earned 250 total points
Comment Utility
Use
Exit Sub
in the Catch block
0
 
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 250 total points
Comment Utility
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."
                return
            End Try
0
 

Author Comment

by:DanGettel
Comment Utility
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

0
 

Author Comment

by:DanGettel
Comment Utility
both of your suggestions should be correct.  There must be something weird going on somewhere in the sub.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hangfire / asp.net sample 3 27
Problem to Popup 2 48
How to open/READ file from UNC path Shared Folder... 2 37
Create XML 5 30
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

7 Experts available now in Live!

Get 1:1 Help Now