I'm trying to select a list of users from the database and send email to each of every users based on a condition isSent == false. After send email to them, the value of this false should be update to true. Below code is the way i retrieve list of users from database and call method sendEmail() to each of them.
myConnection.Open();
//*******
try
{
SqlDataReader myReader = null;
string sql = "SELECT * FROM testTable where isSent = false";
SqlCommand myCommand = new SqlCommand(sql, myConnection);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
sendEmail(myReader["emailAdd"].ToString(),
myReader["UserID"].ToString());
}
Second part:
public static void sendEmail(string emailAdd,string userID){
.
.
.
try
{
smtpClient.Send(mail);
try
{
string sql = "UPDATE testTable SET isSent = 1 WHERE UserID = " + userID;
SqlCommand myCommand = new SqlCommand(sql, myConnection);
int rows = myCommand.ExecuteNonQuery();
.
.
.
}
}
}
The problem I'm facing is that since from main method I already have SqlDataReader being hold to read and so I cant update now. Any work around for me? The error message that I get is as below:
There is already an open DataReader associated with this Command which must be closed first.