SIGN UP MEMBER LOGIN:    
ARTICLE

Windows Phone 7 Data Binding using WCF Service

Posted by Raj Kumar Articles | Windows Phone in C# January 28, 2011
This is my first article on Windows Phone 7. This article is going to explain how to fetch data from SQL database using WCF service and show on a page in a Windows Phone 7 application.
Reader Level:
 


This is my first article on Windows Phone 7. This article is going to explain how to fetch data from SQL database using WCF service and show on a page in a Windows Phone 7 application.

Getting Started

Creating a Silverlight Application:

  • Open Visual Studio 2010.
  • Go to File => New => Project.
  • Select Silverlight Windows Phone from the Installed templates and choose the Windows Phone Application.
  • Enter the Name and choose the location.
  • Click OK.


img1.jpg

Image1.

Before proceeding further now make a new WCF Service Application like image 2.

img2.jpg

Click OK.

Click on Add New Item tab and add a Linq To Sql Classes data template and configure database.

img3.jpg

Image3.

Drag and drop database tables.

img4.jpg

Image 4.

You can rename table name if you want.

img5.jpg

Image 5.

Now let's start work on service part.

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace RajService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
publicinterfaceIService1
    {
        [OperationContract]
List<tbl_Rep_Profile> FindModelByModelId(int modelid);
 
        [OperationContract]
List<tbl_Rep_Profile> GetAllModels();
    }
}


Service1.svc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace RajService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
publicclassService1 : IService1
    {
publicList<tbl_Rep_Profile> FindModelByModelId(int modelid)
        {
DataClasses1DataContext context = newDataClasses1DataContext();
var res = from r in context.tbl_Rep_Profiles where r.UserId == modelid select r;
return res.ToList();
        }

publicList<tbl_Rep_Profile> GetAllModels()
        {
DataClasses1DataContext context = newDataClasses1DataContext();
var res = from r in context.tbl_Rep_Profiles
select r;
return res.Take(5).ToList();
        }
    }
}


Click on View in Browser to see WCF service is working fine or not.

img6.jpg

Image 6.

img7.jpg

Image 7.

Now add service reference in application using click of Add Service Reference.

img8.jpg

Image 8.

We are done with service work here; now to do the pages work.

MainPage.xaml

<Grid x:Name="LayoutRoot" Background="Transparent">
<
Grid.RowDefinitions>
<
RowDefinition Height="*"/>
</
Grid.RowDefinitions>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<
ListBox Height="650" HorizontalAlignment="Left" Margin="11,17,0,0" Name="listBox1" VerticalAlignment="Top" Width="434" >
<
ListBox.ItemTemplate>
<
DataTemplate>
<
StackPanel Orientation="Horizontal">
<
Image Source="{Binding Rep_Img_Name}" Width="150" Stretch="Uniform" HorizontalAlignment="Center" />
<
TextBlock Text="{Binding UserId}"/>
<
TextBlock Text="{Binding Rep_Title}"/>
<
TextBlock Text="{Binding Rep_FirstName}"/>
<
TextBlock Text="{Binding Rep_Email}"/>
</
StackPanel>
</
DataTemplate>
</
ListBox.ItemTemplate>
</
ListBox>

</Grid>
</
Grid>

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 Microsoft.Phone.Controls;
using WindowsPhoneApplication1.ServiceReference1;

namespace WindowsPhoneApplication1
{
publicpartialclassPage2 : PhoneApplicationPage
    {
public Page2()
        {
            InitializeComponent();

Service1Client svc = newService1Client();
            svc.GetAllModelsCompleted += newEventHandler<GetAllModelsCompletedEventArgs>(svc_GetAllModelsCompleted);
            svc.GetAllModelsAsync();
        }

void svc_GetAllModelsCompleted(object sender, GetAllModelsCompletedEventArgs e)
        {
            listBox1.ItemsSource = e.Result;
        }
    }
}


Now

img9.jpg

Image 9.

So we are done here with WCF service using Windows Phone 7. Any question or comments are most welcome just drop me a line in c-sharpcorner comments section.

Login to add your contents and source code to this article
share this article :
post comment
 

That means your wcf service is not fetching data from database...make sure your service has data after that update the refrence or add service refrence again.

Posted by Raj Kumar Sep 05, 2011

Hi Raj Kumar, i've downloaded the solutions of the example, but the thing is, i cant seems to make it work. I've tried to edit some parts and make it to my own. The thing is, after i run the program, listbox shows nothing

Posted by ricky tan Jul 27, 2011

the tutorial is really great, i have a question, how can we edit the data in the listbox and then submit to the database?

Posted by GERASIMOS VONITSANOS Apr 08, 2011

Thanks buddy

Posted by Raj Kumar Feb 08, 2011

Nice article

Posted by Dhananjay Kumar Feb 08, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor