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 –
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
Leave a Response
Additional comments powered by BackType
Thanks a ton.. It saved my time
Thank you very much, this resolved my problem and saved me much headache!
Thanks so much, converting between a web application project and a web project has so many obscure gotchas!
“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!
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
I’ve been struggling with this error!
you saved me!
Thanks a lot!!!
Your article very helped me and saved time.
Thanks!
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
Wow… thanks!
Thanks.Was driving me crazy!!
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.
thanks a lot..was worreid about it.
Thank you.
Me too – Its been driving me crazy and you just fixed it, THANK YOU!!!
[...] Solving error “Could not load type” in Visual Studio From Undermyhat.org (original post: http://www.undermyhat.org/blog/2009/07/tip-solve-could-not-load-type-error-when-copying-code-in-visu...) [...]
Thanks ya …
Holy crap! I’ve been fighting this stupid error for hours!
This fixes it!
Thanks!!!