Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to End Protected Sub within Try Catch

Posted on 2009-04-07
4
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
rettiseert earned 1000 total points
ID: 24090360
Use
Exit Sub
in the Catch block
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 1000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

618 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