473,769 Members | 6,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cache is a tpe and cannot be used as an expression error

I see several reference to using the ASP.NET 2.0 Cache object in C# where
the documentation just shows snippets like this:
if (Cache["sometoken"] == null)
... do something

However when I use this code in a C# class that is called from an ASP.NET
page, the compiler gives this error:
'Cache' is a tpe and cannot be used as an expression

Furthermore when I type in Cache and press the "." for intellisense, the
only things that pop up are Equals, NoAbsoluteExpir ation,
NoSliderExpirat ion, ReferenceEquals . No methods like Add, Insert and so
forth show up.

This drove me crazy for a while. Then I decided to try my cache code
directly in my ASP.NET code behind page and it worked just fine.

Eventually after mucking around for a while I found that if I did this in my
C# class file then things worked correctly:

public Cache MyCache;
MyCache = System.Web.Http Context.Current .Cache;

and now I can use if (MyCache["sometoken'] == null) from my C# class and it
works just fine.

A couple of questions:

1) Is this really necessary or am I referencing or doing something in
correctly?

2) Is there another approach to accessing the Cache from a C# class called
by a ASP.NET code behind, or did I stumble upon the right approach by what I
am using here?

Hopefully I'll get some clarification as to what is/was going on and why
this was necessary, and hopefully someone will find this post one day and it
will save them a lot of time.

Thanks,

Steve

Nov 19 '05 #1
11 3729
Steve,

That's what you had to do. The Cache object is used by the .aspx page
natively and therefore the object has already been instantiated. In a class
file the Cache object is not already being used so you had to explicitly
create a holder for it and then get the cache object being used on the page.

Short story, you're doing it exactly right.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Steve Franks" <pl****@postrep lyhere.com> wrote in message
news:rY******** ************@co mcast.com...
I see several reference to using the ASP.NET 2.0 Cache object in C# where
the documentation just shows snippets like this:
if (Cache["sometoken"] == null)
... do something

However when I use this code in a C# class that is called from an ASP.NET
page, the compiler gives this error:
'Cache' is a tpe and cannot be used as an expression

Furthermore when I type in Cache and press the "." for intellisense, the
only things that pop up are Equals, NoAbsoluteExpir ation,
NoSliderExpirat ion, ReferenceEquals . No methods like Add, Insert and so
forth show up.

This drove me crazy for a while. Then I decided to try my cache code
directly in my ASP.NET code behind page and it worked just fine.

Eventually after mucking around for a while I found that if I did this in
my C# class file then things worked correctly:

public Cache MyCache;
MyCache = System.Web.Http Context.Current .Cache;

and now I can use if (MyCache["sometoken'] == null) from my C# class and
it works just fine.

A couple of questions:

1) Is this really necessary or am I referencing or doing something in
correctly?

2) Is there another approach to accessing the Cache from a C# class called
by a ASP.NET code behind, or did I stumble upon the right approach by what
I am using here?

Hopefully I'll get some clarification as to what is/was going on and why
this was necessary, and hopefully someone will find this post one day and
it will save them a lot of time.

Thanks,

Steve

Nov 19 '05 #2
be sure System.Web.dll is references by project your class is in, and to get
the current Cache object being used by the asp.net, call
System.Web.Http Contect.Current .Cache
Nov 19 '05 #3
Thanks. On a related note, I am now having difficulty getting to the Page
object within the same C# class that is called from my ASP.NET code behind.

For example, I can do the following just fine in my ASP.NET code behind:
Repeater1.ItemT emplate = Page.LoadTempla te("mytemplate. ascx");

However I want to be able to do the same thing from my C# class, but I
cannot figure out how to hook into the current Page instance from within my
C# class.

For example, when it came to the Cache object, I figured out that I had to
use System.Web.Http Context.Current .Cache to hook into the Cach object
instance. I assume I need to do something similar to get at the current
Page object?.

I've been poking around but there is no System.Web.Http Context.Current .Page
object and I can not figure out how to tap into the current Page so that I
can do a Page.LoadTempla te from my C# class.

Can you please advise on what I am missing? Thanks!

Steve
"S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate. com> wrote in
message news:uk******** ******@TK2MSFTN GP09.phx.gbl...
Steve,

That's what you had to do. The Cache object is used by the .aspx page
natively and therefore the object has already been instantiated. In a
class file the Cache object is not already being used so you had to
explicitly create a holder for it and then get the cache object being used
on the page.

Short story, you're doing it exactly right.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

Nov 19 '05 #4
it sounds like a bad design, contolling controls of the page outside the
page. but maybe you can try to add the Page to Session in Global.asax
Application_Beg inRequest. then you can access it by
(System.Web.UI. Page)System.Web .HttpContext.Cu rrent.Session["CurrentPag e"];
Nov 19 '05 #5
I don't think its a case of a bad design. I have a utility class used for
various supporting functions that I need to access through my ASP.NET
application.

Some of my pages used Page.LoadTempla te. In this particular case, I want to
write a helper function that performs the LoadTemplate and then stores the
result in the Cache. In subsequent requests for this template the utility
class will return the cached version. Since different ASP.NET pages need
this functionality, I'm trying to centralize this in this C# utility class.

Anyone know how Page.LoadTempla te can be used from a C# class? Details on
what I'm trying to do are in my previous post on this topic.

Thanks,

Steve

"The Crow" <q> wrote in message
news:uo******** ******@TK2MSFTN GP14.phx.gbl...
it sounds like a bad design, contolling controls of the page outside the
page. but maybe you can try to add the Page to Session in Global.asax
Application_Beg inRequest. then you can access it by
(System.Web.UI. Page)System.Web .HttpContext.Cu rrent.Session["CurrentPag e"];

Nov 19 '05 #6
you can inherit from System.Web.UI.P age and extend the base functionalty.
and then other Page classes in your application those need this functionalty
inherits from that class instead of directly inheriting from Page class.. i
usualy sense bad designs at the beginning. and all i try to do is help you.
happy programming :)
Nov 19 '05 #7
Sounds like a great idea, thanks! I don't think I'm doing this right though.
Here is what I did so far:

1) I created a new class called SuperPage. Here is the source:

public class CustomPage : System.Web.UI.P age
{
public CustomPage()
{
}
public new ITemplate LoadTemplate(st ring filename)
{
// TODO: Retrieve from Cache if previously loaded
ITemplate iTemplate = base.LoadTempla te(filename);
// TODO: Store into Cache if was not previously loaded
return iTemplate;
}
}

2) I then changed my ASP.NET code behind page to derrive from SuperPage
instead of Page, by doing this:

changed:
public partial class mytest : System.Web.UI.P age

to
public partial class mytest : CustomPage

Inside my Page_Load event in the ASP.NET I have this line:
ITemplate itemTemplate = Page.LoadTempla te("myTemplate. ascx");

OK, those are the changes I made, and put a breakpoint in the constructor of
CustomPage and CustomPage's LoadTemplate so I could be sure it was being
called.

Unfortunately this is not working. The challenge I'm having is that when I
run my page, the debugger does stop in my CustomPage constructor. However
when this code executes:
ITemplate itemTemplate = Page.LoadTempla te("myTemplate. ascx");
my LoadTemplate method is not getting called.

Apparently the origional Page's LoadTemplate is getting called and my custom
one never is...

So then I thought that maybe the problem was the I was still using
"Page.LoadTempl ate" and that instead I needed to do this in my ASP.NET page:
CustomPage cp = new CustomPage();
ITemplate itemTemplate = cp.LoadTemplate ("myTemplate.as cx");

However this did not work either. Although CustomPage's LoadTemplate does at
least get called, it throws a nullreference exception on this line saying
that parameter "basepath" cannot be null:
ITemplate iTemplate = base.LoadTempla te(filename);

I don't understand why this is null anyway, since CustomPage derrives from
Page, so I should be able to refer to the base set of methods and properties
using "base" right?

At any rate I think using cp.LoadTemplate is likely the wrong approach since
it doesn't make sense that I'd have to create a new instance of a CustomPage
to call LoadTemplate when the ASP.NET page derrives from CustomPage in the
first place.

As you can see I'm quite confused :) but I think some of my approach is
right and perhaps I'm just missing one part? At any rated I'd greatly
appreciate hearing back on what you think I'm messing up.

Thanks very much for your time! It is greatly appreciated.

Steve
Nov 19 '05 #8

"Steve Franks" <pl****@postrep lyhere.com> wrote in message
news:z7******** ************@co mcast.com...
Sounds like a great idea, thanks! I don't think I'm doing this right
though. Here is what I did so far:

1) I created a new class called SuperPage. Here is the source:

public class CustomPage : System.Web.UI.P age
{
public CustomPage()
{
}
public new ITemplate LoadTemplate(st ring filename)
{
// TODO: Retrieve from Cache if previously loaded
ITemplate iTemplate = base.LoadTempla te(filename);
// TODO: Store into Cache if was not previously loaded
return iTemplate;
}
}

things seems OK up to here.
2) I then changed my ASP.NET code behind page to derrive from SuperPage
instead of Page, by doing this:

changed:
public partial class mytest : System.Web.UI.P age

to
public partial class mytest : CustomPage

Inside my Page_Load event in the ASP.NET I have this line:
ITemplate itemTemplate = Page.LoadTempla te("myTemplate. ascx");


Here, the referense "Page" is of kind "System.Web.UI. Page".. Since
LoadTemplate method is not virtual you used new keyword. Calling it via base
type's reference calls "original version" of the method because polimorphism
is not at charge here.. you shoul dcall that method after casting to
CustomPage > ((CustomPage)Pa ge).LoadTemplat e or if you are calling it within
Page instance you can directly call this.LoadTempla te.. Or you can inclue
public new CustomPage Page
{
get{return (CustomPage)Pag e;}
}
then it should be ok.

By the way, if templates you are talking about are just user controls, why
dont you use Page's-User Control's output caching mechanism? you can do that
by adding <%@ OutputCache Duration="60"> direction in your usercontol file.
you can customize caching options.. check msdn for further info.
Nov 19 '05 #9

things seems OK up to here.
2) I then changed my ASP.NET code behind page to derrive from SuperPage
instead of Page, by doing this:

changed:
public partial class mytest : System.Web.UI.P age

to
public partial class mytest : CustomPage

Inside my Page_Load event in the ASP.NET I have this line:
ITemplate itemTemplate = Page.LoadTempla te("myTemplate. ascx");

Here, the referense "Page" is of kind "System.Web.UI. Page".. Since
LoadTemplate method is not virtual you used new keyword. Calling it via
base type's reference calls "original version" of the method because
polimorphism is not at charge here.. you shoul dcall that method after
casting to CustomPage > ((CustomPage)Pa ge).LoadTemplat e or if you are
calling it within Page instance you can directly call this.LoadTempla te..
Or you can inclue
public new CustomPage Page
{
get{return (CustomPage)Pag e;}
}
then it should be ok.


Thanks but unfortunately I am not able to follow any of that :) What
specifically do I need to add to my code? Not sure what I am supposed to do
with "CustomPage > ((CustomPage)Pa ge).LoadTemplat e" or where it should go.
Likewise for:
public new CustomPage Page
{
get{return (CustomPage)Pag e;}
}

That would go in my CustomPage class and automatically make things work?
By the way, if templates you are talking about are just user controls, why
dont you use Page's-User Control's output caching mechanism? you can do
that by adding <%@ OutputCache Duration="60"> direction in your usercontol
file. you can customize caching options.. check msdn for further info.


Yes, they are user controls. In summary, I am using the Repeater control.
I need to place over a dozen different repeater controls on the same page to
create several html tables of output, with each table holding a different
category of results.

So instead of having to hard code the ItemTemplate and
AlternatingItem Template values, what I did was place the templates into
their own .ascx file. Then I use this:
Repeater1.ItemT emplate = Page.LoadTempla te("mytemplate. ascx");
Repeater1.Alter natingItemTempl ate =
Page.LoadTempla te("mytemplateA lt.ascx");
Repeater2.ItemT emplate = Page.LoadTempla te("mytemplate. ascx");
Repeater2.Alter natingItemTempl ate =
Page.LoadTempla te("mytemplateA lt.ascx");

So the whole purpose of my trying to do this caching was to prevent the
LoadTemplate from going to disk every time.

Are you saying that if I add those caching options to the @ directive in the
..ascx that it will accomplish this caching for me? What would make sense,
as I was surprised I had to do this on my own giving the new Cache control
that was added.

Thanks,

Steve
Nov 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1666
by: NWx | last post by:
Hi, I' trying to implement a callback method when a cache object expires I want to do this to automatically logout user after a timeout (for demo purposes) My thought is, when user logon, create a cache object which expires after, let's say, 15 minutes, and in that moment, to direct user to a page telling the time to run demo app was expired.
6
1990
by: Charts | last post by:
I used HttpContext.Current.Cache To cache data from database. The code is like that. public static DataView GetCategories() { if ( HttpContext.Current.Cache == null ) { HttpContext.Current.Cache = GetCategoriesFromDB(); } return (DataView)HttpContext.Current.Cache;}
4
34779
by: eelis.net | last post by:
Hi I tried to convert the following C# code to vb.net. code in C# ________________________________________________________________ using System; using System.Reflection; using System.Resources;
19
7928
by: lawrence k | last post by:
How can I find out where my script is outputting to the screen for the first time? My error logs are full of stuff like this: PHP Warning: session_start(): Cannot send session cache limiter - headers already sent in /home/httpd/vhosts/monkeyclaus.org/httpdocs/media/audio/pdsIncludes/CommandStartSession.php on line 14
0
2315
by: mateipuiu | last post by:
When a try to run a client build on 2005, which uses the Microsoft.ApplicationBlocks.Cache.dll reference, when using a Microsoft.ApplicationBlocks.Cache.dll created on Debug mode, the client works just fine, but when a use a Microsoft.ApplicationBlocks.Cache.dll created on Release mode, the client doesn't work no more, and I get this error message: ********************************************* 1) Exception Information...
0
1569
by: Scorpio1769 | last post by:
I've got a macro on an mde. database that has been working for years. all of the sudden, my user reports to me that upon execution of the macro, he gets the error message "The function you entered cannot be used in this expression. *You may have used a DoEvents,....." This macro fires on the after update event of a combo box and simply creates a purchase order number according to the following.
0
1980
by: =?Utf-8?B?YmlqYXk=?= | last post by:
The type initializer for 'Microsoft.ApplicationBlocks.Cache.CacheService' threw an exception. We migrated our windows application from 1.1 to 2.0. The debug and Release mode of the application work fine with some tweaking. But when the setup project is migrated to 2.0 the installation gives the follwing error: - <ExceptionInformation> <AdditionalInformationProperty ExceptionManager.MachineName="TestDev"...
0
4569
by: SMH | last post by:
Hi All, I am currently learning .Net 2, studying for 70-528. I've hit a bit of a brick wall with DataColumn.Expression. As I understand it, this can be used to (For example) concatenate two columns to make a derived column. When I run my code, I get this error: ------------
8
20434
by: Stefano Sabatini | last post by:
Hi all, I'm encountering this while trying to implement a factory singleton method to generate objects. The singleton has a static map which binds a static creation function defined in each class to the type of the object to be created. Here it is the code, which is a modification of the wikipedia C++ factory example code: ----------------------------------8<--------------------------------
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.