Validate Delete Record

Hello,

I'm trying to create this validation when someone clicks on the BindingNavigatorDeleteItem to give him a pope message of whether they are sure if they wanted to delete the record. If they click yes, then the delete process continues, if they press no I want to delete process to stop. but also in the event that the record gets deleted I want the applicaiton to email me with the information that was deleted.

How do I do that... my code is below... would anyone be able to assist.
Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        If MessageBox.Show("Are you sure you want to delete Note for Project" + vbCrLf + "Project Name: " & Me.ProjectNameTextBox.Text & vbCrLf & "Note added by: " & Me.NoteAddedByTextBox.Text, "Attention", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            'If no then stop the process
            Exit Sub
        Else
        	'If yes then delete the record, but send me and email message of the Project that was deleted
        	Dim lSlash As Integer = rName.LastIndexOf("\")
            Dim fName As String = rName.Substring(lSlash + 1)
            Dim emailfrm As String = fName & "@somedomainName.com"
            Dim myMailMessage As New MailMessage(emailfrm, "My email address")
            myMailMessage.Priority = MailPriority.High
            myMailMessage.Subject = "Deleted Note"
            
            Dim bodyText As String = Me.ProjectName_TextBox.text
            Dim smtpServer As New smtpClient("Server Name")
            smtpServer.Send(myMailMessage)
        End If
    End Sub

Open in new window

AIdoHSGAsked:
Who is Participating?
 
-Thespian-Connect With a Mentor Commented:
Set the breakpoint for line 4 and watch in debugger what is the value of dlgResult there
0
 
-Thespian-Commented:
Try "Return" where "Exit Sub" is and replace "Else" with "endif" and delete last "endif"
0
 
AIdoHSGAuthor Commented:
Hello Thespian,

Thanks for the reply,
I've made the changes your suggested and still when I clicked "No" the message was deleted. I wanted to terminate the delete process.
Any thoughts.
My code is below
Private Sub BindingNavigaDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        Dim dlgResult As DialogResult
        dlgResult = MessageBox.Show("Are you sure you want to delete Note for Project" + vbCrLf + "Project Name: " & Me.ProjectNameTextBox.Text & vbCrLf & "Note added by: " & Me.NoteAddedByTextBox.Text, "Attention", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dlgResult = DialogResult.No Then
            Return
        End If
 
        'If yes then delete the record, but send me and email message of the Project that was deleted
        Dim rName As String = My.User.Name
        Dim lSlash As Integer = rName.LastIndexOf("\")
        Dim fName As String = rName.Substring(lSlash + 1)
        Dim emailfrm As String = fName & "@healthstrategies.com"
        Dim myMailMessage As New MailMessage(emailfrm, "aido@healthstrategies.com")
        myMailMessage.Priority = MailPriority.High
        myMailMessage.Subject = "Deleted Note"
 
        Dim bodyText As String = Me.ProjectNameTextBox.Text
        Dim smtpServer As New smtpClient("hsgnjex1.healthstrats.com")
        smtpServer.Send(myMailMessage)
 
    End Sub

Open in new window

0
 
AIdoHSGAuthor Commented:
Hello Thespian,

I changed the property of the button to none in the binding navigator property.
and changed my code to as follows, which seems to have fixed the issue... thank you for your help with this:

Private Sub TblProjectNotesBindingNavigator_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles TblProjectNotesBindingNavigator.ItemClicked
        If e.ClickedItem Is Me.BindingNavigatorDeleteItem Then
            If MessageBox.Show("Are you sure you want to delete the current record?", "Confirm Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then
                Dim rName As String = My.User.Name
                Dim lSlash As Integer = rName.LastIndexOf("\")
                Dim fName As String = rName.Substring(lSlash + 1)
                Dim emailfrm As String = fName & "@domain.com"
                Dim myMailMessage As New MailMessage(emailfrm, "aido@domain.com")
                myMailMessage.Priority = MailPriority.High
                myMailMessage.Subject = "Deleted Note"
                Dim bodyText As String
                Dim strProject As String = Me.ProjectNameTextBox.Text
                Dim strAddedBy As String = Me.NoteAddedByTextBox.Text
                Dim strError As String = Me.ErrorTextBox.Text
                Dim strReason As String = Me.ReasonTextBox.Text

                bodyText = "Project Name: " & strProject & vbCrLf & "Note Added By: " & strAddedBy & vbCrLf & "Note : " & strError & vbCrLf & "Details : " & strReason

                Dim HTMLContent As AlternateView = AlternateView.CreateAlternateViewFromString(bodyText)
                myMailMessage.AlternateViews.Add(HTMLContent)
                Dim smtpServer As New SmtpClient("servername.domain.com")
                smtpServer.Send(myMailMessage)

                Me.TblProjectNotesBindingNavigator.BindingSource.RemoveCurrent()

            End If
        End If
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.