2016-07-28 67 views
-1

我有一個代碼,其中有記錄來自SP。我想發送附件而不管他們的數據給他們的用戶。但只有79th記錄作爲附件發送給每個用戶。我不知道爲什麼。只有一個附件被解僱而不是許多記錄

private void Form1_Load(object sender, EventArgs e) 
    { 
     Cls_Email_Sql ce = new Cls_Email_Sql(); 
     List<string> ls_attach = new List<string>(); 
     using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(SqlConn)) 
     { 
      using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand()) 
      { 
       cmd.CommandText = "GET_INWARD_REMINDER_REPORT"; 
       cmd.Connection = conn; 
       cmd.CommandType = CommandType.StoredProcedure; 
       conn.Open(); 
       System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(cmd); 
       DataSet ds = new DataSet(); 
       adapter.Fill(ds); 
       conn.Close(); 
       DataTable tb_RA = ds.Tables[0]; 
       DataTable tb_User = ds.Tables[1]; 

       string strcolorHead = "#C0C0C0"; 
       string strcolorDet = "#C0C0C0"; 

       var groups = tb_RA.AsEnumerable().GroupBy(r => r.Field<string>("RAName")); 

       foreach (var group in groups) // RA Table 
       { 
        sbodyMail = "Dear " + group.Key + ", <br /><br /> " + 

        "As per the details available in the system, below are the summary "+ 
        "of number of documents lying with your reportees for more than five days. "+ 
        "This is for your information and necessary action "; 

        sbodyMail += "<table style='width: 400px;font-size:12px;font-family: Arial, Helvetica, sans-serif;' " + 
           "border='0'><tr><td style='width: 100%;'></b><td></tr></table> " + 

           "<table style='width: 470px;font-size:12px; font-family: Arial, Helvetica, sans-serif;height: 53px' border='1'><tr> " + 
           "<td style='width: 30px; height: 14px;color:black;background-color:" + strcolorHead + " ;white-space:nowrap'><strong>SR No</strong></td> " + 
            "<td style='width: 300px; height: 14px;color:black;background-color:" + strcolorHead + " ;white-space:nowrap'><strong>UserName</strong></td> " + 
           "<td style='width: 120px; height: 14px;color:black;background-color:" + strcolorHead + " ;white-space:nowrap'><strong>Document type</strong></td> " + 
            "<td style='width: 20px; height: 14px;color:black;background-color:" + strcolorHead + " ;white-space:nowrap'><strong><div>No. of docs working </div><div> for more than five days</div></strong></td> "; 

        foreach (var row in group) 
        { 
         sbodyMail += "<tr>" + 
          "<td style='width: 30px; height: 14px;background-color:" + strcolorDet + "'>" + row["SR_No"].ToString() + " </td> " + 
          "<td style='width: 100px; height: 14px;background-color:" + strcolorDet + "'>" + row["userName"].ToString() + " </td> " + 
          "<td style='width: 100px; height: 14px;background-color:" + strcolorDet + "'>" + row["Document_Type"].ToString() + " </td> " + 
          "<td style='width: 100px; height: 14px;background-color:" + strcolorDet + "'>" + row["CountofDocNo"].ToString() + " </td> " + 
          "</tr>"; 
        } 
        sbodyMail += "</table><br>" + //close of header 

        "<b>THIS IS A SYSTEM GENERATED MAIL. PLEASE DO NOT REPLY </b>"; 
        string startupPath = ""; 
        List<string> ls_attach1 = new List<string>(); 

        MailMessage mail = new MailMessage(); 
        startupPath = Environment.CurrentDirectory; 
        ls_attach1.Add(startupPath + "\\Attachment\\Reminder_Sheet.xls"); 

        string strExp = ""; 
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
        { 
         strExp = "RAName = '" + ds.Tables[0].Rows[i]["RAName"].ToString() + "'"; 
         DataRow[] dr = ds.Tables[0].Select(strExp); 
         ds.Tables[0].AcceptChanges(); 
         DataTable dtNew = ds.Tables[0].Select(strExp).CopyToDataTable(); 
         DataSet dsNew = new System.Data.DataSet(); 
         dsNew.Tables.Add(dtNew); 
         ExcelLibrary.DataSetHelper.CreateWorkbook(startupPath + "\\Attachment\\Reminder_Sheet.xls", dsNew); 
        } 
        foreach (var attach in ls_attach1) 
        { 
         mail.Attachments.Add(new Attachment(attach)); 
        } 
        foreach (Attachment attachments in mail.Attachments) 
        { 
         attachments.Dispose(); 
        } 
        ce.SendEmail("[email protected]", "", "", "Information on documents for processing", sbodyMail,"AUTOSQL", "Powersoft", ls_attach1, "ConnectionString"); 
       } 
      } 
     } 
    } 

ds.Tables[0].Rows.Count 

我得到79條記錄。但是附件只是爲了第79個紀錄而已。

+0

您可以在處理附件之前嘗試發送電子郵件嗎? – Kld

+0

@Kld:我在發送郵件後添加了該行,但每次只發送第79條記錄。 **爲什麼**? – BNN

回答

1

如果您希望人們通讀它,您通常應該生成一個沒有業務邏輯的問題的簡單例子。這就是說,你的問題是這一行:

ExcelLibrary.DataSetHelper.CreateWorkbook(startupPath + "\\Attachment\\Reminder_Sheet.xls", dsNew); 

查看源爲您使用的庫,這樣可以節省您提供的數據集來使用微軟的Workbook.Save工作簿()。如果你爲每個你想要的DataSet調用它(我認爲你是,從代碼中),你將它們全部保存到相同的路徑,覆蓋每個。

這意味着,在循環訪問您的79份報告之後,您會覆蓋每個報告並將其保留在文件中的最後一個。您應該找到一種方法將它們全部加載到書中的單獨工作表中,或者將它們保存到不同的文件中。

+0

你可以建議如何做到這一點? **我想發送所有RA郵件及其各自的數據作爲附件**請參閱http://i.stack.imgur.com/CkPFp.png – BNN

+0

如果將所有數據表包含在單個數據集中,而不是按照現在的做法將其拆分,保存單個數據集將將所有表格作爲單獨的工作表包含在內。 將dsNew移到您的foreach外部,將表添加到它,然後在循環完成後再執行CreateWorkbook()。 –

+0

你可以建議你怎麼用代碼告訴,因爲我嘗試過,但仍然獲得第79條記錄作爲附件數據 – BNN