我正在尝试在Windows服务应用程序中使用会话[“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();
答案 0 :(得分: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
- 除非您使用它,因为它允许您动态声明变量,原样。