Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

async call in sliverlight web resource

$
0
0
Hi

I have defined custom entities to save course and contact enrollments.

Each course entity has on-to-many enrollments and each enroll entity has a reference to a contact.
I want to get mobilephone number of each contact and show them as in a sliverlight web resource.

Here is the way I get mobilephones:

List<string> _mobileNos;

private void GetEnrolls(Guid id)
        {
            DataServiceQuery<new_enroll> query = (DataServiceQuery<new_enroll>)_context
                   .new_enrollSet.Where<new_enroll>(a => a.new_course.Id == id);
            try
            {
                query.BeginExecute(OnRetrieveEnrollsComplete, query);
            }
            catch (DataServiceQueryException dsqe)
            {
                _syncContext.Send(new SendOrPostCallback(showErrorDetails), dsqe);
            }
}

 private void OnRetrieveEnrollsComplete(IAsyncResult result)
        {
            //Retrieve the query that was 
            DataServiceQuery<new_enroll> results = result.AsyncState as DataServiceQuery<new_enroll>;



            //Create a collection of Accounts and set it equal to the query result
            ObservableCollection<new_enroll> _enrolls =
                new DataServiceCollection<new_enroll>(results.EndExecute(result));

           

            var dlg=new MobileListDelegate(GetMobilesOfEachContact);
            
            IAsyncResult asyncresult = dlg.BeginInvoke(_enrolls,FillMobileCompleted,null);

//dlg.EndInvoke(asyncresult);

}

private void FillMobileCompleted()
        {
            //MobileListDelegate dlg = (MobileListDelegate)((AsyncResult)result).AsyncDelegate;

            //dlg.EndInvoke(result);

            

            string x = null;

            if (_mobileNos.Count == 0)
            {
                x = " List Is Null";
            }

            foreach (var item in _mobileNos)
            {
                x += item + " , ";
                
            }

            NumberOfEnrolls.Text += x;
        }

private void GetMobilesOfEachContact(ObservableCollection<new_enroll> _enrolls)
        {
            _mobileNos = new List<string>();

            foreach (var item in _enrolls)
            {
                GetContactMobile(item.new_student.Id);

            }
        }

private void GetContactMobile(Guid? contactId)
        {
            if (contactId.HasValue)
            {
                DataServiceQuery<Contact> query = (DataServiceQuery<Contact>)_context
                    .ContactSet.Where<Contact>(a => a.ContactId == contactId);

                query.BeginExecute(OnRetrieveMobileComplete, query);
            }
        }

        private void OnRetrieveMobileComplete(IAsyncResult result)
        {
            try
            {
                DataServiceQuery<Contact> results =
                    result.AsyncState as DataServiceQuery<Contact>;

                Contact retrivedcontact = new DataServiceCollection<Contact>(results
                    .EndExecute(result)).First<Contact>();

                _mobileNos.Add(retrivedcontact.MobilePhone);
               
  
            }
            catch (DataServiceQueryException dsqe)
            {

                _syncContext.Send(new SendOrPostCallback(showErrorDetails), dsqe);
            }
            catch
            {
                MessageBox.Show("Error");
            }
        }

First Of All, I cant have AsyncResult in sliverlight as it seems,  even without referencing this class, the code never reaches 

_mobileNos.Add(retrivedcontact.MobilePhone);

I also tried calling endinvoke right after beigninvoke but did not help.

I also used background worker, but it doesn`t help me, and , and string list is still empty.

Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>