[c#] Type or namespace name does not exist

I have a WCF Data Service project built with Visual Studio 2010, which was working fine. All of a sudden, it didn't compile anymore. It was giving me messages like:

Error 7 The type or namespace name 'Services' does not exist in the namespace 'System.Data' (are you missing an assembly reference?) C:\U...s\Visual Studio 2010\Projects...\DataService.cs ...

Error 8 The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?) DependencyResolver.cs 3 14

Error 10 The type or namespace name 'Web' does not exist in the namespace 'System.ServiceModel' (are you missing an assembly reference?)

Error 12 The type or namespace name 'DataService' could not be found (are you missing a using directive or an assembly reference?)

How can I fix it?

This question is related to c# .net visual-studio-2010 visual-studio

The answer is


I found that this is caused by me having the same namespace name as class name (MyWorld.MyWorld = Namespace.ClassName).

Change your namespace to a name that is not the same name as your class and this will compile.

Source


FOR ANYONE WITH LINKED FILES: I had this problem and was using silverlight and the offending file that was throwing this error was a linked file.

The error from the compiler told me the error was happening in the project that the file lived in. It ended up that the error was NOT in that project, it was occurring in the project that contained the linked file. That project was missing a reference.


I faced the same issue with an ASP.NET MVC site when I tried to use LINQ to SQL. I fixed the problem by:

Solution Explorer -> References -> Right-click on System.Data.Linq -> Copy Local (True)


I have experienced same problem with System.Data.SQLite. The source of the problem is the dll you have used should have same .NET version with your project.

For example if you have used (in my case) SQLite for .NET 4.5, your platform target should be .NET 4.5 too.

You can find platform target by: Project > (project name) Properties > Build.


I had the same problem and tried all of the above without any success, then I found out what it was:

I'd created a folder called "System" in one of my projects and then created a class in it. The problem seems to stem from having a namespace called "System" when the .cs file is created, even if it is in a namespace of "MyProject.System".

Looking back I can understand why this would cause problems. It really stumped me at first as the error messages don't initially seem to relate to the problem.


I have had the same problem, and I had to set the "Target Framework" of all the projects to be the same. Then it built fine. On the Project menu, click ProjectName Properties. Click the compile tab. Click Advanced Compile Options. In the Target Framework, choose your desired framework.


Delete the .refresh.dll file if you are under source control. Then rebuild. It should work. This worked for me


In my case there was no change in projects, it just stopped to compile and with "type or namespace name XXX does not exist" and in the complaining class itself intellisense for that XXX namespace/class works fine. The problem was in references indeed!

Steps to reproduce:

  1. Solution has ProjectA, ProjectB. ProjectA references to third party log4net and it is marked Copy local: true. ProjectB references ProjectA and does not have reference to log4net. Solution compiles fine.

  2. Change in ProjectA: reference property for log4net to Copy local: false.

  3. Clean bin and obj folders.
  4. When you compile, ProjectA compiles but ProjectB complains about not finding ProjectA namespace.

This is because ProjectB bin folder is missing third party library (log4net in my case)!

In this case solution would be -

  1. make sure that third party libraries references are set to Copy local: true or
  2. add path to such libraries in project properties under reference path.

And if all else fails, such as ensuring that the target frameworks are the same, and you are dealing with a WPF class library in VS2010, simply restart Visual Studio. That did it for me.


Check to see that your target framework has the same .NET versions. I had the same problem and my class .NET was 3.5 and web solution had 4.5. I synchronised those, and then it worked :)


I had an issue with System.Linq not being recognized. The using statement had a red squiggly, etc. The way I solved it was to change my website to target dotnet 3.5, then switch back to the original targeted framework (4.0 in my case).


I am referencing Microsoft.CommerceServer.Runtime.Orders and experienced this error. This project is old and has Target framework .NET 2.0. In output I had this error:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3268: The primary reference "Microsoft.CommerceServer.Runtime" could not be resolved because it has an indirect dependency on the framework assembly "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v2.0". To resolve this problem, either remove the reference "Microsoft.CommerceServer.Runtime" or retarget your application to a framework version which contains "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

I simply changed the target framework to .NET 4 and now it builds.


I encountered this problem while using Visual Studio's Git integration to manage the project. For some reason the Windows Phone 8 project would compile just fine when targeting x86, but when I set it to target ARM, it would fail compiling with an error indicating that "Advertising" didn't exist in the Microsoft namespace.

I ended up resolving the issue by removing the Microsoft.Advertising.*.dll reference and adding it again.


I experienced the same errors. After I discovered my project had the wrong Assembly name (I copied files from a different project and the namespaces got a little confused), and changed it back, the project compiled nicely.


I got a very similar error message that was caused by inadvertently having made a duplicate of a class in another project of my solution. Deleting the duplicate fixed the issue


I recently needed to do a System Restore and it caused several of my files to change/disappear that I had been working on since the restore. Some of those were DLL files. I used Source Control to retrieve the entire project but I still had a similar issue as above. I found this answer that described you may need to remove a DLL and readd it to get your errors fixed. This was the case in my scenario.

Removing WebMatrix.WebData and readding it as well as adding in WebMatrix.Data fixed my error of The type or namespace name 'Data' does not exist in the namespace 'WebMatrix' ....


In my case the problem was happening because the class I created had a namespace that interfered with existing classes. The new class A had namespace zz.yy.xx (by mistake). References to objects in another namespace yy.xx were not compiling in class A or other classes whose namespace was zz.

I changed the namespace of class A to yy.xx , which it should have been, and it started working.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to visual-studio-2010

variable is not declared it may be inaccessible due to its protection level SSIS Excel Connection Manager failed to Connect to the Source This project references NuGet package(s) that are missing on this computer Gridview get Checkbox.Checked value error LNK2038: mismatch detected for '_MSC_VER': value '1600' doesn't match value '1700' in CppFile1.obj What is the difference between Visual Studio Express 2013 for Windows and Visual Studio Express 2013 for Windows Desktop? Attach (open) mdf file database with SQL Server Management Studio What is and how to fix System.TypeInitializationException error? Could not load file or assembly "Oracle.DataAccess" or one of its dependencies IIS error, Unable to start debugging on the webserver

Examples related to visual-studio

VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to download Visual Studio Community Edition 2015 (not 2017) Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio Code pylint: Unable to import 'protorpc' Open the terminal in visual studio? Is Visual Studio Community a 30 day trial? How can I run NUnit tests in Visual Studio 2017? Visual Studio 2017: Display method references