Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am brand new to SharePoint, SharePoint development, and MS development.

My SharePoint dev environment consists of Windows 2008, SharePoint 2010, and Visual Studio 2010.

I created a very simple "Hello World" Visual WebPart, deployed it to SharePoint through Visual Studio, added it to a new page, and was able to view it with no problem.

I then enhanced the WebPart by adding a few controls and some code. When I deployed the Web Part, and went to the page containing it, I received an error in Visual Studio: "HttpCompileException was unhandled by user code".

I then erased all of my new code, so that the XXXUserControl.ascx file contains only a single button and the XXXUserControl.ascx.cs file contains only an empty Page_Load method. I deployed again, but received the same error.

I'm looking either for an explanation of what's happening, or tips on how to debug.


The error is: "HttpCompileException was unhandled by user code"

The error occurs in the auto-generated XXXWebPart.cs file on the line marked with "<-----" below:

namespace UserSyncTest1.UserSyncTest1WebPart
    public class UserSyncTest1WebPart : WebPart
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/UserSyncTest1/UserSyncTest1WebPart/UserSyncTest1WebPartUserControl.ascx";

        protected override void CreateChildControls()
            Control control = Page.LoadControl(_ascxPath);  <-----

The output window contains:

A first chance exception of type 'System.Web.HttpCompileException' occurred in System.Web.dll

share|improve this question

2 Answers 2

The error can relate to a lot of things:

File (ASCX) is not valid: Create

  • a new visual webpart, then compare the 2 ascx files, a small typo like a < or > to much can break a file
  • Did you change any project settings: Target framework (can't be 4.0), Deployment type (Sandbox or Farm)
  • Server ran out of memory and stopped compiling (Check the eventlogs)
  • SharePoint is "broken" i.e. SharePoint's Virtual Path Provider is looking for the ascx in the wrong place: delete webapp, reboot, then recreate webapp + sitecollection and redeploy
share|improve this answer

In UserCode solution type, you can't refer to the disk (in this case, the LoadControl will have an IO on the server, to the file /_CONTROLTEMPLATES/UserSyncTest1/UserSyncTest1WebPart/UserSyncTest1WebPartUserControl.ascx).

You should, or make a Farm Solution, or generate your HTML from your control, in code, not in declarative way.

share|improve this answer
Thanks for your reply. However, this bit of code (in fact the whole XXXWebPart.cs file) is auto-generated by Visual Studio. It looks like SharePoint does in fact put the ascx file at that location as part of deploying the Web Part. –  Anonymous Nov 2 '10 at 23:24
I understand that the code is auto-generated. But you surely can't use ascx in a user code solution. You change this setting in your project properties. The UserCode solutions (Sandbox) are protected from using any IO. This is the error you are getting. –  Sébastien Levert Nov 3 '10 at 15:16
But why did the web part initially work? Also, what setting should I try changing? –  Anonymous Nov 5 '10 at 13:29
It could work if the webpart was in Farm Solution mode. You can change it in your project properties. –  Sébastien Levert Nov 5 '10 at 15:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.