2012-03-12 10 views
-1

私は、クイズエンジンであるイントラネットWebアプリケーションを開発しています。管理者は、SendingQuizというページへのリンクをクリックすることで、データベース内のすべてのユーザーにクイズを送信することができます。すべてうまく動作しますが、私の問題は、データベースにクイズがなくても、すべてのユーザに電子メールが送信されることです。このようなことを避けるためにこのコードをどのように変更する必要がありますか?データベースの設計では、私はで構成されてクイズと呼ばれるテーブルを持っているあなたの情報についてはクイズがあるかどうかをチェックしてシステム管理者にメッセージを表示する方法は?

、: 私が今欲しいQuizID、タイトル、説明、オペレーターisSent

が代わりに使用可能な任意のクイズせずに電子メールを送信するのですユーザーがダンプエラーページに直面することができるデータベースでは、私はシステムにクイズがあるかどうかを確認するシステムにしたい。そうでない場合は、何も送信せず、データベースにクイズがなくなったことを管理者に伝えるべきです。 これはどうやって行うのですか?

私のC#コード:

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("[email protected]", "My Application"); 

      // In case the mail system doesn't like no to recipients. This could be removed 
      //msg.To.Add("[email protected]"); 

      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(); 
     } 
    } 
+2

あなたは知っている - デバッグと呼ばれるツールがあり、ステップバイステップで実行されます。しばらく時間がかかりますが、こちらのような質問をするよりも使用することをお勧めします。これがなぜ起こるのかを見つけるためにあなたのコードをステップしてください。 – Aristos

答えて

0

あなたが読者のために一つだけquizidを期待しているとし、句場合は、最小限の選択SendEmailTOAllUser内のブロックを使用した後、メール

protected void SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml) 
    { 
     SmtpClient sc = new SmtpClient("MAIL.Aramco.com"); 
     try 
     { 
      MailMessage msg = new MailMessage(); 
      msg.From = new MailAddress("[email protected]", "PMOD Safety Services Portal (PSSP)"); 

      // In case the mail system doesn't like no to recipients. This could be removed 
      //msg.To.Add("[email protected]"); 

      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=psspdbTest;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) 
       { 
        if(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("@aramco.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://pmv/pssp/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 /> 
          Also, give yourself a chance to gain more safety culture by reading the PMOD Newsletter. 
          <br /> <br /><br /> <br /> 
          This email was generated using the <a href='http://pmv/pssp/Default.aspx'>PMOD Safety Services Portal (PSSP) </a>. 
          Please do not reply to this email. 
          "; 

       int sendCount = 0; 
       List<string> addressList = new List<string>(sEMailAddresses.Split(',')); 
       StringBuilder addressesToSend = new StringBuilder(); 
if(!string.IsNullOrEmpty(quizid)){ 
       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(); 
     } 
    } 
+0

ご協力いただきありがとうございます。ほんとうにありがとう。 –

0

最も簡単な解決策は、行数を決定するスカラークエリを実行することである。

SELECT COUNT(*) FROM Quiz 

それがゼロより大きい場合、にクイズがあります表!

0

あなたはすでにクイズを取得しています。それがnullの場合は、管理者にメールを送り、返却してください。

0

を送信する前にquizidかどうかを確認するために使用できますが使用する必要がいけませんquizidの場合、quizidがnullであるかどうかをチェックし、conn.Close()までのコードをこの条件に埋め込む必要があります。

関連する問題