How to send only one quiz from the database to all users?
<p>I am developing a web application that provides the users with short quizzes. The system will check the Quiz table in the database which consists of a QuizID and IsSent column.</p>

<p>If the IsSent column (which is a bit data type) has a value of 0 (which is false), the system will send the quiz to all users. If it has a 1, this means the quiz has already been sent.</p>

<p>I am able to let the application sends emails, but if there is more than quiz without sending it to the users, the system will send all of them and this should not be happened. <b><i></i>What should happen is, checking the database if there is more than one quiz there, send the old created one not all of the quizzes starting by the last created one</b>. <b>SO HOW TO DO THAT?</b></p>

<p>My code-behind is:</p>

<pre><code>protected void Page_Load(object sender, EventArgs e)
    {
        SendEmailTOAllUser();
    }


    protected void SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
    {
        SmtpClient sc = new SmtpClient("SMTP (MAIL) ADDREASS");
        try
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("psTest@gmail.com", "Our System");

            // In case the mail system doesn't like no to recipients. This could be removed
            msg.To.Add("psTest@gmail.com");

            msg.Bcc.Add(toAddresses);
            msg.Subject = MailSubject;
            msg.Body = MessageBody;
            msg.IsBodyHtml = isBodyHtml;

            sc.Send(msg);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }


    protected void SendEmailTOAllUser()
    {
        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psTest;Integrated Security=True";

        using (SqlConnection conn = new SqlConnection(connString))
        {
            var sbEmailAddresses = new System.Text.StringBuilder(1000);
            var quizIds = new List();

            // Open DB connection.
            conn.Open();

            string cmdText = "SELECT QuizID FROM dbo.QUIZ WHERE IsSent &amp;lt;&amp;gt; 1";
            using (SqlCommand cmd = new SqlCommand(cmdText, conn))
            {
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        // There is only 1 column, so just retrieve it using the ordinal position
                        quizIds.Add(reader.GetInt32(0));
                    }
                }
                reader.Close();
            }

            string cmdText2 = "SELECT Username FROM dbo.employee";
            Collection emailAddresses = new Collection();
            using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
            {
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        string emailTo = reader["Username"].ToString();
                        string receiverEmail = emailTo + "@MailServer.com";
                        emailAddresses.Add(receiverEmail);
                    }
                }
                reader.Close();
            }

            string cmdText3 = "UPDATE dbo.Quiz SET IsSent = 1 WHERE QuizId = @QuizID";
            using (SqlCommand cmd = new SqlCommand(cmdText3, conn))
            {
                // Add the parameter to the command
                var oParameter = cmd.Parameters.Add("@QuizID", SqlDbType.Int);
                // Get a local copy of the email addresses
                var sEMailAddresses = sbEmailAddresses.ToString();

                foreach (int quizid in quizIds)
                {
                    string link = " Click here to participate ";
                    string body = @" Please try to participate in the new short safety quiz "
                                        + link +
                                        @" 
                    This email was generated using the Safety Portal . 
                    Please do not reply to this email.
                    ";

                    foreach (string email in emailAddresses)
                    {
                        SendEmail(email, "", "Notification Email Subject", body, true);
                    }
                    // Update the parameter for the current quiz
                    oParameter.Value = quizid;
                    // And execute the command
                    cmd.ExecuteNonQuery();
                }
            }
            conn.Close();
        }

    }
</code></pre>
