locked
Ado Data Service and Silverlight RRS feed

  • Question

  • Excuse me if this is not the good forum.

    I Try fill a datagrid in silverlight and ado.net dATA sERVICE.

        public partial class AdoNet : Page
        {
            SiteSqlTonicEntities svcContext;
            ObservableCollection<TabCptClie> ClientsBindingCollection;
        
            public AdoNet()
            {
                InitializeComponent();
    
                // Create the binding collections.
                ClientsBindingCollection = new ObservableCollection<TabCptClie>();
    
            }
    
            private void ResetBindingData()
            {
                // Create a new data service context.
                svcContext =
                    new SiteSqlTonicEntities(new Uri("TonicClient.svc", UriKind.Relative));
    
                ClientsBindingCollection.Clear();
              //  detailsBindingCollection.Clear();
            }
            
            private void LireLesClients_Click(object sender, RoutedEventArgs e)
            {
                // Instantiate the data service context and clear any existing bindings.
                ResetBindingData();
    
                DataServiceQuery<TabCptClie> query = svcContext.TabCptClieJeu;
                try
                {
                    // Begin the query execution.
                    query.BeginExecute(OnClientQueryComplete, query);
                }
                catch (Exception ex)
                {
                    messageTextBlock.Text = ex.Message;
                }
    
            }
    
            private void OnClientQueryComplete(IAsyncResult result)
            {
                // Use the Dispatcher to ensure that the 
                // asynchronous call returns in the correct thread.
                Dispatcher.BeginInvoke(() =>
                {
                    
                    // Get the original query back from the result.
                    DataServiceQuery<TabCptClie> query =
                        result.AsyncState as DataServiceQuery<TabCptClie>;
                       
                    try
                    {
                          TabCptClie returnedClient = query.EndExecute(result) ERROR !!!!!!!;
                        
                        if (returnedClient != null)
                        {
                            // Load the retuned orders into the binding collection.
                            //   foreach (Orders order in returnedClient.Orders)
                            //   {
                            //       ClientsBindingCollection.Add(order);
                            //   }
    
                        
                            // Bind the grid control to the collection and update the layout.
                            this.ordersGrid.DataContext = ClientsBindingCollection;
                            this.ordersGrid.UpdateLayout();
    
                            // Hide the relationship property columns.
                            this.ordersGrid.Columns[8].Visibility = Visibility.Collapsed;
                            this.ordersGrid.Columns[11].Visibility = Visibility.Collapsed;
                            this.ordersGrid.Columns[12].Visibility = Visibility.Collapsed;
                        }
                    }
                    catch (DataServiceQueryException ex)
                    {
                        this.messageTextBlock.Text = string.Format("Error: {0} - {1}",
                            ex.Response.StatusCode.ToString(), ex.Response.Error.Message);
                    }
    
                }
                );
            }
    My problem is how recup   IAsyncResult result to load the returnes TabCptClie
    into the binding collection.

                            //   foreach (Orders order in returnedClient.Orders)
                            //   {
                            //       ClientsBindingCollection.Add(order);
                            //   }


    Thank you .
    Desvernes Claide

    Thursday, December 3, 2009 6:06 PM

Answers

  • The BeginExecute method returns the IAsyncResult, which can be used to get the results.

    In the sample you posted, you need to get the IAsyncResult returned by beginexecute method and pass it the OnClientQueryComplete method, and use that instance of the IAsyncResult.       
    private void LireLesClients_Click(object sender, RoutedEventArgs e)
    {
        // Instantiate the data service context and clear any existing bindings.
        ResetBindingData();
    
        DataServiceQuery<TabCptClie> query = svcContext.TabCptClieJeu;
        try
        {
             // Begin the query execution.
             query.BeginExecute(OnClientQueryComplete, query);
        }
        catch (Exception ex)
        {
            messageTextBlock.Text = ex.Message;
        }
    }
    Hope that helps.
    Thanks
    Pratik

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, December 4, 2009 7:14 PM
    Moderator
  • I Have resolved my problem. The following code is good.


     

    private void OnClientQueryComplete(IAsyncResult result)
    {
          Dispatcher.BeginInvoke(() =>
          {
              DataServiceQuery<TabCptClie> query =
                          result.AsyncState
    as DataServiceQuery<TabCptClie>;
              try
              {
               var  returnedClient query.EndExecute(result);
               if (returnedClient != null)
               {
                   foreach (var item in returnedClient)
                   {
                          ClientsBindingCollection.Add(item);
                   }
                   this.ordersGrid.DataContext = ClientsBindingCollection;
                   this.ordersGrid.UpdateLayout();
               }
               }
               catch (DataServiceQueryException ex)
               {
                      this.messageTextBlock.Text = string.Format("Error: {0} -
                                  {1}"
    ,ex.Response.StatusCode.ToString(), ex.Response.Error.Message);
               }
          });
    }


    I Have only replace  TabCptClie returnedClient by var returnedClient


    Thank you to all.
     to Lingzhi Sun , for the link in All-In-One Code Framework, CSADONETDataServiceSL3Client.
     and to Vitek Karas  for ADO.NET Data Services v1.5 CTP2.
     
    AT More
    Desvernes Claude

    • Marked as answer by Cdes Saturday, December 5, 2009 2:26 PM
    Saturday, December 5, 2009 2:25 PM

All replies

  • Hello Desvernes,

     

    Welcome to MSDN Forums!

     

    To be honest, I am not an expert on Silverlight.   I consult one of my colleagues who is an expert of Silverlight.  He recommend you refer to one sample he wrote in All-In-One Code Framework, CSADONETDataServiceSL3Client.  The server side is the sample CSADONETDataService, which is written by me.  J

     

    In the file SchoolLinqToEntitiesUpdate.xaml.cs,

    =============================================================================================
            /// <summary>

            /// Callback method of the query to get CourseGrade records.

            /// </summary>

            /// <param name="result"></param>

            private void OnCourseGradeQueryComplete(IAsyncResult result)

            {

                Dispatcher.BeginInvoke(() =>

                {

     

                    DataServiceQuery<CourseGrade> query =

                           result.AsyncState as DataServiceQuery<CourseGrade>;

                    try

                    {

                        var returnedCourseGrade =

                            query.EndExecute(result);

     

                        if (returnedCourseGrade != null)

                        {

                            _collection = (from c in returnedCourseGrade.ToList()

                                           select new ScoreCardForSchoolLinqToEntities()

                                           {

                                               // Due to the following QueryInterceptor at server side only Course ID>4000 can be returned:

                                               // [QueryInterceptor("Course")]

                                               // public Expression<Func<Course, bool>> QueryCourse()

                                               // {

                                               //     // LINQ lambda expression to filter the course objects

                                               //     return c => c.CourseID > 4000;

                                               // }

     

                                               CourseGrade = c,

                                               Course = c.Course == null ? "Only Course ID>4000 can be shown here" :

                                                   c.Course.Title,

                                               Grade = c.Grade,

                                               PersonName = string.Format("{0} {1}",

                                               c.Person.FirstName, c.Person.LastName)

                                           }).ToList();

     

                            this.mainDataGrid.ItemsSource = _collection;

                        }

                    }

                    catch (DataServiceQueryException ex)

                    {

                        this.messageTextBlock.Text = string.Format("Error: {0} - {1}",

                            ex.Response.StatusCode.ToString(), ex.Response.Error.Message);

                    }

                });

            }
    =============================================================================================

     

    The logic is similar with your code sample.  Could you also provide us with the detailed exception?  

     

    Since this question is more related to this WCF Data Services (formerly named ADO.NET Data Services) instead of Entity Framework, I will move this thread to WCF Data Services forum.

     

    Have a nice day!

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, December 4, 2009 2:50 AM
    Moderator
  • The BeginExecute method returns the IAsyncResult, which can be used to get the results.

    In the sample you posted, you need to get the IAsyncResult returned by beginexecute method and pass it the OnClientQueryComplete method, and use that instance of the IAsyncResult.       
    private void LireLesClients_Click(object sender, RoutedEventArgs e)
    {
        // Instantiate the data service context and clear any existing bindings.
        ResetBindingData();
    
        DataServiceQuery<TabCptClie> query = svcContext.TabCptClieJeu;
        try
        {
             // Begin the query execution.
             query.BeginExecute(OnClientQueryComplete, query);
        }
        catch (Exception ex)
        {
            messageTextBlock.Text = ex.Message;
        }
    }
    Hope that helps.
    Thanks
    Pratik

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, December 4, 2009 7:14 PM
    Moderator
  • Hi,

    Since your code notes that the EndExecute fails, could you please share with us the exception it fails on? (message and callstack, if there's inner exception, then that one as well).

    I don't see any problem in the code, no obvious reason why it would fail. You're trying to use observable collection, but who is the observer in your case? If the data grid adds a new item into it, who is going to call the AddObject on the context?

    If you could use ADO.NET Data Services v1.5 CTP2 then most of this will be much easier. We now support code generation which supports data binding, so you get the observable collections which will automatically call the right methods on the context.
    We also support LoadAsync method on the collection, so that you don't have to write the Begin/End and you simply add the collection as a data/item source and then call LoadAsync.
    Take a look here: http://blogs.msdn.com/astoriateam/archive/2009/09/01/introduction-to-data-binding-in-ctp2.aspx - some description of the data binding support.

    Thanks,
    Vitek Karas [MSFT]
    Friday, December 4, 2009 7:25 PM
    Moderator
  • My problem is : How get the results in OnClientQueryComplete

    Ihis post is for Pratik

    thanks
    Cdes
    Saturday, December 5, 2009 8:17 AM
  • I Have resolved my problem. The following code is good.


     

    private void OnClientQueryComplete(IAsyncResult result)
    {
          Dispatcher.BeginInvoke(() =>
          {
              DataServiceQuery<TabCptClie> query =
                          result.AsyncState
    as DataServiceQuery<TabCptClie>;
              try
              {
               var  returnedClient query.EndExecute(result);
               if (returnedClient != null)
               {
                   foreach (var item in returnedClient)
                   {
                          ClientsBindingCollection.Add(item);
                   }
                   this.ordersGrid.DataContext = ClientsBindingCollection;
                   this.ordersGrid.UpdateLayout();
               }
               }
               catch (DataServiceQueryException ex)
               {
                      this.messageTextBlock.Text = string.Format("Error: {0} -
                                  {1}"
    ,ex.Response.StatusCode.ToString(), ex.Response.Error.Message);
               }
          });
    }


    I Have only replace  TabCptClie returnedClient by var returnedClient


    Thank you to all.
     to Lingzhi Sun , for the link in All-In-One Code Framework, CSADONETDataServiceSL3Client.
     and to Vitek Karas  for ADO.NET Data Services v1.5 CTP2.
     
    AT More
    Desvernes Claude

    • Marked as answer by Cdes Saturday, December 5, 2009 2:26 PM
    Saturday, December 5, 2009 2:25 PM