Thursday, December 07, 2006

InvalidCastException error in an ASP.NET 2.0

Sometimes, when you use ASP.Net 2.0 with MasterPages, UserControls and dynamic loading like LoadControl, you get a silly error: Unable to cast object of type 'X' to type 'X' where X=X. Of course, it is a versioning problem. Some of these errors appear in the VS2005 designer, when the designer uses an older version of a library, but also when trying to open the web page in a browser.

Microsoft has released a fix for this: , but, to quote the same source: "This hotfix may receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next Microsoft .NET Framework 2.0 service pack that contains this hotfix.". So you risk screwing things up.

The issue seems to be mostly a developer problem. That means that during programming, you get this error when you keep changing stuff in the project. A client might get this error if an update of the site created this problem. The iisreset utility won't work. Recompiling locally and reupdating the site usually solves the issue.