[c#] Name does not exist in the current context

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

This question is related to c# asp.net

The answer is


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.


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.


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


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.


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.


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.


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;

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.


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.


"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.