83

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.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Ozzah
  • 10,631
  • 16
  • 77
  • 116

7 Answers7

150

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    Thanks for that. My DLL was set to .Net 4.0, but the Sandbox was set to .Net 4.0 Client Profile. After working on this project for 5 months, the amount of panic I got when the whole thing just fell apart for seemingly no reason... At least I'll know for next time! – Ozzah Feb 02 '11 at 22:46
  • 1
    I have changed the Target Framework to 4.0 in all my projects, however namespaces of the depending projects still can't be found. Is there anything else I should be sure to verify ? – Steven Ryssaert Dec 09 '11 at 18:15
  • @UwConcept - this isn't about the version number. It is about full vs client profile. Please start your own question if that doesn't help. – Hans Passant Dec 09 '11 at 18:20
  • @HansPassant Yes, I'm sorry if my question was confusing. I was using the 4.0 Client version, and changed that to 4.0. Still, the namespaces in other projects in my solution can't be found cross-project. – Steven Ryssaert Dec 09 '11 at 18:27
  • @HansPassant I managed to solve my problem in the meanwhile. Apparently my project references were cleared for some reason. By re-adding them, the namespaces could be found again. – Steven Ryssaert Dec 09 '11 at 18:44
  • That did it! I only had this problem when I added an existing, external project to my solution. – bradjive Aug 22 '12 at 20:39
  • This solved an issue for me unrelated to System.Web. I had a reference to an included project flickering in and out, and changing to .NET 4 instead of Client Profile fixed that as well. Something fishy going on here. – Ducain Feb 07 '13 at 20:07
  • Wow that is kind of strange that it just does that eventually. Thank you! – Serguei Fedorov Jun 18 '13 at 14:44
  • Bug is also present in VS 2013 – ToXinE Dec 19 '14 at 15:16
8

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

Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
2

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.

Muhammad Sohail
  • 598
  • 7
  • 14
1

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.

Robin Maben
  • 22,194
  • 16
  • 64
  • 99
  • And for the love of all things holy make sure the target type is set to Compile and not Content (which mine was for some reason) – jcolebrand May 22 '12 at 21:06
0

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.

Mike Cheel
  • 12,626
  • 10
  • 72
  • 101
0

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

Andy
  • 91
  • 2
0

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.

lu1s
  • 5,600
  • 3
  • 22
  • 37