Solved

How to End Protected Sub within Try Catch

Posted on 2009-04-07
4
202 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

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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 (http://www.ecb.europa.eu/stats/exch…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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