473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why do I "lose context" when I try to move certain operations to class methods?


Hoping someone can help with a simple but puzzling problem. I have some code
that I want to build as a class method. The code works fine when I embed it
in Page_Load. But when I try to generalize the code into the method of a
class I am trying to build, it gives me strange errors: "CS0103: The name
'Server' does not exist in the current context". (Server being a call to
Server.MapPath.)

I'm working in ASP.NET 2.0 and VS.NET 2005.

Here's the code that works just fine:

public partial class aspmht_test1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
aspNetMHT.MHT m = new aspNetMHT.MHT();

string protectedUrl = "http://www.google.com";
m.LoadUrl(protectedUrl);
...
string filename = "new" + DateTime.Now.ToFileTime().ToString() + ".zip";

//save it the filesystem.
m.SaveToFile(Server.MapPath(filename), true); // This is where I get
errors if I move to a class method
}
}

So far, so good. When I try to build a 'Scraper' class, and embed the code
in a method, I get errors on the line notated above. Here's my class code:

public class Scraper
{
public void GetPageAndSave(string remoteurl, string savefilename)

{

aspNetMHT.MHT m = new aspNetMHT.MHT();

m.LoadUrl(remoteurl);
m.Parse();
string filename = savefilename +
DateTime.Now.ToFileTime().ToString() + ".zip";
m.SaveToFile(Server.MapPath(filename), true);
}

The code fails on the last line with the error: CS0103: The name 'Server'
does not exist in the current context.

The code above is in Scraper.cs. Here's how I'm calling this in my page

public partial class aspmht_test2 : System.Web.UI.Page
{

public void Page_Load(object sender, EventArgs e)
{
Scraper myScraper = new Scraper();
string myURL = "http://whatever.com";
myScraper.GetPageAndSave(myURL, "whatever");
}
}

Any help out there? I'm outta ideas....Thanks in advance.

-KF

Nov 19 '05 #1
5 3028
You have to include the namespace for the Server in the file for your helper
classes. The namespace is defined in the file with your page class, but not
automatically if you create more class files.

<ke*****@u.washington.edu> wrote in message
news:#s**************@TK2MSFTNGP15.phx.gbl...

Hoping someone can help with a simple but puzzling problem. I have some code that I want to build as a class method. The code works fine when I embed it in Page_Load. But when I try to generalize the code into the method of a
class I am trying to build, it gives me strange errors: "CS0103: The name
'Server' does not exist in the current context". (Server being a call to
Server.MapPath.)

I'm working in ASP.NET 2.0 and VS.NET 2005.

Here's the code that works just fine:

public partial class aspmht_test1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
aspNetMHT.MHT m = new aspNetMHT.MHT();

string protectedUrl = "http://www.google.com";
m.LoadUrl(protectedUrl);
...
string filename = "new" + DateTime.Now.ToFileTime().ToString() + ".zip";

//save it the filesystem.
m.SaveToFile(Server.MapPath(filename), true); // This is where I get
errors if I move to a class method
}
}

So far, so good. When I try to build a 'Scraper' class, and embed the code
in a method, I get errors on the line notated above. Here's my class code:
public class Scraper
{
public void GetPageAndSave(string remoteurl, string savefilename)

{

aspNetMHT.MHT m = new aspNetMHT.MHT();

m.LoadUrl(remoteurl);
m.Parse();
string filename = savefilename +
DateTime.Now.ToFileTime().ToString() + ".zip";
m.SaveToFile(Server.MapPath(filename), true);
}

The code fails on the last line with the error: CS0103: The name 'Server'
does not exist in the current context.

The code above is in Scraper.cs. Here's how I'm calling this in my page

public partial class aspmht_test2 : System.Web.UI.Page
{

public void Page_Load(object sender, EventArgs e)
{
Scraper myScraper = new Scraper();
string myURL = "http://whatever.com";
myScraper.GetPageAndSave(myURL, "whatever");
}
}

Any help out there? I'm outta ideas....Thanks in advance.

-KF

Nov 19 '05 #2
Thanks. In C#, you're saying I need to include an import directive at the
top of the .cs file? What namespace to I need to be importing (which
namespace manages Server.whatever operations?)

Thanks again.

-KF

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:uz****************@TK2MSFTNGP14.phx.gbl...
You have to include the namespace for the Server in the file for your
helper
classes. The namespace is defined in the file with your page class, but
not
automatically if you create more class files.

<ke*****@u.washington.edu> wrote in message
news:#s**************@TK2MSFTNGP15.phx.gbl...

Hoping someone can help with a simple but puzzling problem. I have some

code
that I want to build as a class method. The code works fine when I embed

it
in Page_Load. But when I try to generalize the code into the method of a
class I am trying to build, it gives me strange errors: "CS0103: The name
'Server' does not exist in the current context". (Server being a call to
Server.MapPath.)

I'm working in ASP.NET 2.0 and VS.NET 2005.

Here's the code that works just fine:

public partial class aspmht_test1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
aspNetMHT.MHT m = new aspNetMHT.MHT();

string protectedUrl = "http://www.google.com";
m.LoadUrl(protectedUrl);
...
string filename = "new" + DateTime.Now.ToFileTime().ToString() +
".zip";

//save it the filesystem.
m.SaveToFile(Server.MapPath(filename), true); // This is where I get
errors if I move to a class method
}
}

So far, so good. When I try to build a 'Scraper' class, and embed the
code
in a method, I get errors on the line notated above. Here's my class

code:

public class Scraper
{
public void GetPageAndSave(string remoteurl, string savefilename)

{

aspNetMHT.MHT m = new aspNetMHT.MHT();

m.LoadUrl(remoteurl);
m.Parse();
string filename = savefilename +
DateTime.Now.ToFileTime().ToString() + ".zip";
m.SaveToFile(Server.MapPath(filename), true);
}

The code fails on the last line with the error: CS0103: The name
'Server'
does not exist in the current context.

The code above is in Scraper.cs. Here's how I'm calling this in my page

public partial class aspmht_test2 : System.Web.UI.Page
{

public void Page_Load(object sender, EventArgs e)
{
Scraper myScraper = new Scraper();
string myURL = "http://whatever.com";
myScraper.GetPageAndSave(myURL, "whatever");
}
}

Any help out there? I'm outta ideas....Thanks in advance.

-KF


Nov 19 '05 #3
Keep this link handy...

The .Net Framework 2.0 Class Browser :
http://beta.asp.net/QUICKSTART/util/classbrowser.aspx

MapPath is a method in System.Web.HttpRequest.

http://beta.asp.net/QUICKSTART/util/...ss=HttpRequest


Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

<ke*****@u.washington.edu> wrote in message news:eu**************@TK2MSFTNGP12.phx.gbl...
Thanks. In C#, you're saying I need to include an import directive at the top of the
.cs file? What namespace to I need to be importing (which namespace manages
Server.whatever operations?)

Thanks again.

-KF

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:uz****************@TK2MSFTNGP14.phx.gbl...
You have to include the namespace for the Server in the file for your helper
classes. The namespace is defined in the file with your page class, but not
automatically if you create more class files.

<ke*****@u.washington.edu> wrote in message
news:#s**************@TK2MSFTNGP15.phx.gbl...

Hoping someone can help with a simple but puzzling problem. I have some

code
that I want to build as a class method. The code works fine when I embed

it
in Page_Load. But when I try to generalize the code into the method of a
class I am trying to build, it gives me strange errors: "CS0103: The name
'Server' does not exist in the current context". (Server being a call to
Server.MapPath.)

I'm working in ASP.NET 2.0 and VS.NET 2005.

Here's the code that works just fine:

public partial class aspmht_test1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
aspNetMHT.MHT m = new aspNetMHT.MHT();

string protectedUrl = "http://www.google.com";
m.LoadUrl(protectedUrl);
...
string filename = "new" + DateTime.Now.ToFileTime().ToString() + ".zip";

//save it the filesystem.
m.SaveToFile(Server.MapPath(filename), true); // This is where I get
errors if I move to a class method
}
}

So far, so good. When I try to build a 'Scraper' class, and embed the code
in a method, I get errors on the line notated above. Here's my class

code:

public class Scraper
{
public void GetPageAndSave(string remoteurl, string savefilename)

{

aspNetMHT.MHT m = new aspNetMHT.MHT();

m.LoadUrl(remoteurl);
m.Parse();
string filename = savefilename +
DateTime.Now.ToFileTime().ToString() + ".zip";
m.SaveToFile(Server.MapPath(filename), true);
}

The code fails on the last line with the error: CS0103: The name 'Server'
does not exist in the current context.

The code above is in Scraper.cs. Here's how I'm calling this in my page

public partial class aspmht_test2 : System.Web.UI.Page
{

public void Page_Load(object sender, EventArgs e)
{
Scraper myScraper = new Scraper();
string myURL = "http://whatever.com";
myScraper.GetPageAndSave(myURL, "whatever");
}
}

Any help out there? I'm outta ideas....Thanks in advance.

-KF



Nov 19 '05 #4
Ah, the missing piece. Thank you, Juan. I will not need to ask a question
like this in the future!

-KF

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl...
Keep this link handy...

The .Net Framework 2.0 Class Browser :
http://beta.asp.net/QUICKSTART/util/classbrowser.aspx

MapPath is a method in System.Web.HttpRequest.

http://beta.asp.net/QUICKSTART/util/...ss=HttpRequest


Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

<ke*****@u.washington.edu> wrote in message
news:eu**************@TK2MSFTNGP12.phx.gbl...
Thanks. In C#, you're saying I need to include an import directive at
the top of the .cs file? What namespace to I need to be importing (which
namespace manages Server.whatever operations?)

Thanks again.

-KF

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:uz****************@TK2MSFTNGP14.phx.gbl...
You have to include the namespace for the Server in the file for your
helper
classes. The namespace is defined in the file with your page class, but
not
automatically if you create more class files.

<ke*****@u.washington.edu> wrote in message
news:#s**************@TK2MSFTNGP15.phx.gbl...

Hoping someone can help with a simple but puzzling problem. I have some
code
that I want to build as a class method. The code works fine when I
embed
it
in Page_Load. But when I try to generalize the code into the method of
a
class I am trying to build, it gives me strange errors: "CS0103: The
name
'Server' does not exist in the current context". (Server being a call
to
Server.MapPath.)

I'm working in ASP.NET 2.0 and VS.NET 2005.

Here's the code that works just fine:

public partial class aspmht_test1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
aspNetMHT.MHT m = new aspNetMHT.MHT();

string protectedUrl = "http://www.google.com";
m.LoadUrl(protectedUrl);
...
string filename = "new" + DateTime.Now.ToFileTime().ToString() +
".zip";

//save it the filesystem.
m.SaveToFile(Server.MapPath(filename), true); // This is where I get
errors if I move to a class method
}
}

So far, so good. When I try to build a 'Scraper' class, and embed the
code
in a method, I get errors on the line notated above. Here's my class
code:

public class Scraper
{
public void GetPageAndSave(string remoteurl, string savefilename)

{

aspNetMHT.MHT m = new aspNetMHT.MHT();

m.LoadUrl(remoteurl);
m.Parse();
string filename = savefilename +
DateTime.Now.ToFileTime().ToString() + ".zip";
m.SaveToFile(Server.MapPath(filename), true);
}

The code fails on the last line with the error: CS0103: The name
'Server'
does not exist in the current context.

The code above is in Scraper.cs. Here's how I'm calling this in my page

public partial class aspmht_test2 : System.Web.UI.Page
{

public void Page_Load(object sender, EventArgs e)
{
Scraper myScraper = new Scraper();
string myURL = "http://whatever.com";
myScraper.GetPageAndSave(myURL, "whatever");
}
}

Any help out there? I'm outta ideas....Thanks in advance.

-KF




Nov 19 '05 #5
You may also consider in your method signature passing along the context:

public void YourMethodCall(System.Web.HttpContext context,string path)
{
context.Server.MapPath(path);
}
--
Clint Hill MCAD
H3O Software
http://www.h3osoftware.com
"ke*****@u.washington.edu" wrote:
Ah, the missing piece. Thank you, Juan. I will not need to ask a question
like this in the future!

-KF

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl...
Keep this link handy...

The .Net Framework 2.0 Class Browser :
http://beta.asp.net/QUICKSTART/util/classbrowser.aspx

MapPath is a method in System.Web.HttpRequest.

http://beta.asp.net/QUICKSTART/util/...ss=HttpRequest


Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

<ke*****@u.washington.edu> wrote in message
news:eu**************@TK2MSFTNGP12.phx.gbl...
Thanks. In C#, you're saying I need to include an import directive at
the top of the .cs file? What namespace to I need to be importing (which
namespace manages Server.whatever operations?)

Thanks again.

-KF

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:uz****************@TK2MSFTNGP14.phx.gbl...
You have to include the namespace for the Server in the file for your
helper
classes. The namespace is defined in the file with your page class, but
not
automatically if you create more class files.

<ke*****@u.washington.edu> wrote in message
news:#s**************@TK2MSFTNGP15.phx.gbl...
>
> Hoping someone can help with a simple but puzzling problem. I have some
code
> that I want to build as a class method. The code works fine when I
> embed
it
> in Page_Load. But when I try to generalize the code into the method of
> a
> class I am trying to build, it gives me strange errors: "CS0103: The
> name
> 'Server' does not exist in the current context". (Server being a call
> to
> Server.MapPath.)
>
> I'm working in ASP.NET 2.0 and VS.NET 2005.
>
> Here's the code that works just fine:
>
> public partial class aspmht_test1 : System.Web.UI.Page
> {
> private void Page_Load(object sender, System.EventArgs e)
> {
> aspNetMHT.MHT m = new aspNetMHT.MHT();
>
> string protectedUrl = "http://www.google.com";
> m.LoadUrl(protectedUrl);
> ...
> string filename = "new" + DateTime.Now.ToFileTime().ToString() +
> ".zip";
>
> //save it the filesystem.
> m.SaveToFile(Server.MapPath(filename), true); // This is where I get
> errors if I move to a class method
> }
> }
>
> So far, so good. When I try to build a 'Scraper' class, and embed the
> code
> in a method, I get errors on the line notated above. Here's my class
code:
>
> public class Scraper
> {
> public void GetPageAndSave(string remoteurl, string savefilename)
>
> {
>
> aspNetMHT.MHT m = new aspNetMHT.MHT();
>
> m.LoadUrl(remoteurl);
> m.Parse();
> string filename = savefilename +
> DateTime.Now.ToFileTime().ToString() + ".zip";
> m.SaveToFile(Server.MapPath(filename), true);
> }
>
> The code fails on the last line with the error: CS0103: The name
> 'Server'
> does not exist in the current context.
>
> The code above is in Scraper.cs. Here's how I'm calling this in my page
>
> public partial class aspmht_test2 : System.Web.UI.Page
> {
>
> public void Page_Load(object sender, EventArgs e)
> {
> Scraper myScraper = new Scraper();
> string myURL = "http://whatever.com";
> myScraper.GetPageAndSave(myURL, "whatever");
> }
> }
>
> Any help out there? I'm outta ideas....Thanks in advance.
>
> -KF
>
>
>



Nov 19 '05 #6

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

Similar topics

0
by: Tom Dacon | last post by:
"Open .Net Command Window Here" context menu for Windows Explorer: The reg file described below adds a new menu item to Windows Explorer's context menu when you right-click over a folder (or the...
8
by: H. S. | last post by:
I am getting this error if I try to compile the file demarcated below. What I am missing here? I am using g++ (GCC) 3.3.5 (Debian 1:3.3.5-8). {tp2}> g++ -ansi -g -Wall tp2.cc -o tp2 tp2.cc: In...
12
by: Susan Baker | last post by:
Hi, I want to store data in a 3-tuple {a,b,c}. Element a is an enumeration, and element c is an enumeration which is specific (i.e. determined) by a. An example will help clarify further. I...
8
by: Simone Chiaretta | last post by:
I've a very strange behaveour related to a website we built: from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx...
1
by: Roy | last post by:
From the MS site: NAME: ClientScriptManager.GetCallbackEventReference (Control, String, String, String) DESCRIPTION: Obtains a reference to a client-side function that, when invoked, initiates a...
2
by: Jeff | last post by:
hey asp.net 2.0 (C'#) In the code behind file I have this method: public String AddBR(Object param) { String text = (String) param; return text; }
3
by: Michael | last post by:
Hi, I am getting a strange error. Last night when I left work this was working perfectly. This morning when I try to run this code in VS2005, it comes up with an error saying "The name 'UserName'...
0
by: NonNB | last post by:
Hi I have a user control which the user can "drag" across the screen (I'm not using "real" .NET drag drop - instead a single click flavour, where the control is drawn at the mouse position). ...
3
dlite922
by: dlite922 | last post by:
Hey guys, My brains asleep and I don't know what's wrong with my session class. I'm over riding session with sesstion_set_save_handler() in a class; When in my member functions (open,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...

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.