title: canceling email send with asp.net custom validator
body: <p>I have a form that sends an email that includes 4 fields.  One of the fields is validated by a custom validator against the database and if the value is found the email should send.  If it is not found the email should cancel and there should be an error message.  Everything is working except the email sends regardless of the validation.  How can I keep the email from sending?  </p>
  
  <pre><code>Imports System.Net.Mail
  Imports System.Data.OleDb
  Imports System.Data.SqlClient
  
  Partial Class inforequest
  Inherits System.Web.UI.Page
  
  Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
  
  
  
      Dim mm As New MailMessage("sender@email.com", "receiver@email.com")
      mm.Subject = txtSubject.Text
      mm.Body = "Name: " &amp; txtName.Text &amp; "&lt;br /&gt;&lt;br /&gt;Email: " &amp; txtEmail.Text &amp; "&lt;br /&gt;" &amp;                       txtBody.Text &amp; "&lt;br /&gt; Agent Code:" &amp; AgentCode.Text
  
  
      mm.IsBodyHtml = True
      Dim smtp As New SmtpClient()
      smtp.Host = "mailserver"
      smtp.EnableSsl = False
  
      Dim NetworkCred As New System.Net.NetworkCredential()
      smtp.UseDefaultCredentials = False
  
  
      NetworkCred.UserName = "username"
      NetworkCred.Password = "password"
      smtp.EnableSsl = False
  
      smtp.Credentials = NetworkCred
      smtp.Port = 587
      smtp.Send(mm)
      lblMessage.Text = "Email Sent SucessFully."
  End Sub
  
  
  
  
  
  
  Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  
  
      Dim dbconn As String
      Dim qstring = Request.QueryString("ID")
      Dim addressDR As System.Data.SqlClient.SqlDataReader
  
      Dim sqlcommand As String = "SELECT * FROM listings WHERE ID=@qstring"
      dbconn = ConfigurationManager.ConnectionStrings("houses").ToString
  
      Dim connection As New System.Data.SqlClient.SqlConnection(dbconn)
      connection.Open()
      Dim addresscmd As New System.Data.SqlClient.SqlCommand(sqlcommand, connection)
      addresscmd.Parameters.AddWithValue("@qstring", qstring)
  
      addressDR = addresscmd.ExecuteReader()
  
  
  
  
  
      If addressDR.HasRows Then
          addressDR.Read()
          Me.txtBody.Text = "I would like to request a showing of the home located at: " &amp; addressDR("address") &amp; " MLS#: " &amp; addressDR("mlsnum")
          addressDR.Close()
  
  
      End If
  
      connection.Close()
  
  
  
  
  
  End Sub
  
  
  
  
  Protected Sub CodeValidate_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CodeValidate.ServerValidate
  
      Dim AgentCode = Request.Form("AgentCode")
      Dim sql As String = "SELECT agentcode FROM Codes WHERE agentcode = @AgentCode"
  
      Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Codes").ConnectionString)
          Using cmd As New SqlCommand(sql, conn)
              cmd.Parameters.AddWithValue("@AgentCode", AgentCode)
  
              conn.Open()
              Using rdr As SqlDataReader = cmd.ExecuteReader()
                  If (rdr.Read()) Then
                      'MsgBox("reader reading")
                      'If AgentCode = rdr("agentcode").ToString() Then
                      args.IsValid = True
  
                      'MsgBox("valid!")
                  Else
                      args.IsValid = False
  
                      'MsgBox("not valid")
                  End If
  
              End Using
              conn.Close()
  
          End Using
  
      End Using
  End Sub
  
  
  
  
  End Class
  </code></pre>