Warm regards,

I'm trying to display the latest events in my home page using user control this is my code it shows unexpected error Idk why please help?

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.IO;
using Microsoft.SharePoint;

namespace CalenderEventView.ControlTemplates.CalenderEventView
    public partial class CalenderE : UserControl
        protected void Page_Load(object sender, EventArgs e)

            SPSite mySite = SPContext.Current.Site;
            SPWeb myWeb = mySite.OpenWeb();
            SPList myList = myWeb.Lists["Calendar List"];

            SPListItemCollection items = myList.Items;
            if (items.Count != 0)
                EventCal.DataSource = items;


        protected void EventCal_ItemCommand(object source, RepeaterCommandEventArgs e)


        protected void EventCal_ItemDataBound(object sender, RepeaterItemEventArgs e)
            //if (e.Item.ItemIndex >= 3)
            //    System.Web.UI.HtmlControls.HtmlGenericControl div = e.Item.FindControl("listItem") as System.Web.UI.HtmlControls.HtmlGenericControl;
            //    div.Visible = false;



<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CalenderE.ascx.cs" Inherits="CalenderEventView.ControlTemplates.CalenderEventView.CalenderE" %>

<asp:Repeater ID="EventCal" runat="server" OnItemCommand="EventCal_ItemCommand" OnItemDataBound="EventCal_ItemDataBound">
         <div id="listItem"></div>


1 Answer 1


unexpected error

What's the error? Any other details? Does the account you are testing with have permissions to that list? Are you debugging from Visual Studio?

You will need to pick some data to display. Change:


<div id="listItem"><%# DataBinder.Eval(Container.DataItem, "Title")  %></div>

I don't think it is required, but try moving the data to a DataTable object first:

EventCal.DataSource = items.GetDataTable();
  • Thank you for your help I fixed the problem I did not have access to the list I changed the URL to the subsite's Url and worked just fine
    – N.M
    Commented Mar 19, 2018 at 10:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.