当前上下文错误中不存在名称`row`

时间:2016-08-16 10:03:31

标签: c# asp.net list email

我想根据

参数向动态用户发送邮件
row["userName"].ToString()

我在我的代码中使用了上面的代码,

foreach (var row in group)
{
    sbodyMail += "<tr>" +
        "<td style='width: 100px; height: 14px;background-color:" + strcolorDet + "'>" + row["userName"].ToString() + " </td> " +
        "<td style='width: 100px; height: 14px;background-color:" + strcolorDet + "'>" + row["Doc_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 strExp = "";
string startupPath = "";
List<string> ls_attach1 = new List<string>();
MailMessage mail = new MailMessage();
startupPath = Environment.CurrentDirectory;
strExp = "RAName = '" + group.Key + "'";
DataTable dtNew = ds.Tables[1].Select(strExp).CopyToDataTable();
DataSet dsNew = new DataSet();
dsNew.Tables.Add(dtNew);

ExcelLibrary.DataSetHelper.CreateWorkbook(startupPath + "\\Attachment\\Reminder_Sheet.xls", dsNew);
ls_attach1.Add(startupPath + "\\Attachment\\Reminder_Sheet.xls");
foreach (var attach in ls_attach1)
{
    mail.Attachments.Add(new Attachment(attach));
}

ce.SendEmail(row["userName"].ToString(), "", "", "Information on documents for processing", sbodyMail, "AUTOSQL", "Powersoft", ls_attach1, "ConnectionString");

但我得到错误:

  

当前上下文错误

中不存在名称row

1 个答案:

答案 0 :(得分:1)

如果你在这一行上得到它:

ce.SendEmail(row["userName"].ToString(), "", "", "Information on documents for processing", sbodyMail, "AUTOSQL", "Powersoft", ls_attach1, "ConnectionString");

变量row仅存在于foreach的范围内。 }foreach之后您还没有row