I am developing an intranet web application which is a Quiz Engine. The admin has the ability to send the quizzes to all users in the database by clicking on a link to the page called SendingQuiz. Everything works well, but my problem is the email will be sent to all usres even if there is no quiz in the database. So how I should modify this code to avoid happening of this?
For your information, in the database design, I have a table called Quiz which consists of:
QuizID, Title, Description, IsSent
What I want now is instead of sending emails without any available quizzes in the database which will let the user facing dump error page, I want to the system to check if there is a quiz in the system or not. If not, it should not send anything and tell the Admin that there is no more quizzes in the database. So how to do that?
My C# code:
protected void SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
{
SmtpClient sc = new SmtpClient("Mail Server");
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("Test@CompanyDomain.com", "My Application");
// In case the mail system doesn't like no to recipients. This could be removed
//msg.To.Add("Test@CompanyDomain.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=Test;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connString))
{
var sbEmailAddresses = new System.Text.StringBuilder(2000);
string quizid = "";
// Open DB connection.
conn.Open();
string cmdText = "SELECT MIN (QuizID) As mQuizID 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
quizid = reader["mQuizID"].ToString();
}
}
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("@CompanyDomain.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);
var sEMailAddresses = sbEmailAddresses.ToString();
string link = "<a href='http://Test/StartQuiz.aspx?testid=" + quizid + "'> Click here to participate </a>";
string body = @"Good day, <br /><br />
<b> Please participate in the new short safety quiz </b>"
+ link +
@"<br /><br />
";
int sendCount = 0;
List<string> addressList = new List<string>(sEMailAddresses.Split(','));
StringBuilder addressesToSend = new StringBuilder();
for (int userIndex = 0; userIndex < addressList.Count; userIndex++)
{
sendCount++;
if (addressesToSend.Length > 0)
addressesToSend.Append(",");
addressesToSend.Append(addressList[userIndex]);
if (sendCount == 10 || userIndex == addressList.Count - 1)
{
SendEmail(addressesToSend.ToString(), "", "Notification of New Weekly Safety Quiz", body, true);
addressesToSend.Clear();
sendCount = 0;
}
}
// Update the parameter for the current quiz
oParameter.Value = quizid;
// And execute the command
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
You dont need to use While for the reader as you are expecting only one quizid and you can use an if clause to check if quizid before sending the mail