|
-
Jul 18th, 2013, 07:39 PM
#1
Thread Starter
Frenzied Member
Try Catch Exit Sub
Hi guys I have been googling around and a bit confused. I have a try statement in my code would like if an exception is caught to exit the sub and start over. My code is reading data into a DataTable , when an error is caught and I hit the button to start the sub again the data is still in the table.
Example:
I have a table with 1 address
Hit button to send email, address is retrieved , if there is a problem sending the email it is caught in the catch block
if I hit the button again to send email now I have 2 address's
Bottom line is I am confused as to how to properly code this to handle errors correctly, exit the sub and resume
In my googling efforts I came across some posts about NOT exiting a sub but using return instead
can someone give me some guidance thanks
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
GetEmailAdd() 'function to readtable from txt file
Dim attach As Attachment = Nothing
If CheckBox1.Checked Then
Dim filename As String = OpenFileDialog2.FileName
attach = New Attachment(filename)
End If
Dim i As Integer = 1
Dim pauseTime As Integer = CDbl(txttimesecs.Text) * 1000
Dim result As DialogResult = MessageBox.Show("There are" & " " & emailadd.Count & " " & "Messages to be Sent", "Send Email", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
If result = DialogResult.Cancel Then
Return
End If
For Each email In emailadd
If emailadd.Count <= 0 Then
MsgBox("No Email Address Found")
Exit Sub
End If
Try
i = +1
Dim sslValue As String = ComboBox1.Text
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New Net.NetworkCredential(txtusername.Text, txtpassword.Text)
SmtpServer.Port = CDbl(txtserverport.Text)
SmtpServer.Host = txtservername.Text
SmtpServer.EnableSsl = sslValue
mail = New MailMessage()
mail.From = New MailAddress(txtfrom.Text)
mail.To.Add(email)
mail.Subject = txtsubj.Text
If CheckBox1.Checked Then
mail.Attachments.Add(attach)
End If
mail.Body = txtmsgbox.Text
If btnhtml.Checked = True Then
mail.IsBodyHtml = True
Else
mail.IsBodyHtml = False
End If
SmtpServer.Send(mail)
Catch ex As Exception
MsgBox(ex.ToString)
MsgBox("No Messages Sent")
Exit Try
Return
End Try
Next
wait(pauseTime)
MsgBox(i.ToString & " " & "Messages Sent")
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|