---
title: "Data Binding in Windows Phone 7 Using WCF Service"  
description: "This article is going to explain how to bind data in Windows Phone 7 using WCF Service. Let’s see a brief demonstration on it."  
author: "Anonymous User"  
published: 2012-04-18  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/926/data-binding-in-windows-phone-7-using-wcf-service  
category: "windows phone"  
tags: ["windows phone"]  
reading_time: 8 minutes  

---

# Data Binding in Windows Phone 7 Using WCF Service

This article is going to explain how to [bind data](https://www.mindstick.com/forum/415/how-to-bind-data-in-windows-phone-7-using-wcf-service) in Windows Phone 7 using WCF Service. Let’s see a brief demonstration on it.\

##### Getting Started:

1. Open [Visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available)

2. Create new [Silverlight](https://www.mindstick.com/blog/54/silverlight-in-asp-dot-net) Windows 7 Phone [Development Project](https://www.mindstick.com/blog/11637/how-to-choose-the-right-web-designer-for-your-web-design-development-project)

3. Enter [your project](https://www.mindstick.com/forum/156583/best-way-to-link-bootstrap-file-in-your-project) Name

4. Click on button ‘Ok’

![Data binding in Windows Phone 7 using WCF Service](https://www.mindstick.com/mindstickarticle/b0046214-afb8-41c1-8a7f-3250a3b663ca/images/a9f7408a-196a-4a78-aff3-a9f314bad480.png)

Now drag and drop TextBlock and Button control in MainPage.xaml user interface design and made following layout by setting some property too.

![Data binding in Windows Phone 7 using WCF Service](https://www.mindstick.com/mindstickarticle/b0046214-afb8-41c1-8a7f-3250a3b663ca/images/11c86c30-2657-43a4-9a48-a6004337acec.png)

Now make WCF service which returns [data from database](https://www.mindstick.com/forum/34370/how-to-populate-google-charts-using-data-from-database-created-via-data-first-asp-dot-net-mvc). Here I’m just retrieving user information from the database on the basis of user email id.

To create WCF service, follow up some following step.

Step: Open Visual Studio and go to File-> New-> Project.

![Data binding in Windows Phone 7 using WCF Service](https://www.mindstick.com/mindstickarticle/b0046214-afb8-41c1-8a7f-3250a3b663ca/images/c1c9e4cb-091f-4000-b26e-5e3f7fcac654.png)

Now click on button ‘Ok’ and write down following code.

##### Code of 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;
using System.Data;
 
namespace WcfService2
{
    // 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]
    public interface IService1
    {
 
        [OperationContract]
        string GetData(intvalue);
        /// <summary>
        /// Method to retrieve information from database for particular email id.
        /// </summary>
        /// <param name="eMail">EmailId of that person for which you would take information</param>
        /// <returns> string type xml data</returns>
        [OperationContract]
        [WebGet(UriTemplate="data/{value}")]
       string GetLoginInformation(stringeMail);
 
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeTypecomposite);
 
        // TODO: Add your service operations here
    }
 
 
    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue=true;
        string stringValue="Hello ";
 
        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue= value; }
        }
 
        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue= value; }
        }
    }
}
```

##### Code of Service1.svc.cs:

```
using System;
using System.Data;
using System.Data.SqlClient;
 
namespace WcfService2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public string GetData(intvalue)
        {
            return string.Format("You entered: {0}", value);
        }
 
        public string GetLoginInformation(stringeMail)
        {
            SqlConnection con=new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
            con.Open();
            string queryString= string.Format("select * from tblLogin where EmailId = '{0}'", eMail);
            SqlCommand cmd= new SqlCommand(queryString, con);
            // SqlDataReader dr = cmd.ExecuteReader();
            SqlDataAdapter da=new SqlDataAdapter(cmd);
            DataSet ds= newDataSet();
            da.Fill(ds);
            DataTable dt= newDataTable();
            string xmlData=ds.GetXml();
            da.Dispose();
            con.Close();
            return xmlData;
        }
 
        public CompositeType GetDataUsingDataContract(CompositeTypecomposite)
        {
            if (composite== null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue+="Suffix";
            }
            return composite;
        }
    }
}
```

Now start recently created WCF Service.

![Data binding in Windows Phone 7 using WCF Service](https://www.mindstick.com/mindstickarticle/b0046214-afb8-41c1-8a7f-3250a3b663ca/images/298f645a-467a-4d0c-bf78-f9d1f1d0a37f.png)

Now click on **‘Service1.svc’** link.

![Data binding in Windows Phone 7 using WCF Service](https://www.mindstick.com/mindstickarticle/b0046214-afb8-41c1-8a7f-3250a3b663ca/images/6aa2e389-6475-4166-aa9c-76b1b9100b27.png)

Now add this WCF Service in your DataBindDemo project. To add WCF service in windows phone 7 applications follows the following step.

Step: Right click on project name; select ‘Add Service Reference’ options and following wizard’s instruction.

![Data binding in Windows Phone 7 using WCF Service](https://www.mindstick.com/mindstickarticle/b0046214-afb8-41c1-8a7f-3250a3b663ca/images/045a8895-7b0d-44e5-b5ab-4ea2e1ef28c0.png)

##### Now following window will be appearing; fill appropriate information in text fields and click on button ‘Ok’.

![Data binding in Windows Phone 7 using WCF Service](https://www.mindstick.com/mindstickarticle/b0046214-afb8-41c1-8a7f-3250a3b663ca/images/f062bad4-7d0d-455f-a1f7-31f4529a4d51.png)

![Data binding in Windows Phone 7 using WCF Service](https://www.mindstick.com/mindstickarticle/b0046214-afb8-41c1-8a7f-3250a3b663ca/images/499c14ab-9e20-4d48-a73e-6802ba1ab289.png)

Now WCF service added successfully in windows phone 7 [applications](https://www.mindstick.com/articles/12847/how-to-choose-the-right-ethernet-cable-for-industrial-applications). Write down following code;

##### Code of MainPage.xaml:

```
<!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="My Application" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="DataBindingDemo" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" FontSize="36" />
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" DataContext="{Binding}">
            <Button Content="Invoke WCF Service" Height="72" HorizontalAlignment="Left" Margin="124,583,0,0" Name="button1" VerticalAlignment="Top" Width="302" Click="button1_Click" />
            <TextBlock Height="571" HorizontalAlignment="Left" Margin="12,6,0,0" Name="txtBlockContent" Text="" VerticalAlignment="Top" Width="438" />
        </Grid>
    </Grid>
```

##### \

##### Code of MainPage.xaml.cs:

#####

```
using System;
using System.Windows;
using Microsoft.Phone.Controls;
using DataBindDemo.ServiceReference1;


namespace DataBindDemo
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // GetInfo();

        }
        public void GetInfo()
        {

            ServiceReference1.Service1Clientproxy=newServiceReference1.Service1Client();
            proxy.GetLoginInformationAsync("amitsingh@xyz.com");
            proxy.GetLoginInformationCompleted+=newEventHandler<ServiceReference1.GetLoginInformationCompletedEventArgs>(proxy_GetLoginInformationCompleted);

        }

        void proxy_GetLoginInformationCompleted(objectsender, ServiceReference1.GetLoginInformationCompletedEventArgse)
        {
            // Bind Data from WCF Service
            txtBlockContent.Text=e.Result;

        }

        private void button1_Click(objectsender, RoutedEventArgse)
        {

            Service1Client proxyClient=new Service1Client();
            proxyClient.GetLoginInformationAsync("amitsingh@xyz.com");
            proxyClient.GetLoginInformationCompleted+= new EventHandler<ServiceReference1.GetLoginInformationCompletedEventArgs>(proxy_GetLoginInformationCompleted);
        }
    }
}
```

##### \

##### Code of ServiceReferences.ClientConfig:

```
<configuration>

    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1"maxBufferSize="2147483647"
                    maxReceivedMessageSize="2147483647">
                    <security mode="None" />
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:51641/Service1.svc"binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1"contract="ServiceReference1.IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>
</configuration>
```

##### \

##### Now execute DataBindDemo application; following output will be appearing after executing program.

![Data binding in Windows Phone 7 using WCF Service](https://www.mindstick.com/mindstickarticle/b0046214-afb8-41c1-8a7f-3250a3b663ca/images/991384dd-78dd-43a9-a3a5-f65c1ab6babd.png)

To retrieve data from database call WCF Service, to perform it click on button **‘Invoke WCF Service’**.

![Data binding in Windows Phone 7 using WCF Service](https://www.mindstick.com/mindstickarticle/b0046214-afb8-41c1-8a7f-3250a3b663ca/images/da2e5754-89b2-4419-9b03-a5f8a9aff525.png)

Returned data will be displayed on screen with the help of TextBlock control. This is the complete description of [Data Binding](https://www.mindstick.com/articles/337466/exploring-data-binding-in-mvc-how-it-connects-model-and-view) concept in Windows Phone 7 development using WCF service. I hope you will enjoy it and I’m very thankful to you for reading this article.

---

Original Source: https://www.mindstick.com/articles/926/data-binding-in-windows-phone-7-using-wcf-service

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
