0

我正在尝试在 Windows 服务应用程序中使用Session["emailcc3"] ..但它向我显示了一个错误会话在此上下文中不可用....有没有其他方法可以做到这一点?

这是我的代码:

 foreach (DataRow newRow in employee.Rows)
                {
                    num = num - 1;



                    if (num == 4)
                    {
                        Session["emailto"] = newRow["EMAILID"].ToString();
                    }
                    if (num == 3)
                    {
                        Session["emailcc1"] = newRow["EMAILID"].ToString();
                    }
                    if (num == 2)
                    {
                        Session["emailcc2"] = newRow["EMAILID"].ToString();
                    }
                    if (num == 1)
                    {
                        Session["emailcc3"] = newRow["EMAILID"].ToString();
                    }

                }

                string emailto = Session["emailto"].ToString();

                string emailcc1 = Session["emailcc1"].ToString();

                string emailcc2 = Session["emailcc2"].ToString();

                string emailcc3 = Session["emailcc3"].ToString();
4

1 回答 1

0

如果我大胆猜测,我认为您想要的只是:

string emailto = null;
string emailcc1 = null;
string emailcc2 = null;
string emailcc3 = null;

foreach (DataRow newRow in employee.Rows)
{
    num = num - 1;
    switch (num)
    {
       case 4:
          emailto = newRow["EMAILID"].ToString();
          break;
       case 3:
          emailcc1 = newRow["EMAILID"].ToString();
          break;
       case 2:
          emailcc2 = newRow["EMAILID"].ToString();
          break;
       case 1:
          emailcc3 = newRow["EMAILID"].ToString();
          break;
       default:
          //Do something here?
    }
}

我不确定您为什么要Session使用它-除非您使用它,因为它允许您动态声明变量,就像它一样。

于 2013-07-09T06:21:02.027 回答