Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7038019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:37:08+00:00 2026-05-28T01:37:08+00:00

I am developing a web application that provides the users with short quizzes. The

  • 0

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.

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.

I am able to let the application sends emails, but now I want the system to update the value of IsSent to 1 or true after sending the email but I don’t know how to do it.

Can anyone tell me how to do it?

My Code:

protected void Page_Load(object sender, EventArgs e)
{
    SendEmailTOAllUser();
}


protected void SendEmail(string toAddress, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml) 
{
    SmtpClient sc = new SmtpClient("SMTP (MAIL) ADDRESS");
    try
    {
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("pssp@gmail.com", "OUR SYSTEM");
        msg.To.Add(toAddress);
        msg.Subject = MailSubject;
        msg.Body = MessageBody;
        msg.IsBodyHtml = isBodyHtml;
        //Response.Write(msg);
        sc.Send(msg);
    }
    catch (Exception ex)
    {
        throw ex;
    }

}

protected void SendEmailTOAllUser()
{
    string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspTest;Integrated Security=True";
    string cmdText = "SELECT QuizID, IsSent FROM dbo.QUIZ";
    string cmdText2 = "SELECT Username FROM dbo.employee";

    Collection<string> emailAddresses = new Collection<string>();
    string link = "";
    string body = "";

    using (SqlConnection conn = new SqlConnection(connString))
    {
        conn.Open();
        // Open DB connection.
        using (SqlCommand cmd = new SqlCommand(cmdText, conn))
        {
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader != null)
            {
                while (reader.Read())
                {
                    if (!(bool)reader["IsSent"])
                    {
                        string quizid = reader["QuizID"].ToString();
                        link = "<a href='http://pmv/pssp/StartQuiz.aspx?testid=" + quizid + "'> Click here to participate </a>";
                        body = @"<b> Please try to participate in the new short safety quiz </b>"
                                            + link +
                                            @"<br /> <br />
                        This email was generated using the <a href='http://pmv/pssp/Default.aspx'>PMOD Safety Services Portal </a>. 
                        Please do not reply to this email.
                        ";    
                    }
                }
            }
            reader.Close();
        }

        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 + "@gmail.com";
                    emailAddresses.Add(receiverEmail);
                }
            }
            reader.Close();
        }
        conn.Close();
    }

    foreach (string email in emailAddresses)
    {
        SendEmail(email, "", "Notification Email Subject", body, true);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:37:09+00:00Added an answer on May 28, 2026 at 1:37 am

    First, your code as it currently stands, will only send an email about the last quiz.

    Second, as a general rule of thumb, you should only retrieve the data that is absolutely necessary from the database. There is no reason to read in quizzes that have already been sent, so you can change that query to:

    string cmdText = "SELECT QuizID FROM dbo.QUIZ WHERE IsSent <> 1";
    

    Third, you should update IsSent for each quiz immediately after the emails for the quiz have been sent.

    Finally, you should send a single email with all recipients as BCC users rather than multiple emails.

    Here is a rewrite containing all of these concepts:

        protected void SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
        {
            SmtpClient sc = new SmtpClient("SMTP (MAIL) ADDRESS");
            try
            {
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress("pssp@gmail.com", "OUR SYSTEM");
    
                // In case the mail system doesn't like no to recipients. This could be removed
                msg.To.Add("pssp@gmail.com");
    
                msg.Bcc.Add(toAddresses);
                msg.Subject = MailSubject;
                msg.Body = MessageBody;
                msg.IsBodyHtml = isBodyHtml;
                //Response.Write(msg);
                sc.Send(msg);
            }
            catch (Exception ex)
            {
                throw ex;
            }
    
        }
    
        protected void SendEmailTOAllUser()
        {
            string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspTest;Integrated Security=True";
    
            using (SqlConnection conn = new SqlConnection(connString))
            {
                var sbEmailAddresses = new System.Text.StringBuilder(1000);
                var quizIds = new List<int>();
    
                // Open DB connection.
                conn.Open();
    
                string cmdText = "SELECT QuizID FROM dbo.QUIZ WHERE IsSent <> 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";
                using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
                {
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            var sName = reader.GetString(0);
                            if (!string.IsNullOrEmpty(sName)
                            {
                                if (sbEmailAddresses.Length != 0)
                                {
                                    sbEmailAddresses.Append(",");
                                }
                                // Just use the ordinal position for the user name since there is only 1 column
                                sbEmailAddresses.Append(sName).Append("@gmail.com");
                            }
                        }
                    }
                    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 = "<a href='http://pmv/pssp/StartQuiz.aspx?testid=" + quizid + "'> Click here to participate </a>";
                        string body = @"<b> Please try to participate in the new short safety quiz </b>"
                                            + link +
                                            @"<br /> <br />
                        This email was generated using the <a href='http://pmv/pssp/Default.aspx'>PMOD Safety Services Portal </a>. 
                        Please do not reply to this email.
                        ";
    
                        SendEmail(sEMailAddresses, "", "Notification Email Subject", body, true);
    
                        // Update the parameter for the current quiz
                        oParameter.Value = quizid;
                        // And execute the command
                        cmd.ExecuteNonQuery();
                    }
                }
                conn.Close();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an intranet web application that provides the users with short quizzes.
I am developing an ASP.NET intranet web application that provides short quizzes to the
I'm developing a Web application that will let users upload images. My concern is
We are developing a web application that will be sold to many clients. There
I am currently developing a web application that receives data from an on-site database.
The web application that I am developing right now has something called quiz engine
I am developing a web application for a comapny. This application provides the users
The web application that I am developing right now has something called quiz engine
I am developing an intranet web application which should provide its users with a
The web application that I am developing right now has something called quiz engine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.