Reputation: 4721
I have an aspx
page where I am calling a function like below
public static string FunFillEmp(object[] args)
{
string StrPriReturn = "";
DataAccessLayer ObjPriDt = new DataAccessLayer();
DataTable dt = new DataTable();
dt = ObjPriDt.ExecuteDataTable("select mkey,Emp_Name,emp_card_no from emp_mst where "+
"department_mkey='" + args[0].ToString() + "' and status in ('A','S') and hub_mkey IN " +
"(select hub_mkey from emp_mst e,user_mst u where e.mkey=u.employee_mkey and e.STATUS IN ('A','S') "+
"and u.mkey='" + Session["UserId"].ToString().Trim() + "') " +
"order by 2");
if (dt.Rows.Count > 0)
{
for (int IntpriI = 0; IntpriI < dt.Rows.Count; IntpriI++)
{
StrPriReturn += dt.Rows[IntpriI][0].ToString() + "~" + dt.Rows[IntpriI][1].ToString() + "~" + dt.Rows[IntpriI][2].ToString() + "|";
}
}
return StrPriReturn;
}
But Using Session
is giving me error as
an object reference required for nonstatic field
So how would I use Session here?
Upvotes: 0
Views: 72
Reputation: 127
You need to use HttpContext.Current.Session["UserId"]
since you're accessing it inside a static
method.
And yes, you'll need to handle the scenario when you do not assign anything into the Session.
Upvotes: 3
Reputation: 30022
Your Session is not assigned, you need to handle that case:
string UserId = HttpContext.Current.Session["UserId"] == null ? null : HttpContext.Current.Session["UserId"].ToString().Trim();
if(UserId == null)
{
// do something
}
dt = ObjPriDt.ExecuteDataTable("select mkey,Emp_Name,emp_card_no from emp_mst where " +
"department_mkey='" + args[0].ToString() + "' and status in ('A','S') and hub_mkey IN " +
"(select hub_mkey from emp_mst e,user_mst u where e.mkey=u.employee_mkey and e.STATUS IN ('A','S') " +
"and u.mkey='" + UserId + "') " +
"order by 2");
Upvotes: 0