Visual Studio 2010 suddenly can't see namespace?

C#WinformsNamespaces

C# Problem Overview


My C# WinForms solution has two projects. A DLL which is the main project I'm working on, and an executable WinForms I call "Sandbox" so that I can compile/run/debug the DLL easily in one go.

I'm working in .Net 4.0 for both projects.

Everything was working fine until I added some seemingly innocent code, and a reference to System.Web in the DLL. Now my Sandbox project can't see the namespace of the DLL project. I didn't change anything which I believe should have affected this.

If I delete the project reference to the DLL from the Sandbox references and re-add it, then the red underlines all disappear and the colour coding comes back for all my classes etc; but as as soon as I try to build the solution, the whole thing falls apart again.

When I right-click the DLL project in the Sandbox's references and view in object browser, I can see the namespace and all the stuff in there.

I have a feeling this might be some sort of bug?

Is this some sort of VS2010 bug? I had this same issue a few months ago and I could only fix it at the time by making a whole new project and re-importing my files. This time, however, I have a bajillion files and will only do that as a last resort!

Edit: After panickedly going through and undoing all my changes, trying to find what caused the problems, it seems to be this line:

string url = "http://maps.google.com?q=" + HttpUtility.UrlEncode(address);

If I comment out this line, then I get no namespace errors and the project builds fine. I can't see anything wrong with this line though.

C# Solutions


Solution 1 - C#

I'm ready to declare this a bug in VS2010, this has bitten way too many programmers already. The fix is easy: Project + Properties, Application tab, change Target Framework to ".NET Framework 4" instead of the Client Profile that is selected by default.

System.Web is not included in the client profile. Having this option in the first place is quite silly, the client profile is only 15% smaller than the full version of .NET 4.0. Having it selected by default is even sillier. But I digress.

UPDATE: mercifully this all got fixed in VS2012. Which no longer makes the client profile the default for a new project. And the client profile got retired completely in .NET 4.5, good riddance.

Solution 2 - C#

Check to make sure that both projects are using the non-client profile for their target framework (go to each project's properties to do this).

Solution 3 - C#

One possibility is that the target .NET Framework version of the class library is higher than that of the project. I faced this problem, and I solved it by closing visual studio, reopening visual studio, cleaning and rebuilding the solution. This worked for me. On some other posts, I have read the replies and most of users solved the problem by following this way.

Solution 4 - C#

Try building only the project with the Sandbox dll first independently.

Then point your executable project to the required dll and ensure copy local is set to true. in reference settings.

Tthen build the executable project.

Solution 5 - C#

Changing the target framework from the ".NET Framweork 4 Client Profile" to ".NET Framework 4" worked for me with a similar problem. I agree that the client profile doesn't seem to have much of an advantage to using it. I seem to get nailed with weird errors that I hunt for until I remember that Visual Studio defaults to the client profile. I guess the moral of the story when getting an error is: if "Rebuild Solution" doesn't work, check the Target framework...

Solution 6 - C#

If you tried already doing the Framework change, and still not worked, I hope this works for you (as it did for me): Simply add the necessary references from within your projects. Very obvious but I was doing it wrong until I found what was the issue.

Solution 7 - C#

I just had this issue and it turned out to be I had multiple namespaces being used that had the same object name (i.e. business objects had the same names as mvc models);

Fully qualifying the names fixed the issue for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOzzahView Question on Stackoverflow
Solution 1 - C#Hans PassantView Answer on Stackoverflow
Solution 2 - C#Mark AveniusView Answer on Stackoverflow
Solution 3 - C#Muhammad SohailView Answer on Stackoverflow
Solution 4 - C#Robin MabenView Answer on Stackoverflow
Solution 5 - C#AndyView Answer on Stackoverflow
Solution 6 - C#lu1sView Answer on Stackoverflow
Solution 7 - C#Mike CheelView Answer on Stackoverflow