protected virtual void OnSelecting(CrmDataSourceSelectingEventArgs args)
        {
            EventHandler <CrmDataSourceSelectingEventArgs> handler = base.Events[EventSelecting] as EventHandler <CrmDataSourceSelectingEventArgs>;

            if (handler != null)
            {
                handler(this, args);
            }
        }
        protected override IAsyncResult BeginExecuteSelect(
            DataSourceSelectArguments arguments,
            AsyncCallback asyncCallback,
            object asyncState)
        {
            Tracing.FrameworkInformation("CrmDataSourceView", "BeginExecuteSelect", "Begin");

            if (CanSort)
            {
                arguments.AddSupportedCapabilities(DataSourceCapabilities.Sort);
            }

            if (CanPage)
            {
                arguments.AddSupportedCapabilities(DataSourceCapabilities.Page);
            }

            if (CanRetrieveTotalRowCount)
            {
                arguments.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount);
            }

            string           fetchXml;
            QueryByAttribute query;

            InitializeParameters(out fetchXml, out query);

            // raise pre-event
            CrmDataSourceSelectingEventArgs selectingArgs = new CrmDataSourceSelectingEventArgs(
                Owner,
                arguments,
                fetchXml,
                query);

            OnSelecting(selectingArgs);

            IEnumerable selectResult = null;

            if (selectingArgs.Cancel)
            {
                Tracing.FrameworkInformation("CrmDataSourceView", "BeginExecuteSelect", "Cancel");
                return(new SynchronousAsyncSelectResult(selectResult, asyncCallback, asyncState));
            }

            // merge event results
            arguments.RaiseUnsupportedCapabilitiesError(this);
            fetchXml = selectingArgs.FetchXml;

            if (CancelSelectOnNullParameter && string.IsNullOrEmpty(fetchXml) && query == null)
            {
                Tracing.FrameworkInformation("CrmDataSourceView", "BeginExecuteSelect", "CancelSelectOnNullParameter");
                return(new SynchronousAsyncSelectResult(selectResult, asyncCallback, asyncState));
            }

            try
            {
                _client = OrganizationServiceContextFactory.Create(Owner.CrmDataContextName);

                if (!string.IsNullOrEmpty(fetchXml))
                {
                    Tracing.FrameworkInformation("CrmDataSourceView", "BeginExecuteSelect: fetchXml", fetchXml);

                    var queryExpression = ToFetchExpression(arguments, fetchXml);

                    Tracing.FrameworkInformation("CrmDataSourceView", "BeginExecuteSelect", "End");

                    return(ExecuteSelect(_client, queryExpression, asyncCallback, asyncState));
                }

                if (query != null)
                {
                    Tracing.FrameworkInformation("CrmDataSourceView", "BeginExecuteSelect", "QueryByAttribute");

                    // the SortExpression has high priority, apply it to the query
                    AppendSortExpressionToQuery(arguments.SortExpression, order => query.Orders.Add(order));

                    Tracing.FrameworkInformation("CrmDataSourceView", "BeginExecuteSelect", "End");

                    return(ExecuteSelect(_client, query, asyncCallback, asyncState));
                }
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                Tracing.FrameworkError("CrmDataSourceView", "BeginExecuteSelect", "{0}\n\n{1}", ex.Detail.InnerXml, ex);
            }
            catch (Exception e)
            {
                Tracing.FrameworkError("CrmDataSourceView", "BeginExecuteSelect", "Exception: {0}", e);

                _client = null;

                return(new SynchronousAsyncSelectResult(e, asyncCallback, asyncState));
            }

            Tracing.FrameworkInformation("CrmDataSourceView", "BeginExecuteSelect", "End");

            return(new SynchronousAsyncSelectResult(selectResult, asyncCallback, asyncState));
        }