[SOLVED] How do I update rows in my database as I loop through reading them?

Issue

I have the following code which loops through a database table and sends emails to the address stored in each row. How can I modify it to update an additional column at the same time as it reads them? I want to update each row with a DateSent value of the current date and time. The table contains five columns – ID, FirstName, LastName, Email, DateSent – and it’s the last column I want to update with the date and time the specific email was sent.

I’m still new to this so apologies if this is basic beginners stuff.

Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Net.Mail;

public partial class displayRecords : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      string strConn = "my connection string;";
      string strSQL = "select * from EmailTable";
      SqlConnection objConnection = new SqlConnection(strConn);
      SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
      objConnection.Open();
      SqlDataReader objReader = objCommand.ExecuteReader();
      while (objReader.Read())
      {

        MailMessage myMessage = new MailMessage();
        myMessage.Subject = "Test Message for " + (objReader.GetValue(1)) + " " + (objReader.GetValue(2));
        myMessage.Body = "This email would be sent to: " + (objReader.GetValue(3));
        myMessage.From = new MailAddress("[email protected]", "Sender Name");
        myMessage.To.Add(new MailAddress((objReader.GetString(3)), (objReader.GetString(2))));

        SmtpClient mySmtpClient = new SmtpClient();
        mySmtpClient.Send(myMessage);

        Response.Write("Email sent to: " + (objReader.GetValue(3)) + "<br>");
      }
      objReader.Close();
      objConnection.Close();
    }

}

Solution

You can use an UPDATE statement to update the row based on the id.

  while (objReader.Read())
  {

    MailMessage myMessage = new MailMessage();
    myMessage.Subject = "Test Message for " + (objReader.GetValue(1)) + " " + (objReader.GetValue(2));
    myMessage.Body = "This email would be sent to: " + (objReader.GetValue(3));
    myMessage.From = new MailAddress("[email protected]", "Sender Name");
    myMessage.To.Add(new MailAddress((objReader.GetString(3)), (objReader.GetString(2))));

    SmtpClient mySmtpClient = new SmtpClient();
    mySmtpClient.Send(myMessage);

    Response.Write("Email sent to: " + (objReader.GetValue(3)) + "<br>");

    // Update the table, assuming ID is the first column in the table.
    // This is for demonstration only and it is not the most efficient way
    // of doing this because a new command is created each time. 
    // The correct way would be to move the command and parameters creation
    // outside the loop and just update the parameter values inside the loop.
    SqlCommand UpdateCommand = new SqlCommand("UPDATE EmailTable SET DateSent = @dtSent WHERE id = @thisId", objConnection);
    updateCommand.Parameters.AddWithValue( "@dtSent", DateTime.Now);
    updateCommand.Parameters.AddWithValue( "@thisId", objReader.GetValue(0));
    updateCommand.ExecuteNonQuery();
  }

Answered By – Alex W

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *