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

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

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 –

  • lienys

    thank you.. it really fix my problem

  • Jesse

    Thanks for sharing. Now, five years later, know that another stumped developer ended hours of frustration finding this nugget.

  • Ashwani Kumar

    Thanks i was trying to resolve this error from last two days and i have tried hundred of solutions but only your solution works for my problem .Thanks one again

  • Eric

    Thanks so much. As you said… I stared at the code for *HOURS* before stumbling across your answer.

  • Peter

    I really appreciate your hint ! Cool !

  • Carlo

    you are my saver!!!! thankyou!

  • cai

    I have the same problem but after I changed, there is a compilation error saying “Make sure that the class defined in this code file matches the inherits attribute, any idea ? Please help Thanks

  • satz

    Thanks. it helped

  • johny

    u are fantastic my friend

  • Mohan Singh

    Thanks it works

  • Maya

    Thank you somuch for this post…

  • Roze

    Great!! Thank you so much!!!

  • Nate

    Wow. 3.5 years on and this is still a gem. Just saved me a hell of a lot of time. Thank the lord for coders who like to share!

  • Shameel Faraz

    i really dont understand why , but it worked , i am working on this project that was already made by someone almost completely and out of nowhere this error started occuring , i changed it to codefile it works again ??? :S

  • Farooq

    Awesome! Thanks a trillion :)

  • Bartosz

    Thank you. It worked for me. Seems I have an ASP.net WebSite.

  • Michael E.

    Thank you. Thank you. I might never have found it.

  • rui

    thank you !!!

  • Dušan Knežević

    Thank You so much!

  • Ppciuser

    Thanks.  I did stare at it quite a while before I started googling.

  • Gayatri

    Thanks a ton.. It saved my time :)

  • Tim

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

  • D. Li

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

  • John W

    “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

    Ohh, Thanks, good info

  • http://www.reddit.com/r/reddit.com/comments/g2l95/fashion_beauty_store/ Benedict Howard

    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

    I’ve been struggling with this error!

    you saved me!

    Thanks a lot!!!

  • Igor

    Your article very helped me and saved time.

    Thanks!

  • Arch

    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

    Wow… thanks!

  • Nandan D

    Thanks.Was driving me crazy!!

  • joebloe

    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.

    • http://www.undermyhat.org Abel Braaksma

      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

    thanks a lot..was worreid about it.

  • Howard

    Thank you.

  • http://www.iris.co.uk Justin Beasley

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

  • Pingback: Solving error “Could not load type” in Visual Studio « Wez Morris’ Weblog

  • Polika

    Thanks ya … :)

  • Bill Kuhn

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

    This fixes it!

    Thanks!!!

    • Koh

      I have the same problem but after I changed, there is a compilation error saying “Make sure that the class defined in this code file matches the inherits attribute, any idea ? Please help Thanks

Get Adobe Flash player