IDataReader – Validating e-mail addresses
Use BeginValidateEmails to asynchronously validate e-mail addresses in an IDataReader interface. This method validate e-mail address asynchronously with execution occurring on a new thread, therefore it allows your next line of code to execute immediately. The event ValidateEmailsCompleted is raised when the BeginValidateEmails is complete. In the handler method of the ValidateEmailsCompleted, you need to call the EndValidateEmails to finish the asynchronous operation.
To validate e-mail addresses in an IDataReader interface asynchronously with ComponentSoft EmailValidator, you can simply perform the following steps
- Add the Email Validator component to your application. See Creating a WinForms Application or Creating a Web Application for more details.
- Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
C#
Copy Codeusing ComponentSoft.Net;VB.NET
Copy CodeImports ComponentSoft.Net - Create a new instance of the EmailValidator class. The code looks similar to the following:
C#
Copy Code// Create a new instance of the EmailValidator class.
EmailValidator validator = new EmailValidator();VB.NET
Copy Code‘ Create a new instance of the EmailValidator class.
Dim validator As New EmailValidator() - Now you can call the BeginValidateEmails to asynchronously validate e-mail addresses. Prior to calling BeginValidateEmails method, you have to register an event handler to the ValidateEmailsCompleted event (you do not need to do that before each call to the BeginValidateEmails method, just before the first call). Upon completion of the upload operation, the ValidateEmailsCompleted event will be raised. When the event is raised, access information contained in the AsyncMethodCompletedEventArgs object. The code looks similar to the following:
C#
Copy Code// Register an event handler.
validator.MessageLogging += validator_MessageLogging;
validator.EmailValidated += validator_EmailValidationCompleted;
validator.ValidateEmailsCompleted += validator_ValidateEmailsCompleted;
_reader = GetReader(“Test.mdb”);
// Validate e-mail addresses.
validator.BeginValidateEmails(_reader);VB.NET
Copy Code‘ Register an event handler.
AddHandler validator.MessageLogging, AddressOf validator_MessageLogging
AddHandler validator.EmailValidated, AddressOf validator_EmailValidationCompleted
AddHandler validator.ValidateEmailsCompleted, AddressOf validator_ValidateEmailsCompleted
_reader = GetReader(“Test.mdb”)
‘ Validate e-mail addresses.
validator.BeginValidateEmails(_reader) - Now you need to write the code for client_ValidateEmailsCompleted event handler. And in the client_ValidateEmailsCompleted event handler, write your own code to do something like displaying a message indicating that the operation has been completed, etc. The code looks similar to the following:
C#
Copy Codevoid validator_ValidateEmailsCompleted(object sender, AsyncMethodCompletedEventArgs e)
{
EmailValidator validator = (EmailValidator)sender;
try
{
validator.EndValidateEmails(e.AsyncResult);
// Show result.
MessageBox.Show(“Completed!”);
}
catch (Exception exc)
{
Console.WriteLine(“Error: “ + exc.ToString());
}
}VB.NET
Copy CodePrivate Sub validator_ValidateEmailsCompleted(ByVal sender As Object, ByVal e As AsyncMethodCompletedEventArgs)
Dim validator As EmailValidator = CType(sender, EmailValidator)
Try
validator.EndValidateEmails(e.AsyncResult)
‘ Show result.
MessageBox.Show(“Completed!”)
Catch exc As Exception
Console.WriteLine(“Error: “ & exc.ToString())
End Try
End Sub
Final Example Code
| C# | Copy Code |
|---|---|
|
private static IDataReader GetReader(string path)
{ OleDbConnection dbCon = new OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + path); dbCon.Open(); // Use IDataReader OleDbCommand cmd = new OleDbCommand(); cmd.CommandType = CommandType.Text; cmd.Connection = dbCon; cmd.CommandText = “SELECT * FROM tblContacts”; IDataReader reader = cmd.ExecuteReader(); return reader; } private IDataReader _reader; public void DoValidateEmailsAsync() { // Create a new instance of the EmailValidator class. EmailValidator validator = new EmailValidator(); // Register an event handler. validator.MessageLogging += validator_MessageLogging; validator.EmailValidated += validator_EmailValidationCompleted; validator.ValidateEmailsCompleted += validator_ValidateEmailsCompleted; _reader = GetReader(“Test.mdb”); // Validate e-mail addresses. validator.BeginValidateEmails(_reader); // … } void validator_EmailValidationCompleted(object sender, EmailValidatedEventArgs e) { if (e.ValidatedLevel == ValidationLevel.Success) Console.WriteLine(e.EmailAddress + ” validation done”); else Console.WriteLine(e.EmailAddress + ” validation failed at “ + e.ValidatedLevel); } void validator_MessageLogging(object sender, EmailValidatorLogEventArgs e) { Console.Write(e.SmtpTranscript); } void validator_ValidateEmailsCompleted(object sender, AsyncMethodCompletedEventArgs e) { EmailValidator validator = (EmailValidator)sender; try { validator.EndValidateEmails(e.AsyncResult); // Show result. MessageBox.Show(“Completed!”); } catch (Exception exc) { Console.WriteLine(“Error: “ + exc.ToString()); } finally { if (_reader != null) _reader.Close(); } } |
|
| VB.NET | Copy Code |
|---|---|
|
Private Shared Function GetReader(ByVal path As String) As IDataReader
Dim dbCon As New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” & path) dbCon.Open() ‘ Use IDataReader Dim cmd As New OleDbCommand() cmd.CommandType = CommandType.Text cmd.Connection = dbCon cmd.CommandText = “SELECT * FROM tblContacts” Dim reader As IDataReader = cmd.ExecuteReader() Return reader End Function Private _reader As IDataReader Public Sub DoValidateEmailsAsync() ‘ Create a new instance of the EmailValidator class. Dim validator As New EmailValidator() ‘ Register an event handler. AddHandler validator.MessageLogging, AddressOf validator_MessageLogging AddHandler validator.EmailValidated, AddressOf validator_EmailValidationCompleted AddHandler validator.ValidateEmailsCompleted, AddressOf validator_ValidateEmailsCompleted _reader = GetReader(“Test.mdb”) ‘ Validate e-mail addresses. validator.BeginValidateEmails(_reader) ‘ … End Sub Private Sub validator_EmailValidationCompleted(ByVal sender As Object, ByVal e As EmailValidatedEventArgs) If e.ValidatedLevel = ValidationLevel.Success Then Console.WriteLine(e.EmailAddress & ” validation done”) Else Console.WriteLine(e.EmailAddress & ” validation failed at “ & e.ValidatedLevel) End If End Sub Private Sub validator_MessageLogging(ByVal sender As Object, ByVal e As EmailValidatorLogEventArgs) Console.Write(e.SmtpTranscript) End Sub Private Sub validator_ValidateEmailsCompleted(ByVal sender As Object, ByVal e As AsyncMethodCompletedEventArgs) Dim validator As EmailValidator = CType(sender, EmailValidator) Try validator.EndValidateEmails(e.AsyncResult) ‘ Show result. MessageBox.Show(“Completed!”) Catch exc As Exception Console.WriteLine(“Error: “ & exc.ToString()) Finally If _reader IsNot Nothing Then _reader.Close() End If End Try End Sub |
|
Categories: Uncategorized