Subscribe For Free Updates!

We'll not spam mate! We promise.

Feb 20, 2012

Call A WebService From Windows Phone 7

Views:

Today,I will show you how to Call A Web Service hosted on a live server or local machine from Windows Phone 7.

First We create a Service,
  1. New Project
  2. Click on C#
  3. Then Select Web
  4. Asp.net webservice
If You have any confusion in creating a web service,please see video at the bottom of this post which will show you how to create web service.

Write this code (Make sure to use your own
data base connection string and data base element )

Download Source Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.Linq;

namespace WebService1
{
///

/// Summary description for Service1
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}

[WebMethod]
public string Data()
{
SqlConnection sqlcon = new SqlConnection("Data Source=CE-HP;Initial Catalog=BegVCSharpEvents;Integrated Security=True");
sqlcon.Open();
SqlCommand comm = new SqlCommand("Select * from Events", sqlcon);

DataSet ds = new DataSet("Dataset");
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(ds, "DataTable");

ds.WriteXml("C:\\TRYXML.xml");

XmlDataDocument xddoc = new XmlDataDocument(ds);

return xddoc.InnerXml;
}
}
}

Run this project and you will get a link like, http://localhost:1937/Service1.asmx
Please do not close the browser and do not stop the service.
Now create a windows Phone 7 Application.
Open Main.Xml Page.

Rigth click on  Reference.

  1. Add service Reference
  2. Enter URL (http://localhost:1937/Service1.asmx) in address,Now press the go button,It will show you a service and its method (make sure your local host service is open ) 
  3. Then Press Ok button in Main.xml Page and write the following code.
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 System.Xml.Linq;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using WindowsPhoneApplication1.ServiceReference1;

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

private void btn1_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.Service1SoapClient ob = new ServiceReference1.Service1SoapClient();
ob.DataAsync();
ob.DataCompleted += new EventHandler(ob_DataCompleted);


}

void ob_DataCompleted(object sender, ServiceReference1.DataCompletedEventArgs e)
{

TextReader reader = new StringReader(e.Result);

XDocument xdoc = XDocument.Load(e.Result);

var events = from event1 in xdoc.Descendants("DataTable")
select new
{
Id = event1.Element("Id").Value
};
foreach (var item in events)
{
textBlock1.Text += item.Id + "\n";
}

}
}
}
Now run the project and click the button.
then the textblock will show you the data which comes from web service.

Download Source Code

This video will demonstrate you how to create Asp.net web service.

Plese Feel Free to Socializer This Post
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

3 comments:

  1. I WANT TO ASK A QUESTION

    ReplyDelete
  2. in which u said write code in xml/ to xml k nechy code likhen ta xml code delet kr k likhu ?

    ReplyDelete
  3. ob.DataCompleted += new EventHandler(ob_DataCompleted);

    XDocument xdoc = XDocument.Load(e.Result);

    IN ME ERROR ARA HY .. wht to do?

    ReplyDelete

Become a Fan

visual studio learn