65

So, I'm working on this project between my laptop and my desktop.

The project works on the laptop, but now having copied the updated source code onto the desktop, I have over 500 errors in the project, all of them are...

The name does not exist in the current context

Here's one example...

Jobs.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Members/Members.master" AutoEventWireup="true" CodeFile="Jobs.aspx.cs" Inherits="Members_Jobs" %>

<%@ Register Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" TagPrefix="aj" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:UpdatePanel runat="server" ID="upJobs">
        <ContentTemplate>
            <!-- page content goes here -->
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

Jobs.aspx.cs

public partial class Members_Jobs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadJobs();
            gvItems.Visible = false;
            loadComplexes();
            loadBusinesses();
            loadSubcontractors();
            loadInsurers();

            pnlCallback.Visible = false;
            pnlInsurer.Visible = false;
        }
    }

    // more goes down here
}

Here's a sample of the designer.cs file...

namespace stman.Members {


    public partial class Jobs {

        /// <summary>
        /// upJobs control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.UpdatePanel upJobs;
    }
}

I know this error means that the control being referenced generally doesn't exist or is not a part of the class that's referencing it, but as far as I can see, that isn't the case here.

Can anyone provide some insight?

VS2012 Screenshot

Ortund
  • 8,095
  • 18
  • 71
  • 139
  • Do you see the instance variables in the designer.cs file? – Serge Sep 26 '13 at 11:32
  • Point in fact, I don't... just definitions for all the controls on the page – Ortund Sep 26 '13 at 11:34
  • 1
    You will have a Designer.cs file. Delete it and regenerate. – Bharadwaj Sep 26 '13 at 11:34
  • `public partial class Members_Jobs` and `public partial class Jobs` do not match – Squirrel5853 Sep 26 '13 at 11:36
  • @SecretSquirrel So which one should it be? I assume the designer files need to have the `Members_` on their class definitions – Ortund Sep 26 '13 at 11:40
  • a `partial class` is exactly that. If you want your two files the `designer` and the `codebehind` to link up they have to be part of the same `class`. – Squirrel5853 Sep 26 '13 at 11:56
  • @Ortund if you are still having issues with this I would do as Bharadwaj suggested and delete the designer file. You can then right-click on the page, in the solution explorer, and there is an option, something like "Convert to Web Application", which will regenerate your designer file. – Squirrel5853 Sep 27 '13 at 10:33
  • I encountered the exact same situation. I’m not sure what finally resolved it, but I changed the targetFramework attribute of the httpRuntime element in the Web.config to the same version as that being targeted by the project. Cleaned, built and re-built the project multiple times and restarted VS several times in between. Finally the errors vanished. – Dennis T --Reinstate Monica-- Jun 28 '18 at 23:54

16 Answers16

28

In case someone being a beginner who tried all of the above and still didn't manage to get the project to work. Check your namespace. In an instance where you copy code from one project to another and you forget to change the namespace of the project then it will also give you this error.

Hope it helps someone.

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
TheDanMan
  • 1,746
  • 1
  • 17
  • 22
16

Jobs.aspx

This is the phyiscal file -> CodeFile="Jobs.aspx.cs"

This is the class which handles the events of the page -> Inherits="Members_Jobs"

Jobs.aspx.cs

This is the partial class which manages the page events -> public partial class Members_Jobs : System.Web.UI.Page

The other part of the partial class should be -> public partial class Members_Jobs this is usually the designer file.

you dont need to have partial classes and could declare your controls all in 1 class and not have a designer file.

EDIT 27/09/2013 11:37

if you are still having issues with this I would do as Bharadwaj suggested and delete the designer file. You can then right-click on the page, in the solution explorer, and there is an option, something like "Convert to Web Application", which will regenerate your designer file

Squirrel5853
  • 2,376
  • 1
  • 18
  • 34
  • I understand all that, but the problem persists. Please see my updated question with a screenshot of of the codefile and the designer side-by-side – Ortund Sep 27 '13 at 08:32
  • Thanks Squirrel! I was unsure about how to regenerate the designer files... Can you explain why this should be necessary? I mean... What happened to the project? – Ortund Sep 27 '13 at 10:55
  • @Ortund to be honest I do not know... if everything is referenced properly and all the controls exist, etc it should work... – Squirrel5853 Sep 27 '13 at 11:51
  • 1
    would be nice if you could have saved the old designer file and then created the new one to see the difference :) – Squirrel5853 Sep 27 '13 at 11:52
  • Do I have to this for each individual page? What if I add a page later, I have to do that to all the new ones too? The new page I created is the same as the old one :/ – Si8 May 23 '14 at 12:47
  • This helped me; for myself I just had to change the "inherits" attribute to the class I wanted – information_interchange Dec 08 '17 at 23:38
  • secret squirrel? int op in the forces? – CrackSmoker9000 Dec 27 '17 at 09:18
  • The "convert to web application" tip did the trick for me. I deleted the designer file first – deebs Jan 26 '21 at 17:15
7

"The project works on the laptop, but now having copied the updated source code onto the desktop ..."

I did something similar, creating two versions of a project and copying files between them. It gave me the same error.

My solution was to go into the project file, where I discovered that what had looked like this:

<Compile Include="App_Code\Common\Pair.cs" />
<Compile Include="App_Code\Common\QueryCommand.cs" />

Now looked like this:

<Content Include="App_Code\Common\Pair.cs">
  <SubType>Code</SubType>
</Content>
<Content Include="App_Code\Common\QueryCommand.cs">
  <SubType>Code</SubType>
</Content>

When I changed them back, Visual Studio was happy again.

TomDestry
  • 3,349
  • 5
  • 31
  • 40
  • 1
    YES! Only, what you should've mentioned, is if you click on the class files in the Solution Explorer and get their properties, you can simply change "Build Action" to "Compile" and it will do the same thing. This was awesome and helped me when I was creating an App_Code folder and dumping in my code, and couldn't figure out why it wasn't picking up the classes. – vapcguy Jun 02 '16 at 22:11
7

From the MSDN website:

This error frequently occurs if you declare a variable in a loop or a try or if block and then attempt to access it from an enclosing code block or a separate code block.

So declare the variable outside the block.

cars4chimps
  • 71
  • 1
  • 1
5

I had this problem in my main project when I referenced a dll file.

The problem was that the main project that referenced the dll was targeting a lower framework version than that of the dll.

So I upped my target framework version (Right-click project -> Application -> Target framework) and the error disappeared.

tno2007
  • 1,993
  • 25
  • 16
3

I came across a similar problem with a meta tag. In the designer.cs, the control was defined as:

protected global::System.Web.UI.HtmlControl.HtmlGenericControl metatag;

I had to move the definition to the .aspx.cs file and define as:

protected global::System.Web.UI.HtmlControl.HtmlMeta metatag;
tshepang
  • 12,111
  • 21
  • 91
  • 136
1

I also faced a similar issue, the problem was the form was inside a folder and the file .aspx.designer.cs I had the namespace referencing specifically to that directory; which caused the error to appear in several components:

El nombre no existe en el contexto actual

This in your case, a possible solution is to leave the namespace line of the Members_Jobs.aspx.designer.cs file specified globally, ie change this

namespace stman.Members {

For this

namespace stman {

It's what helped me solve the problem.

I hope to be helpful

rpalaciosg
  • 11
  • 4
0

I solved mine by using an Import directive under my Page directive. You may also want to add the namespace to your Inherits attribute of your Page directive.

Since it appears that your default namespace is "stman.Members", try: <%@ Page Title="" Language="C#" MasterPageFile="~/Members/Members.master" AutoEventWireup="true" CodeFile="Jobs.aspx.cs" Inherits="stman.Members.Members_Jobs" %> <%@ Import Namespace="stman.Members"%>

Additionally, data that I wanted to pass between aspx.cs and aspx, I put inside a static class inside the namespace. That static class was available to move data around in the name space and no longer has a "not in context" error.

Jroonk
  • 1,376
  • 1
  • 9
  • 12
0

I also faced a similar issue. The reason was that I had the changes done in the .aspx page but not the designer page and hence I got the mentioned error. When the reference was created in the designer page I was able to build the solution.

Aarthy
  • 1
  • 1
0

Same issue happened with me when i mistakenly added some text in front of the top directive of the master page all the controls across all the page included in my project started showing, does not exist in the current context. After spending much time across the forum and self analysis, i found the mistakenly added text and removed but it does not solved the problem. Then i had to click on each error to open the associated page so that VS can load the page and recognize it, once the code was loaded in VS, error from those particular page started disappearing because VS recognized it. clicking on each error, loading each page in VS editor did the trick for me and you have to do it only once because later it will be automatically recognized by VS. I would say it is a Visual Studio Glitch.

0

In our case, beside changing ToolsVersion from 14.0 to 15.0 on .csproj projet file, as stated by Dominik Litschauer, we also had to install an updated version of MSBuild, since compilation is being triggered by a Jenkins job. After installing Build Tools for Visual Studio 2019, we had got MsBuild version 16.0 and all new C# features compiled ok.

Julio Nobre
  • 4,196
  • 3
  • 46
  • 49
0

I had this come up and found that a couple files used for debugging shared the same CodeFile and Inherits values. Changing those to be unique resolved my issue.

PutoTropical
  • 113
  • 1
  • 9
0

I had the same issue, it solved when I added ".forms" at the end of the namespace in myform.aspx.cs . Like this:
namespace YourProgramName.forms

0

The very first step in these situations is to try build -> clean solution

Sometimes, VS will confuse itself from its own project outputs.

Clean solution removes the code that VS has previously built.

It doesn't remove the bin/obj directories themselves, rather the output files in these folders.

Dean P
  • 1,841
  • 23
  • 23
0

In my case, somehow the *.csproj file of my main project lost the reference to a secondary project it depends on, it probably happened during the import phase in Visual Studio 2022 (from VS 2017). I checked the backup copy of the *.csproj file and there was an entire tag missing. I manually copied it over from the backup, and the error disappeared.

  <ItemGroup>
    <ProjectReference Include="..\projectName\projectName.csproj">
        <Project>{cf9ab6e2-8e15-4915-820d-2d0d8f365cbf}</Project>
        <Name>ProjectName</Name>
    </ProjectReference>     
  </ItemGroup>
rekotc
  • 595
  • 1
  • 10
  • 21
0

I also face the same problem and project showed scores of errors. I copy some dll's which are creating problem from my colleague and copy them in another folder then delete those dll files from my project and add their references again from new location and it solved the problem.