Search code examples
.netwpfwcf-ria-services

How to bind WCF RIA Services query entity to the WPF label?


I really want to know why in the first case I can bind data to the DataGrid and in the second case it doesn't work at all. Thank you for any clue!!!

Here is the code:

        Guid id = Guid.Parse("BCBBF129-30ED-400B-9D63-403ED79FDFC7");
        EntityQuery<Alert> alert = this._preDomainContext.GetAlertQuery(id);  
        LoadOperation<Alert> l = this._preDomainContext.Load(alert);
        dataGrid1.ItemsSource = l.Entities; // <- It works fine

        label1.Content = l.Entities.ElementAt(0).Message; // It gives an error

And the XAML:

<sdk:DataGrid   AutoGenerateColumns="True" Height="100" 
HorizontalAlignment="Left" Margin="173,154,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="120" />
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="194,114,0,0" Name="label1" VerticalAlignment="Top" Width="120" />

And error is:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Oh! On both cases when I debug I cannot see any values for the l.Entities and as well as l.Entities.ElementAt(0).Message. But when page appears under the DataGrid I can see the data but at the same time I get the error above...

I am not sure... But is it possible to see the data before when a page appears? It's really uncomfortable don't see any data at all...

UPDATES:

    [RequiresAuthentication]
    [EnableClientAccess()]
    public class PreDomainService : LinqToEntitiesDomainService<PreEntities>
    {


    public  Alert GetAlert(Guid id)
        {
            return this.ObjectContext.Alerts.Where(a=>a.ID == id).FirstOrDefault();
        }

Solution

  • I would assume that your Entities collection is empty.