title: Issue with Sharepoint 2010 application page
body: <p>I am fairly new to both c# and sharepoint and have a very basic piece of code that is behaving unexpectedly. I am clearly lacking some of the basic troubleshooting skills here and was hoping someone would be able to point me in the right direction.</p>
  
  <p>I have an application page with one button and one label. I have a bit of code in the page load event to retrieve some information from an external list which works just fine. I have an exact copy of the code in the button which returns an error. I'd like to know why the button is returning an error when the same code in the pageload event seems fine and dandy.</p>
  
  <p>The C# code:</p>
  
  <pre><code>using System;
  using Microsoft.SharePoint;
  using Microsoft.SharePoint.WebControls;
  using System.Text;
  
  namespace farmBDCProj2.Layouts.farmBDCProj2
  {
      public partial class TestGetInfo : LayoutsPageBase
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              StringBuilder output = new StringBuilder();
              output.Append("&lt;h2&gt;Generated by PageLoad event&lt;/h2&gt;");
              SPContext context = SPContext.Current;
              using(SPSite site = context.Site)
              using (SPWeb web = site.AllWebs["BDC_SQL"])
              {
                  SPList list = web.Lists["BDC_SQL"];
                  foreach (SPListItem item in list.Items)
                  {
                      output.AppendFormat("&lt;br&gt;item: {0}", item["ADName"]);
                  }
              }
              Label1.Text = output.ToString();
          }
  
          public void btn_click(object sender, EventArgs e)
          {
              StringBuilder output = new StringBuilder();
              output.Append("&lt;h2&gt;Generated by button click event&lt;/h2&gt;");
              SPContext context = SPContext.Current;
              using (SPSite site = context.Site)
              using (SPWeb web = site.AllWebs["BDC_SQL"])
              {
                  SPList list = web.Lists["BDC_SQL"];
                  foreach (SPListItem item in list.Items)
                  {
                      output.AppendFormat("&lt;br&gt;item: {0}", item["ADName"]);
                  }
              }
              Label1.Text = output.ToString();
          }
      }
  }
  </code></pre>
  
  <p>The ASPX page:</p>
  
  <pre><code>&lt;%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %&gt;
  &lt;%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %&gt;
  &lt;%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %&gt;
  &lt;%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %&gt;
  &lt;%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %&gt;
  &lt;%@ Import Namespace="Microsoft.SharePoint" %&gt;
  &lt;%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %&gt;
  &lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestGetInfo.aspx.cs" Inherits="farmBDCProj2.Layouts.farmBDCProj2.TestGetInfo" DynamicMasterPageFile="~masterurl/default.master" %&gt;
  
  &lt;asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server"&gt;
  
  &lt;/asp:Content&gt;
  
  &lt;asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server"&gt;
      &lt;asp:Button ID="Button1" runat="server" Text="Click Here" OnClick="btn_click" /&gt;
      &lt;br /&gt;
      &lt;asp:Label ID="Label1" runat="server" Text="Label"&gt;&lt;/asp:Label&gt;
  &lt;/asp:Content&gt;
  
  &lt;asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server"&gt;
  Application Page
  &lt;/asp:Content&gt;
  
  &lt;asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" &gt;
  My Application Page
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>The error I get is at the 'foreach' stage of the btn_click event and the error text is as follows:</p>
  
  <pre><code>Microsoft.SharePoint.SPException was unhandled by user code
    Message=Access denied by Business Data Connectivity.
    Source=Microsoft.SharePoint
    ErrorCode=-2146232832
    StackTrace:
         at Microsoft.SharePoint.SPListDataSource.GetEntityInstanceEnumerator(XmlNode xnMethodAndFilters)
         at Microsoft.SharePoint.SPListDataSource.GetFilteredEntityInstancesInternal(XmlDocument xdQueryView, Boolean fFormatDates, Boolean fUTCToLocal, String firstRowId, Boolean fBackwardsPaging, String&amp; bdcidFirstRow, String&amp; bdcidNextPageRow, List`1&amp; lstColumnNames, Dictionary`2&amp; dictColumnsUsed, List`1&amp; mapRowOrdering, List`1&amp; lstEntityData)
         at Microsoft.SharePoint.SPListDataSource.GetFilteredEntityInstances(XmlDocument xdQueryView, Boolean fFormatDates, Boolean fUTCToLocal, String firstRowId, Boolean fBackwardsPaging, String&amp; bdcidFirstRow, String&amp; bdcidNextPageRow, List`1&amp; lstColumnNames, Dictionary`2&amp; dictColumnsUsed, List`1&amp; mapRowOrdering, List`1&amp; lstEntityData)
         at Microsoft.SharePoint.SPListItemCollection.EnsureEntityDataViewAndOrdering(String&amp; bdcidFirstRow, String&amp; bdcidNextPageFirstRow)
         at Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData()
         at Microsoft.SharePoint.SPListItemCollection.get_Count()
         at Microsoft.SharePoint.SPEntityInstanceEnumerator.System.Collections.IEnumerator.MoveNext()
         at farmBDCProj2.Layouts.farmBDCProj2.TestGetInfo.btn_click(Object sender, EventArgs e)
         at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
         at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
         at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
         at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    InnerException: Microsoft.BusinessData.Infrastructure.AccessDeniedException
         Message=Access denied by Business Data Connectivity.
         Source=Microsoft.SharePoint
         StackTrace:
              at Microsoft.SharePoint.BusinessData.Runtime.DataClassRuntime.ExecuteInternal(IDataClass thisDataClass, ILobSystemInstance lobSystemInstance, ILobSystem lobSystem, IMethodInstance methodInstanceToExecute, IMethod methodToExecute, IParameterCollection nonReturnParameters, Object[]&amp; overrideArgs)
              at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.ExecuteInternal(IDataClass thisDataClass, ILobSystemInstance lobSystemInstance, ILobSystem lobSystem, IMethodInstance methodInstanceToExecute, IMethod methodToExecute, IParameterCollection nonReturnParameters, Object[]&amp; overrideArgs, IFilterCollection filters)
              at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.ExecuteFiltered(IEntity this, IFilterCollection filterCollection, IMethodInstance methodInstanceToExecute, ILobSystemInstance lobSystemInstance)
              at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.&lt;FindFiltered&gt;b__3(IEntity e, IMethodInstance mi, IFilterCollection fc, ILobSystemInstance lsi)
              at Microsoft.SharePoint.BusinessData.Runtime.EntityInstanceEnumeratorFactory.CreateEntityInstanceEnumerator(IEntity entity, IMethodInstance methodInstance, IFilterCollection filters, ILobSystemInstance lobSystemInstance, ExecutionCallBack executionCallBack)
              at Microsoft.SharePoint.BusinessData.MetadataModel.Static.Entity.FindFiltered(IFilterCollection filterCollection, String finderName, ILobSystemInstance lobSystemInstance)
              at Microsoft.SharePoint.SPListDataSource.GetEntityInstanceEnumerator(XmlNode xnMethodAndFilters)
         InnerException: 
  </code></pre>
  
  <p>Any and all help would be much appreciated.</p>
  
  <p>Thanks, Matt.</p>