Tip: Solving error “Could not load type” in Visual Studio

Posted on 17 July 2009

If you copy or import code from one Visual Studio 2008 project to another, for instance the aspx, ascx or master files, then you may receive the above error and it may take ages before you find out that the solution is extraordinarily simple:

change CodeBehind in CodeFile

The following example shows that for a Web UserControl, but it works the same for a Page, a Master Page and a WebPart:

<!-- change this (ASP.NET Web Site version) -->
<%@ Control Language="C#" AutoEventWireup="true"
     CodeFile="WebUserControl.ascx.cs"
     Inherits="Controls_WebUserControl" %>

<!-- into this (ASP.NET Web Application version), or vice versa -->
<%@ Control Language="C#" AutoEventWireup="true"
     CodeBehind="WebUserControl.ascx.cs"
     Inherits="Controls_WebUserControl" %>

That’s all you need to do to get rid of that annoying error!

The reason? If you have an “ASP.NET Web Application”, Microsoft needs CodeBehind in the Page, Control or Master directive, if you have an “ASP.NET Web Site”, the compiler expects CodeFile instead. I have no idea why this difference exists, it is very annoying and you can stare forever at your code before you find it, but this is the solution for this scenario.

– Abel –

Other Hatties at Under My Hat

Don’t follow the advised on the TechNet, MS Connect and MSDN forums, they are time-consuming and don’t work if you have a newer version of .NET installed like .NET 4.0. Instead, read this and easily fix this hard-to-resolve error by simply changing one registry value. read article

Ever tried to shrink a volume? Ever wondered why you cannot shrink a volume smaller than half its size? Ever wondered what $MFTMirr is all about and what it’s doing in the middle of your drive? Or do you just want to get the biggest available free space and shrink your drive? Then this article is for you — read article

Have you ever received this error using Windows System Backup and Restore Center? Never managed to get rid of it or it mysteriously keeps coming back? Here’s a lightweight and easy solution — read article

 


19 responses to Tip: Solving error “Could not load type” in Visual Studio

  • Gayatri says:

    Thanks a ton.. It saved my time :)

  • Tim says:

    Thank you very much, this resolved my problem and saved me much headache!

  • D. Li says:

    Thanks so much, converting between a web application project and a web project has so many obscure gotchas!

  • John W says:

    “it is very annoying and you can stare forever at your code before you find it”

    Well, I stared forever but never saw it–thanks SO MUCH!

  • HG says:

    Ohh, Thanks, good info

  • As soon as I came over to Tip: Solving error “Could not load type” in Visual Studio | Under My Hat I can only see portion of it, is this my net internet browser or the web website? Should I reboot? Cheers Benedict Howard

  • kamal says:

    I’ve been struggling with this error!

    you saved me!

    Thanks a lot!!!

  • Igor says:

    Your article very helped me and saved time.

    Thanks!

  • Arch says:

    Wow!!! You made my day…. I was thinking of creating new aspx files copy paste the code ebehind…but that would be a nightmare…..thanks a lot

  • Bob Sherwood says:

    Wow… thanks!

  • Nandan D says:

    Thanks.Was driving me crazy!!

  • joebloe says:

    Worked perfectly. What exactly is the difference between a web application and website?

    I was working in a ecommerce site that I guess was compiled as a web application, because it used ‘codebehind’. I was editing the site as a ‘web site’ and added a control file. Nothing worked until I changed to ‘codefile’.

    Just trying to understand the why.

    • There are many differences between a website and a web application. The most apparent being that a website does not have a project file but is just a bunch of files that together comprise your website (you can even open a live website over FTP or HTTP). It’s very much “what you see is what you get”, no hidden feats. A file in a website project is a file in the actual website.

      A web project, on the contrary, has a project file, contains files that are included / excluded and has compile settings on the project. It can be compared to a classic WinForms project.

      More information in this blog post about Website vs Web Application.

  • bhsakar says:

    thanks a lot..was worreid about it.

  • Howard says:

    Thank you.

  • Me too – Its been driving me crazy and you just fixed it, THANK YOU!!!

  • Polika says:

    Thanks ya … :)

  • Bill Kuhn says:

    Holy crap! I’ve been fighting this stupid error for hours!

    This fixes it!

    Thanks!!!

  • Leave a Response

    Additional comments powered by BackType

    Get Adobe Flash playerPlugin by wpburn.com wordpress themes

    Copyright © Under My Hat.

    Other Recent Hatties (alphabetically)