do you know why this won't work?

T

Tim T

Hi, I'm new to .NET and am still trying to get my head around OO concepts.

I am building an asp.net app and on several pages i want to check to see if
a session object exists, if so - contine to show the page, if not check if
cookie exists, if no cookie then redirect to a login page.
This is easy enough, but now i want to put this functionallity in a class so
that i can just call a checkSupplierID method from that class easily from
any page

This is what i have done:

//method in tools class

public void checkSupplierIDSession()
{
if(Session["supplierID"] == null)
{
HttpCookie cookie = Request.Cookies["supplierID"];
if (cookie != null)
{
Session["supplierID"] = cookie.Value.ToString();
}
else
{
Server.Transfer("Login.aspx?callingPage=" +
this.Page.ToString().Substring(4,this.Page.ToString().Length - 9));
//redirect with this page as calling page
}
}
}

code in aspx.cs code behind file on page i want to check:

private void Page_Load(object sender, System.EventArgs e)
{
tools.checkSupplierIDSession();
}

I get this error when trying to compile:

An object reference is required for the nonstatic field, method, or property
'tools.checkSupplierIDSession()'

Initially the method was static, but that meant that i couldn't use the
Session, Request, and this.Page properties on the calling page?

can someone please advise on the best way to do what i am trying to achieve?
Thanks for your help

Tim
 
K

Kevin Spencer

Hi Tim,

It's not whether it's static or not that is the problem. The problem is that
it is not a Page class, or another class which intrinsicly gets the
HttpContext. However, all you have to do is reference the Current
HttpContext in your method to use it. Example:

if(HttpContext.Current.Session["supplierID"] == null)

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
T

Tim T

Thanks Kevin, that worked a treat, (after a little change to the bit of code
that gets the 'callingPage')
I appreciate your help

Tim..


Kevin Spencer said:
Hi Tim,

It's not whether it's static or not that is the problem. The problem is that
it is not a Page class, or another class which intrinsicly gets the
HttpContext. However, all you have to do is reference the Current
HttpContext in your method to use it. Example:

if(HttpContext.Current.Session["supplierID"] == null)

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.

Tim T said:
Hi, I'm new to .NET and am still trying to get my head around OO concepts.

I am building an asp.net app and on several pages i want to check to see if
a session object exists, if so - contine to show the page, if not check if
cookie exists, if no cookie then redirect to a login page.
This is easy enough, but now i want to put this functionallity in a
class
so
that i can just call a checkSupplierID method from that class easily from
any page

This is what i have done:

//method in tools class

public void checkSupplierIDSession()
{
if(Session["supplierID"] == null)
{
HttpCookie cookie = Request.Cookies["supplierID"];
if (cookie != null)
{
Session["supplierID"] = cookie.Value.ToString();
}
else
{
Server.Transfer("Login.aspx?callingPage=" +
this.Page.ToString().Substring(4,this.Page.ToString().Length - 9));
//redirect with this page as calling page
}
}
}

code in aspx.cs code behind file on page i want to check:

private void Page_Load(object sender, System.EventArgs e)
{
tools.checkSupplierIDSession();
}

I get this error when trying to compile:

An object reference is required for the nonstatic field, method, or property
'tools.checkSupplierIDSession()'

Initially the method was static, but that meant that i couldn't use the
Session, Request, and this.Page properties on the calling page?

can someone please advise on the best way to do what i am trying to achieve?
Thanks for your help

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top