CodeCharge Studio

Use the After Update Event to Send Emails

You previously added the necessary code that sends email notification to the assignee upon recording a new task in the system. We shall now implement similar functionality in After Update Event to notify the assignee when an existing task is updated and reassigned to someone.

  1. Click on the tasks form in the Project Explorer.
  2. In the Properties window, select the Events tab.
  3. Add the following Custom Code in After Update event:

    C#

    if(ExecuteFlag&&!ErrorFlag&&DBUtility.UserId.ToString() != item.user_id_assign_to.Value.ToString())
    {
      SqlCommand userEmail = new SqlCommand("SELECT email FROM " +
        "employees WHERE emp_id=" + DBUtility.UserId, 
        Settings.IntranetDBDataAccessObject);
      SqlCommand assignedUserEmail = new SqlCommand("SELECT email FROM " +
        "employees WHERE emp_id=" + item.user_id_assign_to.Value.ToString(),
    	Settings.IntranetDBDataAccessObject);
      System.Web.Mail.MailMessage newMessage = new System.Web.Mail.MailMessage();
      newMessage.From = userEmail.ExecuteScalar().ToString();
      newMessage.To = assignedUserEmail.ExecuteScalar().ToString();
      newMessage.Subject = " A task was assigned to you";
      newMessage.BodyFormat = System.Web.Mail.MailFormat.Html;
      newMessage.Body = "The following task was assigned to you:<br><br>" +
        "Task ID :" + Request.QueryString["task_id"].ToString() +
    	"<br><br>"+ item.task_desc.Value;
      System.Web.Mail.SmtpMail. SmtpServer = "localhost";
      System.Web.Mail.SmtpMail.Send(newMessage );
    }
    

    VB.Net

    If ExecuteFlag AndAlso (Not ErrorFlag) AndAlso Convert.ToString (DBUtility.UserId) <> Convert.ToString(item.user_id_assign_to.Value) Then
      
      Dim userEmail, assignedUserEmail As String
      userEmail = Convert.ToString(Settings.IntranetDBDataAccessObject.ExecuteScalar( _
      	"SELECT email FROM employees WHERE emp_id=" & DBUtility.UserId))
      assignedUserEmail = Convert.ToString(Settings.IntranetDBDataAccessObject.ExecuteScalar( _
      	"SELECT email FROM employees WHERE emp_id=" & Convert.ToString(item.user_id_assign_to.Value)))
    	
      Dim newMessage As New System.Web.Mail.MailMessage()
      
      newMessage.From = userEmail
      newMessage.To = assignedUserEmail
      newMessage.Subject = "A task was assigned to you"
      newMessage.BodyFormat = System.Web.Mail.MailFormat.Html
      newMessage.Body = "The following task was assigned to you :<br><br> Task ID :" & _
        Convert.ToString(Request.QueryString("task_id")) & "<br><br>" & item.task_desc.Value
    	
      System.Web.Mail.SmtpMail. SmtpServer = "localhost"
      System.Web.Mail.SmtpMail.Send(newMessage)
    
    End if
    

    The main differences between the above code and the one you used in After Insert event are as follows:

    1. An "if" condition was added to send an email only if a user assigns a task to someone other than himself/herself
    2. task_id is retrieved from the URL using the Request.QueryString indexer. You can use this method because tasks can be updated only if the user arrived at the current page via a URL that contains the task id to be updated. A such a URL would look like this:http://localhost/TaskManager/tasks_maint.aspx?task_id=9

Next: Test Email Delivery


On-line, printable versions and updates