364

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?

Sire
  • 4,086
  • 4
  • 39
  • 74
Attilah
  • 17,632
  • 38
  • 139
  • 202

17 Answers17

700

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.

Community
  • 1
  • 1
Nathan Koop
  • 24,803
  • 25
  • 90
  • 125
  • 13
    I set the "Target Framework" by right clicking the project in solution explorer, and selecting properties. Target Framework should be visible in default Application tab. – Cookie Jan 02 '12 at 06:26
  • 4
    @jaminator This is a solution in some instances. At least in mine. For some reason, my project refused to accept a namespace existed from the same solution. The problem was that the console application was targeting the client profile. – Amadiere Mar 02 '12 at 13:48
  • My setup: exe - framework 4.0 client profile refering dll (framework 2.0) Solution: setting exe to framework 4.0 (not client profile) i did not have to touch the dll it is still framework 2.0. This is not the first time the "client profile" makes trouble for me... – mortb Nov 14 '12 at 11:09
  • 76
    +1 to this. Horribly misleading error message. Probably saved me hours of trying to figure it out. Hey Microsoft how about "unable to import namespace BLAH because BLAH is an incompatible version" but then I guess it would have robbed Nathan of 184 upvotes. – Gerald Davis Jan 06 '14 at 16:22
  • 3
    Is there a workaround for cases where we can't change the Target-frameworks of the projects? – Raj123 Jun 10 '15 at 14:01
  • 4
    Switching from "Error List" tab to "Output" tab in the Visual Studio revealed the actual error for me (in fact warnings that caused the error). There were actually references to other Libraries missing (in my case Microsoft.Bcl). – Stefan Aug 25 '16 at 20:57
  • Verify if your SharePoint project was set accidentally to be a Sandboxed solution instead of Farm solution. The Sandboxed solutions have a limited support for classes in Microsoft.SharePoint.dll, that may result in the same build error "Type or namespace name does not exist". Our project was set to Sandboxed due to repeated unloading and reloading the project file in Visual Studio 2013 (bug?). See: http://stackoverflow.com/questions/20741291/receiving-error-of-the-type-or-namespace-name-layoutspagebase-could-not-be-fo – pholpar Nov 15 '16 at 10:49
  • 1
    I have main WPF project, and DLL targeting netstandard2.0. I had to change target for WPF to earlier one (.NET 4.5.2) then I got error that netstandard2.0 cannot work with 4.5.2. Then I changed again targeting framework of WPF to the one that was before. And it worked :| – Adam Jachocki Sep 07 '18 at 14:20
  • if you want to upvote this it may help you and others in the future if it's implemented https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/30934717-provide-a-warning-when-projects-target-different-v – Steve Oct 05 '18 at 07:47
  • In my case I have to change (replace AT with the symbol) ATusing (Html.BeginForm()) to ATusing (Html.BeginForm("ForgottenPassword", "Account", FormMethod.Post)) -- easy to look past if your error message is something totally different! – indofraiser Nov 12 '20 at 22:40
49

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

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
john west
  • 646
  • 5
  • 9
  • 2
    Worked for me too! But I had to do it twice... go figure! – nicholeous Jul 12 '13 at 16:38
  • After manually merging together some web.config files of different target frameworks for the same app, the web.config stopped working and I got a 500 Internal Server Error. After resetting as described, some old settings were removed by Visual Studio 2012 automatically from the web.config, and then the app worked again. – humbads Feb 24 '14 at 16:49
  • Had a similar issue, I think certain framework versions just don't recognize `System.Linq` as a valid library – Callat Jun 19 '17 at 19:45
  • That was truly a magic. Just like my new Mobile Honor 8X, in which sometimes changing the settings does not take effect immediately. In order for it to work properly I have to switch back and forth multiple times. – Harish Ninge Gowda Apr 02 '19 at 06:36
  • This worked in Visual Studio 2019 for .NET 4.6.1 to 3.5 and back but also majorly messed-up Visual Studio causing every action to invoke an error dialog with the text: "No LocalRegistry Service". Upon closing the application (this requires 2 clicks on close as the first will also invoke the dialog) and re-opening, the solution then built as expected and those annoying error dialogs were gone. – Matt Arnold Feb 24 '20 at 14:45
  • Also beware of the "Output type" of your project changing; I had a "Class Library" turn into a "Window Application" project! It can be changed back via the Project Properties (Application tab) after restarting Visual Studio. – Matt Arnold Feb 24 '20 at 14:50
  • Don't do this in an MVC Web Application! It results in an error page with `The compiler failed with error code -532462766` when you try to run the solution. – Matt Arnold Feb 24 '20 at 16:07
38

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

Robotnik
  • 3,643
  • 3
  • 31
  • 49
KiwiNige
  • 1,146
  • 3
  • 16
  • 18
22

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)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ModChowdhury
  • 501
  • 4
  • 5
  • 1
    For me, I didn't need to set Copy Local -> True, but actually Specific Version -> False. This cleared it up. – order Mar 17 '22 at 21:05
16

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.

Mike Guthrie
  • 4,029
  • 2
  • 25
  • 48
hakanali_md
  • 161
  • 1
  • 2
15

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 :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gorglar
  • 295
  • 2
  • 2
  • 24
    this merely repeats point made and explained in [prior answer](http://stackoverflow.com/a/7155510/1864167) that was posted several months before this one – gnat Dec 09 '14 at 09:18
14

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.

Community
  • 1
  • 1
GrandMasterFlush
  • 6,269
  • 19
  • 81
  • 104
  • 1
    I had this issue as well, for reference. It would be nice if there was a more explanatory error message in the tool, but oh well. – Nick May 29 '14 at 19:13
  • 4
    I posted something on MSDN abput it but MS said it wasn't a bug. I won't argue with that but I would have hoped they would have understood how it could cause problems. – GrandMasterFlush May 29 '14 at 19:15
  • 1
    I was trying to use the same name for the class and the namespace. At minimum, this is confusing, and may also cause errors. – JosephDoggie Oct 11 '22 at 14:04
8

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.

zDan
  • 81
  • 1
  • 1
6

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.

Mario
  • 3,405
  • 6
  • 38
  • 48
  • I had this issue with .NET 4 project using System.Management.Automation when testing project with Visual Studio 2013 on Windows 10. Upgrading project to .NET 4.5.1 fixed problem. – Malcolm McCaffery Jun 02 '15 at 00:29
3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tom Stu
  • 31
  • 2
2

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.

Mxyk
  • 10,678
  • 16
  • 57
  • 76
Fred
  • 51
  • 1
  • Yes, for me this was the correct answer, also. Also: In my case, I was trying to use the same name for the class and the namespace. At minimum, this is confusing, and may also cause errors. I had to fully qualify everything, e.g. ourNameSpace.OurClass.OurStatitcMethod. This answer was very useful, sorry it was initially down-voted, but as of now it has 2 net-positive votes! – JosephDoggie Oct 11 '22 at 14:05
2

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

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
2

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.
Sasha Bond
  • 984
  • 10
  • 13
2

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.

Klaus Nji
  • 18,107
  • 29
  • 105
  • 185
  • in my case vs2010, a dll reference has error, I readd the dll reference and it seems great, but after build the `type namespace error` occur. And I restart visual studio, after I restart, the original dll error show again, and I readd it again, and build again, success. – yu yang Jian Dec 29 '16 at 08:23
1

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.

Skystrider
  • 389
  • 3
  • 13
0

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

JabberwockyDecompiler
  • 3,318
  • 2
  • 42
  • 54
-3

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

sajad
  • 923
  • 2
  • 10
  • 25