Wednesday, September 10, 2008

Compile Error in ASP.Net (error CS0103: The name 'X' does not exist in the current context)

Note: I would have liked to title this post "Microsoft Voodoo make Hulk MAD!!" but for reasons outlined in the rant below, I went with the title above.

The Problem

In an ASP.Net web project you may see one of these two compile error messages referring to a control in a page:

In the Visual Studio 2005 Output window:

"error CS0103: The name 'X' does not exist in the current context"
"error CS0117: 'XPage' does not contain a definition for 'X'"

In the VS.Net 2005 Compile Error List

"The name 'X' does not exist in the current context."

Where 'X' is your control variable name (like TextBox1). If you're seeing these, then you may have the same problem I did.

My problem more specifically was that I was copying files from one (working) VS.Net solution which contained one sort of web project, to another stand-alone project. I needed the new project to have a .CSProj file so our build guys could compile the site with whatever they're using. As soon as I copied the first non-static content into the new Project, the errors above started.

My Solution

The technical root cause of the problem was that there was no variable referring to the control(s) in the page object model.

More details

When I added a new page from scratch, a WebForm1.aspx.designer.CS file appeared, with the following code in it:

protected global::System.Web.UI.WebControls.TextBox TextBox1;

Hmmm . . . why did the other page not have a designer.cs file? In fact the original project I was copying from has no Designer.cs files at all.

I went back to the other (working) project and right clicked on the object referece to my control. In response to "Go to Definition" I got a message box stating:

"The definition of the object is hidden."


Hidden where exactly? Visual Studio's little "Show All Files" option is greyed out. A look at the file system revealed no mystery files. This was starting to sound familiar from my long ago reading about new partial class features.
After Googling "The definition of the object is hidden" I had my answer (see references for details, specifically [1]).

It turns out that I'd stumbled into a gap between "Web Site Project" and "Web Application Project"[2]. I was attempting to manually copy files from a "Web Site" into a "Web Application". In a "Web Site Project", the compiler does some code generation for you, and hides it away in a temporary assembly stored in the location specified by HTTPRuntime.CodeGenDir.

"Web Application Projects" were rolled into VS.Net 2005 with Service Pack 1.

So anyway, throwing the textBox variable declaration into the code-behind file allowed a successful compile with a cut down version of the page. That was definitely it.

Now that I knew what I was looking for, I searched on "Converting a Web Site Project to a Web Application Project". Sure enough, that turned up [4] Walkthrough: Converting a Web Site Project to a Web Application Project in Visual Studio 2005

Great . . . not only does this guide contain an exact description of the specifics of the problem, but it's a detailed guide to the solution. I still need to read a bit more of this tomorrow, but this has certainly cleared the biggest roadblock. To rub salt in the wounds, if you do the file copy from IDE window to IDE window it all happens magically, I never would have seen this problem at all:

"Visual Studio includes an option to convert pages and classes within Web application projects to use partial class declarations. Partial classes are used to separate designer-generated code from code-behind code. These designer-generated classes are stored in a separate file from the code-behind file. This conversion process causes Visual Studio 2005 to recursively examine every page, user-control, and master-page in the project, and to automatically generate a .designer.cs file for each. Visual Studio also changes the .aspx or .ascx files to use the codeBehind attribute instead of the codeFile attribute."

<BonusRant read="optional">
I get incredibly annoyed with some of these 'easy' problems. I want to be able to just look up the error message in a search engine, but it seems I always run across the same pattern of search results:

1. Often the top couple of hits are totally irrelevant sites that happened to be experiencing the same error at the time that the crawler bot visited.

2. The next hit or two are even more annoying. Typically, it's somebody who has innocently(naively) cut and pasted their error message into some forum site or support board. Their post is at the top, then there follow the useless trolls and know-it-alls who bitch about posting off topic or demand more irrelevant data. Then the whole thing either trails off, or the person solves their own problem, but doesn't post the solution. Better forums at least they put a little mark on the solution (if one has emerged).
3. After the first page or so of search results, you just get forum sites that scrape other forum sites, or tons of cross posts of the original problem to ghost-town forums.

So anyway, the moral of this story is: here's my contribution back to the internet: The error message and a solution in one troll-free page. ;-) I've seen quite a number of search hits for other technical posts here, so hopefully this helps somebody.



[1]The Definition of the Object is Hidden

[2]Visual Studio.Net 2005 Web Application Projects

[3]ASP.Net 2.0 Hidden Files and Assemblies

[4]Walkthrough: Converting a Web Site Project to a Web Application Project in Visual Studio 2005


  1. Rob,

    Thanks very much for posting this, I had spent 2 days doing the same as you, copying in an existing page, it not compiling, and then having to create new page and pasting in the content. I did not know why I was doing it though, however, I do now!

    Why do Microsoft always have to make things so complicated?

    After spending a couple of hours scanning the web, I am still not sure which is better, a Web Site Project or a Web Application Project. As it seems to be more recent, I will carry on converting to WAP.


    Æthelwulf of Dorset

  2. Ugh... very painful indeed. Thanks for the post as it helped me as well.

    I actually took the steps further --
    * moved all 'protected ...' stuff from the .designer file
    * deleted the .designer file

    Thanks for the post!

  3. You saved my sanity. This also happens in Mono ... I've spent (wasted) two days of my life looking for an explanation.

    Thanks a million.