OData URL
I will be using NorthWind OData URL. You can access that URL from the following link.
http://services.odata.org/Northwind/Northwind.svc/
We are going to fetch the Customers table from the NorthWind Database.
Create Project and Add Service Reference
Create a Silverlight project.
Choose Silverlight 4 as the version.
Right-click on the project and add a Service Reference.
In the Address you need to give the URL of the OData. As we discussed in previous steps, here I am giving the OData URL of the NorthWind Database hosted bythe OData.org.
Add following namespaces on MainPage.Xaml.cs page:
Please make sure about the second namespace. Since I have added a service reference for OData with the name ServiceReference1 and the name of the project is ODatainMangoUpdated.
Globally defined following variables:
In the constructor of the MainPage:
- Create instance of NorthWindEntities
- Create instance of DataServiceCollection passing context
- Write the LINQ query.
- Attach event handler LoadCompleted on DataServiceCollection object.
- Fetch the result asynchronously
On the completed event:
- Check whether next page exist or not , if yes load automatically
- Set the DataContext of layout as result.
Design Page and Bind List Box
Here you need to create a ListBox and in the Data Template put three TextBlocks vertically. Bind the columns of the table to the text blocks.
For your reference the full source code is given below. Feel free to use it.
MainPage.Xaml
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="ODATAWithSilverLight.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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<ListBox x:Name="MainListBox" Margin="0,0,-12,0"
ItemsSource="{Binding}"
Height="350"
Width="250"
ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock Text="{Binding Path=CompanyName}"
TextWrapping="NoWrap"
/>
<TextBlock Text="{Binding Path=ContactName}"
TextWrapping="NoWrap"
Margin="12,-6,12,0" />
<TextBlock Text="{Binding Path=Phone}"
TextWrapping="NoWrap"
Margin="12,-6,12,0"
/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
MainPage.Xaml.cs
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 ODATAWithSilverLight.ServiceReference1;
using System.Data.Services.Client;
namespace ODATAWithSilverLight
{
public partial class MainPage : UserControl
{
private NorthwindEntities context;
private readonly Uri ODataUri = new Uri("http://services.odata.org/Northwind/Northwind.svc/");
private DataServiceCollection<Customer> lstCustomers;
public MainPage()
{
InitializeComponent();
context = new NorthwindEntities(ODataUri);
lstCustomers = new DataServiceCollection<Customer>(context);
var result = from r in context.Customers select r;
lstCustomers.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(lstCustomers_LoadCompleted);
lstCustomers.LoadAsync(result);
}
void lstCustomers_LoadCompleted(object sender, LoadCompletedEventArgs e)
{
if (lstCustomers.Continuation != null)
{
lstCustomers.LoadNextPartialSetAsync();
}
else
{
this.LayoutRoot.DataContext = lstCustomers;
}
}
}
}
Run Application
Press F5 to run the application. You should get all the records from the Customer table in the List box.