0

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.

Details:

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
{
    [ToolboxItemAttribute(false)]
    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);  <-----
            Controls.Add(control);
        }
    }
}

The output window contains:

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

2 Answers 2

1

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
0

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.

4
  • 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, 2010 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. Nov 3, 2010 at 15:16
  • But why did the web part initially work? Also, what setting should I try changing?
    – Anonymous
    Nov 5, 2010 at 13:29
  • It could work if the webpart was in Farm Solution mode. You can change it in your project properties. Nov 5, 2010 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.