协助编写循环以返回数据

时间:2018-07-24 13:58:30

标签: c# .net foreach sqldatasource

我有一张桌子,他们有多个雇员分配给多个经理。数据是从HR的excel文件中导入的。我需要遍历表格,找到分配给每个经理的员工人数,将他们分组在一起,然后向该经理发送一封电子邮件,而不是向多个单独的电子邮件发送。我在如何编写循环来执行此操作时遇到了麻烦。我是编码新手。谢谢。

All of the fields:
 [ Reports To Name]
      ,[ Last Name]
      ,[ Middle Initial]
      ,[ First Name]
      ,[ Department #]
      ,[ Department]
      ,[ Job Code-Title]
      ,[Form Due Date]
      ,[Current Route Step Name]
      ,[ Status]
      ,[ Company]
      ,[Manager Email]

//Get all of the records
  public DataTable OCCheckList()
    {
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM dbo.OrientationChecklistTest", _connectionString);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }

//Count the number of times the manager shows up in the table
    public DataTable OCCount(string ManagerEmail)
    {
        SqlDataAdapter da = new SqlDataAdapter("SELECT COUNT([Manager Email]) AS EmployeeCount FROM [dbo].[OrientationChecklistTest] WHERE [Manager Email]='@ManagerEmail' GROUP BY [Manager Email]", _connectionString);
        da.SelectCommand.Parameters.AddWithValue("@ManagerEmail", ManagerEmail);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }

//Find the names and additional information about the employees for each manager
    public DataTable OCManager(string ManagerEmail)
    {
        SqlDataAdapter da = new SqlDataAdapter("SELECT [Manager Email], [ First Name], [ Last Name], [Current Route Step Name], [Form Due Date] From dbo.OrientationChecklistTest WHERE [Manager Email]='@ManagerEmail' ORDER BY [Manager Email]", _connectionString);
        da.SelectCommand.Parameters.AddWithValue("@ManagerEmail", ManagerEmail);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }





 protected void btnEmail_Click(object sender, EventArgs e)
{

    DataTable dtCheck = OCCheckList();
    string managerEmail = "";
    string currManager = "";
    string firstName = "";
    string lastName = "";
    string reportsTo = "";
    string currReportsTo = "";
    string dueDate = "";
    string stepName = "";
    int count = 0;
    MailMessage myMessage = new MailMessage();
    myMessage.Body = "Please be advised that tasks for the following employees has been assigned to you and is available in your To Do Tile. <br /><br />If you have any questions, do not reply to this e-mail, please contact: Help at <a href=\"mailto:Help@company.org\">Help@company.org</a>. <br /> <br /> Thank you. <br /> <br /> Please click the link to log in to SuccessFactors: <a href=\"http://url.org\">Click Here</a><br /> <br />----------------<br /> <br />";

    /*  foreach (DataRow row in dtCheck.Rows)
      {
          managerEmail = Convert.ToString(row["Manager Email"]);
          reportsTo = Convert.ToString(row[" Reports To Name"]);

          DataTable dtCount = OCCount(managerEmail);
          int numberOfRecords = 0;
              foreach (DataRow row3 in dtCount.Rows)
              {
                      if (row3["Manager Email"].ToString() == managerEmail)
                      {
                          numberOfRecords++;
                      }
                  Console.WriteLine("Count: " + numberOfRecords.ToString());

                  DataTable dtManager = OCManager(managerEmail);

                  foreach (DataRow row2 in dtManager.Rows)
                  { 
                      firstName = Convert.ToString(row[" First Name"]);
                      lastName = Convert.ToString(row[" Last Name"]);
                      dueDate = Convert.ToString(row["Form Due Date"]);
                      stepName = Convert.ToString(row["Current Route Step Name"]);
                      myMessage.Body += "<strong>Employee Name:</strong> " + firstName + " " + lastName + "<br /><strong>Task Due:</strong> " + stepName + "<br /><strong>Due Date:</strong> " + dueDate + "<br />-----<br />";
                      count++;

                          if (count == numberOfRecords)
                          {
                              break;
                          }
                  }
                      myMessage.Subject = "SuccessFactors Message for " + reportsTo;
                      myMessage.From = new MailAddress("SuccessFactorsHelp@atlanticare.org", "AtlantiCare Human Resources");
                      myMessage.To.Add(new MailAddress(managerEmail, reportsTo));
                      myMessage.IsBodyHtml = true;
                      string sSmtpServer = "smtp.atlanticare.org";

                      SmtpClient mySmtpClient = new SmtpClient();
                      mySmtpClient.Host = sSmtpServer;
                      try
                      {
                          Thread t1 = new Thread(delegate ()
                          {
                              mySmtpClient.Send(myMessage);
                              myMessage.Body = "";
                          });
                          t1.Start();
                          lblMessage.Text += "<center>Email sent to: " + managerEmail + "<br /></center>";
                      }
                      catch
                      {
                          lblError.Text += "<center>Email <font color=\"red\">FAILURE</font> to: " + managerEmail + "<br /></center>";
                      }
              break;
              }
      }
      */
    DataTable dtCount = OCCount(managerEmail);
    int numberOfRecords = 0;
    foreach (DataRow row3 in dtCount.Rows)
    {
        if (row3["Manager Email"].ToString() == managerEmail)
        {
            numberOfRecords++;
        }
    }

    foreach (DataRow row in dtCheck.Rows)
    {
        if (managerEmail == "")
        {
            managerEmail = Convert.ToString(row["Manager Email"]);
            reportsTo = Convert.ToString(row[" Reports To Name"]);
            currManager = Convert.ToString(row["Manager Email"]);
            currReportsTo = Convert.ToString(row[" Reports To Name"]);
        }
        else
        {
            managerEmail = Convert.ToString(row["Manager Email"]);
            reportsTo = Convert.ToString(row[" Reports To Name"]);
        }

        if (currManager == managerEmail)
        {
            Response.Write("First IF<br />");
            firstName = Convert.ToString(row[" First Name"]);
            lastName = Convert.ToString(row[" Last Name"]);
            dueDate = Convert.ToString(row["Form Due Date"]);
            stepName = Convert.ToString(row["Current Route Step Name"]);
            myMessage.Body += "<strong>Employee Name:</strong> " + firstName + " " + lastName + "<br /><strong>Task Due:</strong> " + stepName + "<br /><strong>Due Date:</strong> " + dueDate + "<br />-----<br />";
        }

        if (currManager != managerEmail)
        {
            Response.Write("Second IF<br />");
            myMessage.Subject = "SuccessFactors Message for " + reportsTo;
            myMessage.From = new MailAddress("Help@company.org", "Human Resources");
            myMessage.To.Add(new MailAddress(managerEmail, reportsTo));
            myMessage.IsBodyHtml = true;
            string sSmtpServer = "smtp.company.org";

            SmtpClient mySmtpClient = new SmtpClient();
            mySmtpClient.Host = sSmtpServer;
                try
                {
                    Thread t1 = new Thread(delegate ()
                    {
                        mySmtpClient.Send(myMessage);
                    });
                    t1.Start();
                    lblMessage.Text += "<center>Email sent to: " + managerEmail + "<br /></center>";  
                }
                catch
                {
                    lblError.Text += "<center>Email <font color=\"red\">FAILURE</font> to: " + managerEmail + "<br /></center>";
                }

            currManager = managerEmail;
            myMessage.Body = "Please be advised that tasks for the following employees has been assigned to you and is available in your To Do Tile. <br /><br />If you have any questions, do not reply to this e-mail, please contact: Help at <a href=\"mailto:Help@company.org\">Help@company.org</a>. <br /> <br /> Thank you. <br /> <br /> Please click the link to log in to SuccessFactors: <a href=\"http://url.org\">Click Here</a><br /> <br />----------------<br /> <br />";
            firstName = Convert.ToString(row[" First Name"]);
            lastName = Convert.ToString(row[" Last Name"]);
            reportsTo = Convert.ToString(row[" Reports To Name"]);
            dueDate = Convert.ToString(row["Form Due Date"]);
            stepName = Convert.ToString(row["Current Route Step Name"]);
            myMessage.Body += "<strong>Employee Name:</strong> " + firstName + " " + lastName + "<br /><strong>Task Due:</strong> " + stepName + "<br /><strong>Due Date:</strong> " + dueDate + "<br />-----<br />";
        }

        Response.Write("Mgr: " + managerEmail + "<br />");
        Response.Write("CurrMgr: " + currManager + "<br /><br />");

    }

    }


}

样本数据: SampleData

我想要的数据: DataIWant

这样,我可以向每位经理发送一封电子邮件,告知他们应归还的项目以及其下的所有员工。

0 个答案:

没有答案