Reputation: 487
Please read my question it is not related to how to access webmethod or how to use session.'
I am facing one issue and did not get it.
I have one sessioninfo class that contains all session related info in that. When I am using it in Page_load() it is working fine.but when I will try to access it in webmethod it show me error like this : "SessionInfo.UserId error CS0103: The name 'SessionInfo' does not exist in the current context"
here is my code SessionInfo Class
namespace Abc.Common
{
public class SessionInfo
{
public static decimal UserID
{
get
{
try
{
if (HttpContext.Current.Session["UserID"] != null)
return Convert.ToDecimal(HttpContext.Current.Session["UserID"]);
return 0;
}
catch (Exception)
{
return 0;
}
}
set
{
HttpContext.Current.Session["UserID"] = value;
}
}
...
...
}
}
//backend code of asp.net (.aspx.cs)
using Abc.common;
....
Code in Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (SessionInfo.UserID == 0) //here its working fine
{
Response.Redirect(Page.ResolveUrl("~/Default.aspx?errormsg=Your Session Is Expired."));
}
if (!IsPostBack)
{
...
}
}
Code of Webmethod
[System.Web.Services.WebMethod]
public static DataTable ValidateUploadedFiles()
{
//if (Abc.Common.SessionInfo.UserID == 0) //its working fine
if (SessionInfo.UserID == 0)
{
...
}
}
when I used sessioninfo variable directly its giving me error(already namespace is included in using statement)
SessionInfo.UserID //this want work.
when I will used its with namespace with class its working fine.
Abc.Common.SessionInfo.UserID //this is working fine.
Upvotes: 0
Views: 185
Reputation: 336
From your description I'm assuming that you are missing "using" statement at the top of your file. Just add "using Abc.Common;" in first line of your file and it should work fine. Oh. And it has nothing to do with it being webmethod. It's just how namespaces work.
Upvotes: 2