Solved

How to End Protected Sub within Try Catch

Posted on 2009-04-07
4
203 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
ID: 24090360
Use
Exit Sub
in the Catch block
0
 
LVL 70

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."
                return
            End Try
0
 

Author Comment

by:DanGettel
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
 
            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
ID: 24107847
both of your suggestions should be correct.  There must be something weird going on somewhere in the sub.

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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