silverlightdata-bindingtelerikopenaccess

Openaccess: How to get data from DB


Silverlight newbie here, trying to do a simple databinding:

Im trying to get a list of all users in a test db, but i cant; Im using Openaccess and followed all tutorials on how to create a Domain Model, then a Domain Service; Im having problems executing the following code, which returns always 0 even though i have 200 lines on db:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.DomainServices.Client;
using SLTest.Web;

namespace SLTest {
    public partial class MainPage : UserControl  {

        private TestDomainContext link = new TestDomainContext();



        public MainPage() {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e) {

            int count = 0;
            LoadOperation<DomainUser> loadOperation = link.Load(link.GetDomainUsersQuery());
            loadOperation.Completed += (s, a) =>{
                theList.ItemsSource = link.DomainUsers;
                count = link.DomainUsers.Count();
            };

        }
    }
}

Also, why is that simply doing

int count = link.DomainUsers.Count();

doesnt work as it does in ASP.NET?

Here is the XAML:

<UserControl x:Class="SLTest.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

    <Grid x:Name="theGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <ListBox Grid.Row="0" Grid.Column="0" x:Name="theList">
            <DataTemplate>
                <TextBlock Text="{Binding FullName}"></TextBlock>
            </DataTemplate>
        </ListBox>
    </Grid>
</UserControl>

Solution

  • I am guessing that the link object is your actual domain service and as such you should not be addressing it directly in the callback method as all query operations in your service are asynchronous and will not yield any result of addressed that way. Instead you can use the asynchronous callback methods arguments to get the information you are requesting like that:

      loadOperation.Completed += (s, a) =>
            {
                LoadOperation<DomainUser> loadedObjects = (LoadOperation<DomainUser>)s;
                theList = loadedObjects.Entities;
                count = loadedObjects.Entities.Count();
            };
    

    That should work.