Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type or namespace name 'X' does not exist in the namespace 'Y' - in VS generated code

Tags:

c#

asp.net-mvc

This is the weirdest error I have ever encountered.

This MVC web project was working fine until today. It hasn't been worked on by anyone in a couple of weeks.

Even though NOTHING has changed, simply running it now results in:

The type or namespace name 'ColourboxViewModel' does not exist in the namespace 'CMSModels.ViewModels'

(x5)

The type or namespace name 'Colourbox' does not exist in the namespace 'CMSModels' (are you missing an assembly reference?)

(x1)

There is a reference to CMSModels in the project. Removing this reference gives me about 200 extra errors for each other class in the namespace, so I know it is working, but not for these two errors.

This is the line of generated code which the error points to:

public class views_colorbox_index_aspx : System.Web.Mvc.ViewPage<IEnumerable<CMSModels.Colourbox>>, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler {
...
public class views_colorbox_content_ascx : System.Web.Mvc.ViewUserControl<CMSModels.ViewModels.ColourboxViewModel> {

Intellisense is picking up the location of both CMSModels.ColourBox and CMSModels.ViewModels.ColourboxViewModel.

I am completely stumped. Any help greatly appreciated as I am completely at a loss.


OK I have found the issue - but not the solution.

The class is defined as

public class ColourBoxViewModel

yet the generated code is looking for a class of 'ColourboxViewModel'. Note the lowercase B. Where does the auto-gen code get the names of classes from? Search in project reveals no mentions of it with a lowercase b.

The issue is the same for the ColourBox class.


Problem solved - and @František Žiačik you were correct. There was a folder which had been excluded from the project (which is why searching in entire project and within the views in visual studio found nothing) which contained the incorrectly typed inherit statement. The excluded folder was called ColorBox when originally created by outsourcers. This had been copied and renamed as ColourBox by another dev on the team who decided to, and I quote, 'spell everything correctly'. Said dev is now only allowed to code using a Speak n' Spell.

like image 596
Rory McCrossan Avatar asked Apr 05 '11 13:04

Rory McCrossan


People also ask

Why am I getting error CS0246 the type or namespace name could not be found?

This often occurs because the casing used in the name of the type is not correct. For example, Dataset ds; generates CS0246 because the s in Dataset must be capitalized. If the error is for a type name, did you include the proper using directive, or, alternatively, fully qualify the name of the type?

How do I fix error cs0234?

To fix this error, simply update the example . csproj file to use a previous System. CommandLine version. Note that this package is only used to parse the options for the example.


2 Answers

So here is my guess.

In your content.ascx view (or whatever it's named) you have declared:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CMSModels.ViewModels.ColourboxViewModel>" %>

instead of

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CMSModels.ViewModels.ColourBoxViewModel>" %>
like image 87
František Žiačik Avatar answered Sep 29 '22 02:09

František Žiačik


I had this error yesterday, and I found these answers, but in my case they didn't help. Instead it turned out that in my client in project properties the Target framework was set to .NET Framework 4 Client Profile. Solution was to change this to full .NET 4. Hope this can help someone, because the error in VS is not very helpfull.

like image 30
JerryA Avatar answered Sep 29 '22 02:09

JerryA