Ortund
Ortund

Reputation: 8255

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

Upvotes: 70

Views: 572816

Answers (17)

Phoeniceus Agelaius
Phoeniceus Agelaius

Reputation: 531

Just in case anyone else is as unobservant as I am: I got this error when I put a line of code inside a new class but not inside any methods (there were no methods yet and I wasn't paying attention).

Upvotes: 0

TheDanMan
TheDanMan

Reputation: 1836

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.

Upvotes: 28

Waseem Khan
Waseem Khan

Reputation: 1

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.

Upvotes: 0

rekotc
rekotc

Reputation: 555

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>

Upvotes: 0

Dean P
Dean P

Reputation: 2225

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.

Upvotes: 1

Farzane Keshtkar
Farzane Keshtkar

Reputation: 11

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

Upvotes: 0

PutoTropical
PutoTropical

Reputation: 123

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.

Upvotes: 0

Julio Nobre
Julio Nobre

Reputation: 4405

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.

Upvotes: 0

Krishn Pal Chauhan
Krishn Pal Chauhan

Reputation: 1

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.

Upvotes: 0

rpalaciosg
rpalaciosg

Reputation: 11

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

Upvotes: 1

Aarthy
Aarthy

Reputation: 1

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.

Upvotes: 0

tno2007
tno2007

Reputation: 2168

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.

Upvotes: 5

Jroonk
Jroonk

Reputation: 1481

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.

Upvotes: 0

cars4chimps
cars4chimps

Reputation: 71

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.

Upvotes: 7

TomDestry
TomDestry

Reputation: 3409

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

Upvotes: 7

user2776839
user2776839

Reputation: 41

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;

Upvotes: 3

Squirrel5853
Squirrel5853

Reputation: 2406

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

Upvotes: 18

Related Questions