Rectangle 27 108

I know this is an old question, but I had a similar problem and wanted to post my solution in case it could benefit someone else. I encountered the problem while learning to use:

I was trying to create an AJAX-enabled page (look into a tutorial about using the ScriptManager object if you aren't familiar with this). I tried to access the HTML elements in the page via the C# code, and I was getting an error stating the the identifier for the HTML ID value "does not exist in the current context."

To access the HTML element as a variable in the C# code, the following value must be placed in the HTML element tag in the aspx file:

runat="server"

Some objects in the Toolbox in the Visual Studio IDE do not automatically include this value when added to the page.

2. Regenerate the auto-generated C# file:

  • In the Solution Explorer, under the aspx file there should be two files: *.aspx.cs and *.aspx.designer.cs. The designer file is auto-generated.
  • Delete the existing *.aspx.designer.cs file. Make sure you only delete the designer file. Do not delete the other one, because it contains your C# code for the page.
  • Right-click on the parent aspx file. In the pop-up menu, select Convert to Web Application.

Now the element should be accessible in the C# code file.

fyi the right click "Convert to Web Application" option is also available on folders and projects.

option number #2 was the perfect answer for me in solving this issue when I was running into it today!

Don't know why this works, but it worked for me.

FYI in Visual Studio 2013, the "Convert To Web Application" option has moved to the Project menu: stackoverflow.com/questions/19561982/

c# - The name 'controlname' does not exist in the current context - St...

c# asp.net visual-studio-2005 code-behind
Rectangle 27 108

I know this is an old question, but I had a similar problem and wanted to post my solution in case it could benefit someone else. I encountered the problem while learning to use:

I was trying to create an AJAX-enabled page (look into a tutorial about using the ScriptManager object if you aren't familiar with this). I tried to access the HTML elements in the page via the C# code, and I was getting an error stating the the identifier for the HTML ID value "does not exist in the current context."

To access the HTML element as a variable in the C# code, the following value must be placed in the HTML element tag in the aspx file:

runat="server"

Some objects in the Toolbox in the Visual Studio IDE do not automatically include this value when added to the page.

2. Regenerate the auto-generated C# file:

  • In the Solution Explorer, under the aspx file there should be two files: *.aspx.cs and *.aspx.designer.cs. The designer file is auto-generated.
  • Delete the existing *.aspx.designer.cs file. Make sure you only delete the designer file. Do not delete the other one, because it contains your C# code for the page.
  • Right-click on the parent aspx file. In the pop-up menu, select Convert to Web Application.

Now the element should be accessible in the C# code file.

fyi the right click "Convert to Web Application" option is also available on folders and projects.

option number #2 was the perfect answer for me in solving this issue when I was running into it today!

Don't know why this works, but it worked for me.

FYI in Visual Studio 2013, the "Convert To Web Application" option has moved to the Project menu: stackoverflow.com/questions/19561982/

c# - The name 'controlname' does not exist in the current context - St...

c# asp.net visual-studio-2005 code-behind
Rectangle 27 108

I know this is an old question, but I had a similar problem and wanted to post my solution in case it could benefit someone else. I encountered the problem while learning to use:

I was trying to create an AJAX-enabled page (look into a tutorial about using the ScriptManager object if you aren't familiar with this). I tried to access the HTML elements in the page via the C# code, and I was getting an error stating the the identifier for the HTML ID value "does not exist in the current context."

To access the HTML element as a variable in the C# code, the following value must be placed in the HTML element tag in the aspx file:

runat="server"

Some objects in the Toolbox in the Visual Studio IDE do not automatically include this value when added to the page.

2. Regenerate the auto-generated C# file:

  • In the Solution Explorer, under the aspx file there should be two files: *.aspx.cs and *.aspx.designer.cs. The designer file is auto-generated.
  • Delete the existing *.aspx.designer.cs file. Make sure you only delete the designer file. Do not delete the other one, because it contains your C# code for the page.
  • Right-click on the parent aspx file. In the pop-up menu, select Convert to Web Application.

Now the element should be accessible in the C# code file.

fyi the right click "Convert to Web Application" option is also available on folders and projects.

option number #2 was the perfect answer for me in solving this issue when I was running into it today!

Don't know why this works, but it worked for me.

FYI in Visual Studio 2013, the "Convert To Web Application" option has moved to the Project menu: stackoverflow.com/questions/19561982/

c# - The name 'controlname' does not exist in the current context - St...

c# asp.net visual-studio-2005 code-behind
Rectangle 27 108

I know this is an old question, but I had a similar problem and wanted to post my solution in case it could benefit someone else. I encountered the problem while learning to use:

I was trying to create an AJAX-enabled page (look into a tutorial about using the ScriptManager object if you aren't familiar with this). I tried to access the HTML elements in the page via the C# code, and I was getting an error stating the the identifier for the HTML ID value "does not exist in the current context."

To access the HTML element as a variable in the C# code, the following value must be placed in the HTML element tag in the aspx file:

runat="server"

Some objects in the Toolbox in the Visual Studio IDE do not automatically include this value when added to the page.

2. Regenerate the auto-generated C# file:

  • In the Solution Explorer, under the aspx file there should be two files: *.aspx.cs and *.aspx.designer.cs. The designer file is auto-generated.
  • Delete the existing *.aspx.designer.cs file. Make sure you only delete the designer file. Do not delete the other one, because it contains your C# code for the page.
  • Right-click on the parent aspx file. In the pop-up menu, select Convert to Web Application.

Now the element should be accessible in the C# code file.

fyi the right click "Convert to Web Application" option is also available on folders and projects.

option number #2 was the perfect answer for me in solving this issue when I was running into it today!

Don't know why this works, but it worked for me.

FYI in Visual Studio 2013, the "Convert To Web Application" option has moved to the Project menu: stackoverflow.com/questions/19561982/

c# - The name 'controlname' does not exist in the current context - St...

c# asp.net visual-studio-2005 code-behind
Rectangle 27 59

I found out that if I choose Build Only instead of Build + IntelliSense the errors (that are related to intellisense) will go away.

The reason that this is happening is that for codes like this:

<% if (true) { %>
    <%=1%>
<% } %>
<%=2%>

In order to provide intellisense in <%= %> blocks at design time, ASP.NET generates assignment to a temporary __o variable and language (VB or C#) then provide the intellisense for the variable. That is done when page compiler sees the first <%= ... %> block. But here, the block is inside the if, so after the if closes, the variable goes out of scope. We end up generating something like this:

if (true) { 
    object @__o;
    @__o = 1;
}
@__o = 2;

The workaround is to add a dummy expression early in the page. E.g. <%="" %>. This will not render anything, and it will make sure that __o is declared top level in the Render method, before any potential if (or other scoping) statement.

Update 2: Getting rid of this error without loosing other intelliSense errors

Click on the filter button on the top left corner of the error list panel and uncheck the CS0103 which the error code for the: The name '__o' does not exist in the current context and these errors will not be shown anymore and you can still have other intelliSense errors and warnings:

Having the same problem here. I could hide IntelliSense errors, but that seems like brushing it under the rug. Want to trace the problem.

This is not the fix, like Dan said, that it is just hiding that problem. Plus it would hide all IntelliSense errors, not just that one..

c# - The name '__o' does not exist in the current context - Stack Over...

c# asp.net visual-studio visual-studio-2015 compiler-errors
Rectangle 27 29

I ran into this same issue when I created an new area to organize my pages. My structure looks kind of like:

The views created in the Views folder under the WebProject worked fine, but the views created under the NewArea threw the following error: "The name 'model' does not exist in current context." To fix this I copied the web.config in the Views folder under the WebProject to the Views folder in the NewArea. See below.

I ran into this because I manually created this new area using Add -> New Folder to add the folders. I should of right clicked the project and selected Add -> Area. Then Visual Studio would have taken care of setting the area up correctly.

I realize this is old, but I actually had the same problem today, so I wanted to say thanks for this answer. :)

I had the same problem too, and the similar solution. My Views/Web.config was referencing System.Web.Mvc.MvcWebRazorHostFactory version 5.0.0.0, and I had a third-party Area referencing v4.0 and with the views cooperating. I set Views/Web.config back to v4.0 and everything worked.

it was specifically your comment about overwriting the web.config with the one from the Views folder that helped me fix this. simply making the contents exactly the same doesn't work for some insane reason. AAARRRGGGHHH! ;)

In my case, I use robocopy to synchronize local version with server version, excluded all the web.config files. without web.config file in server version, this error will appear.

asp.net mvc 3 - The name 'model' does not exist in current context in ...

asp.net-mvc-3 razor
Rectangle 27 28

I ran into this same issue when I created an new area to organize my pages. My structure looks kind of like:

The views created in the Views folder under the WebProject worked fine, but the views created under the NewArea threw the following error: "The name 'model' does not exist in current context." To fix this I copied the web.config in the Views folder under the WebProject to the Views folder in the NewArea. See below.

I ran into this because I manually created this new area using Add -> New Folder to add the folders. I should of right clicked the project and selected Add -> Area. Then Visual Studio would have taken care of setting the area up correctly.

I realize this is old, but I actually had the same problem today, so I wanted to say thanks for this answer. :)

I had the same problem too, and the similar solution. My Views/Web.config was referencing System.Web.Mvc.MvcWebRazorHostFactory version 5.0.0.0, and I had a third-party Area referencing v4.0 and with the views cooperating. I set Views/Web.config back to v4.0 and everything worked.

it was specifically your comment about overwriting the web.config with the one from the Views folder that helped me fix this. simply making the contents exactly the same doesn't work for some insane reason. AAARRRGGGHHH! ;)

In my case, I use robocopy to synchronize local version with server version, excluded all the web.config files. without web.config file in server version, this error will appear.

asp.net mvc 3 - The name 'model' does not exist in current context in ...

asp.net-mvc-3 razor
Rectangle 27 9

configurationmanager does not exist in the current context

To a read connection string from web.config we need to use ConfigurationManager class and its method. If you want to use you need to add namespace using System.Configuration;

Though you used this namespace, when you try to use the ConfigurationManager class then the system shows an error configurationmanager does not exist in the current context. To solve this Problem:

ConfigurationManager.ConnectionStrings["ConnectionSql"].ConnectionString;

c# - The name 'ConfigurationManager' does not exist in the current con...

c# .net visual-studio visual-studio-2008
Rectangle 27 9

configurationmanager does not exist in the current context

To read connection string from web.config we need to use configurationmanager class and its method which root class is System.Configuration; If you want to use you need to add namespace using System.Configuration;

To a read connection string from web.config we need to use ConfigurationManager class and its method. If you want to use you need to add namespace using System.Configuration;

Though you used this namespace, when you try to use the ConfigurationManager class then the system shows an error configurationmanager does not exist in the current context. To solve this Problem:

ConfigurationManager.ConnectionStrings["ConnectionSql"].ConnectionString;

c# - The name 'ConfigurationManager' does not exist in the current con...

c# .net visual-studio visual-studio-2008
Rectangle 27 9

configurationmanager does not exist in the current context

To a read connection string from web.config we need to use ConfigurationManager class and its method. If you want to use you need to add namespace using System.Configuration;

Though you used this namespace, when you try to use the ConfigurationManager class then the system shows an error configurationmanager does not exist in the current context. To solve this Problem:

ConfigurationManager.ConnectionStrings["ConnectionSql"].ConnectionString;

c# - The name 'ConfigurationManager' does not exist in the current con...

c# .net visual-studio visual-studio-2008
Rectangle 27 348

You should not need to query the database directly for the current App...

That introduces a new dependency of having an extra context for starters, but going forward the user database tables change (3 times in the past 2 years) but the API is consistent. For example the users table is now called AspNetUsers in Identity Framework, and the names of several primary key fields kept changing, so the code in several answers will no longer work as-is.

Another problem is that the underlying OWIN access to the database will use a separate context, so changes from separate SQL access can produce invalid results (e.g. not seeing changes made to the database). Again the solution is to work with the supplied API and not try to work-around it.

The correct way to access the current user object in ASP.Net identity (as at this date) is:

var user = UserManager.FindById(User.Identity.GetUserId());

or, if you have an async action, something like:

var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

FindById requires you have the following using statement so that the non-async UserManager methods are available (they are extension methods for UserManager, so if you do not include this you will only see FindByIdAsync):

using Microsoft.AspNet.Identity;

If you are not in a controller at all (e.g. you are using IOC injection), then the user id is retrieved in full from:

System.Web.HttpContext.Current.User.Identity.GetUserId();

If you are not in the standard Account controller you will need to add the following (as an example) to your controller:

/// <summary>
    /// Application DB context
    /// </summary>
    protected ApplicationDbContext ApplicationDbContext { get; set; }

    /// <summary>
    /// User manager - attached to application DB context
    /// </summary>
    protected UserManager<ApplicationUser> UserManager { get; set; }
this.ApplicationDbContext = new ApplicationDbContext();
    this.UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this.ApplicationDbContext));
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());

When using EF and Identity Framework with Azure, over a remote database connection (e.g. local host testing to Azure database), you can randomly hit the dreaded error: 19 - Physical connection is not usable. As the cause is buried away inside Identity Framework, where you cannot add retries (or what appears to be a missing .Include(x->someTable)), you need to implement a custom SqlAzureExecutionStrategy in your project.

@TBA - thanks, I realized later it's an extension method. Need to add Microsoft.AspNet.Identity using . thanks again

Type or namesapce UserStore could not be found.I added using Microsft.AspNet.Indentity

@Zapnologica: That sounds like a new question (suggest you post it). You can extend the ApplicationUser class (application specific) and the AspNetUsers table in parallel and they will provide any new fields. Again: Do not hit the database directly! :)

@LifeH2O: The ApplicationUser returned by FindById is your class, complete with your extra properties. Please try it.

c# - ASP.NET MVC 5 - Identity. How to get current ApplicationUser - St...

c# asp.net-mvc-5 identity asp.net-identity
Rectangle 27 342

You should not need to query the database directly for the current App...

That introduces a new dependency of having an extra context for starters, but going forward the user database tables change (3 times in the past 2 years) but the API is consistent. For example the users table is now called AspNetUsers in Identity Framework, and the names of several primary key fields kept changing, so the code in several answers will no longer work as-is.

Another problem is that the underlying OWIN access to the database will use a separate context, so changes from separate SQL access can produce invalid results (e.g. not seeing changes made to the database). Again the solution is to work with the supplied API and not try to work-around it.

The correct way to access the current user object in ASP.Net identity (as at this date) is:

var user = UserManager.FindById(User.Identity.GetUserId());

or, if you have an async action, something like:

var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

FindById requires you have the following using statement so that the non-async UserManager methods are available (they are extension methods for UserManager, so if you do not include this you will only see FindByIdAsync):

using Microsoft.AspNet.Identity;

If you are not in a controller at all (e.g. you are using IOC injection), then the user id is retrieved in full from:

System.Web.HttpContext.Current.User.Identity.GetUserId();

If you are not in the standard Account controller you will need to add the following (as an example) to your controller:

/// <summary>
    /// Application DB context
    /// </summary>
    protected ApplicationDbContext ApplicationDbContext { get; set; }

    /// <summary>
    /// User manager - attached to application DB context
    /// </summary>
    protected UserManager<ApplicationUser> UserManager { get; set; }
this.ApplicationDbContext = new ApplicationDbContext();
    this.UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this.ApplicationDbContext));
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());

When using EF and Identity Framework with Azure, over a remote database connection (e.g. local host testing to Azure database), you can randomly hit the dreaded error: 19 - Physical connection is not usable. As the cause is buried away inside Identity Framework, where you cannot add retries (or what appears to be a missing .Include(x->someTable)), you need to implement a custom SqlAzureExecutionStrategy in your project.

@TBA - thanks, I realized later it's an extension method. Need to add Microsoft.AspNet.Identity using . thanks again

Type or namesapce UserStore could not be found.I added using Microsft.AspNet.Indentity

@Zapnologica: That sounds like a new question (suggest you post it). You can extend the ApplicationUser class (application specific) and the AspNetUsers table in parallel and they will provide any new fields. Again: Do not hit the database directly! :)

@LifeH2O: The ApplicationUser returned by FindById is your class, complete with your extra properties. Please try it.

c# - ASP.NET MVC 5 - Identity. How to get current ApplicationUser - St...

c# asp.net-mvc-5 identity asp.net-identity
Rectangle 27 1

@Html.HiddenFor(Model => model.Id)

You should replace Model with model.

Troubleshooting this kind of spelling mistake isn't hard. If you have turned on detailed error messages in ASP.NET something like this will be displayed in your browser:

The offending line (line 22) is highlighted in red making it quite obvious where the error is.

If you have a really bad syntax error then the code displayed below the heading Source Error may be hard to understand. In that case you can click on the text Show Complete Compilation Source to see what the compiler is having a hard time to understand and hopefully be able to correlate that back to your Razer view engine source code.

If you see in Modules View (Parent) code, I tried different ways including Model with a capital M. Still didn't work for me though.

asp.net mvc 3 - Rendering Partial View: The name 'model' does not exis...

asp.net-mvc-3 entity-framework entity-framework-5 partial-views strongly-typed-view
Rectangle 27 1

I ran into this same error, except it was a WPF error. I was rearranging projects and had a control defined in like this:

<local:CustomControl Name="Custom" />

In my code behind I tried using Custom.Blah, but I got the error:

The name 'Custom' does not exist in the current context

What did the trick for me was changing my control in Xaml to this:

<local:CustomControl x:Name="Custom" />

Hope this helps someone out there!

c# - The name 'controlname' does not exist in the current context - St...

c# asp.net visual-studio-2005 code-behind
Rectangle 27 1

I ran into this same error, except it was a WPF error. I was rearranging projects and had a control defined in like this:

<local:CustomControl Name="Custom" />

In my code behind I tried using Custom.Blah, but I got the error:

The name 'Custom' does not exist in the current context

What did the trick for me was changing my control in Xaml to this:

<local:CustomControl x:Name="Custom" />

Hope this helps someone out there!

c# - The name 'controlname' does not exist in the current context - St...

c# asp.net visual-studio-2005 code-behind
Rectangle 27 1

I get the same error after i made changes with my data context. But i encounter something i am unfamiliar with. I get used to publish my files manually. Normally when i do that there is no App_Code folder appears in publishing folder. Bu i started to use VS 12 publishing which directly publishes with your assistance to the web server. And then i get the error about being precompiled application. Then i delete app_code folder it worked. But then it gave me the Data Context error that you are getting. So i just deleted all the files and run the publish again with no file restrictions (every folder & file will be published) then it worked like a charm.

c# - The name 'controlname' does not exist in the current context - St...

c# asp.net visual-studio-2005 code-behind
Rectangle 27 1

I ran into this same error, except it was a WPF error. I was rearranging projects and had a control defined in like this:

<local:CustomControl Name="Custom" />

In my code behind I tried using Custom.Blah, but I got the error:

The name 'Custom' does not exist in the current context

What did the trick for me was changing my control in Xaml to this:

<local:CustomControl x:Name="Custom" />

Hope this helps someone out there!

c# - The name 'controlname' does not exist in the current context - St...

c# asp.net visual-studio-2005 code-behind
Rectangle 27 1

I get the same error after i made changes with my data context. But i encounter something i am unfamiliar with. I get used to publish my files manually. Normally when i do that there is no App_Code folder appears in publishing folder. Bu i started to use VS 12 publishing which directly publishes with your assistance to the web server. And then i get the error about being precompiled application. Then i delete app_code folder it worked. But then it gave me the Data Context error that you are getting. So i just deleted all the files and run the publish again with no file restrictions (every folder & file will be published) then it worked like a charm.

c# - The name 'controlname' does not exist in the current context - St...

c# asp.net visual-studio-2005 code-behind
Rectangle 27 1

I get the same error after i made changes with my data context. But i encounter something i am unfamiliar with. I get used to publish my files manually. Normally when i do that there is no App_Code folder appears in publishing folder. Bu i started to use VS 12 publishing which directly publishes with your assistance to the web server. And then i get the error about being precompiled application. Then i delete app_code folder it worked. But then it gave me the Data Context error that you are getting. So i just deleted all the files and run the publish again with no file restrictions (every folder & file will be published) then it worked like a charm.

c# - The name 'controlname' does not exist in the current context - St...

c# asp.net visual-studio-2005 code-behind
Rectangle 27 1

I ran into this same error, except it was a WPF error. I was rearranging projects and had a control defined in like this:

<local:CustomControl Name="Custom" />

In my code behind I tried using Custom.Blah, but I got the error:

The name 'Custom' does not exist in the current context

What did the trick for me was changing my control in Xaml to this:

<local:CustomControl x:Name="Custom" />

Hope this helps someone out there!

c# - The name 'controlname' does not exist in the current context - St...

c# asp.net visual-studio-2005 code-behind