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

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

The improvements that matter to you, focused on the .NET Framework in general and the CLR or CLI especially. Read about parallel computing and concurrency support that’s now available to everybody developing for .NET — read article

 


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

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