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,
- New Project
- Click on C#
- Then Select Web
- Asp.net webservice
Write this code (Make sure to use your own
data base connection string and data base element )
Download Source Code
using System;Run this project and you will get a link like, http://localhost:1937/Service1.asmx
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;
}
}
}
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.
- Add service Reference
- 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 )
- Then Press Ok button in Main.xml Page and write the following code.
using System;Now run the project and click the button.
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";
}
}
}
}
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.
I WANT TO ASK A QUESTION
ReplyDeletein which u said write code in xml/ to xml k nechy code likhen ta xml code delet kr k likhu ?
ReplyDeleteob.DataCompleted += new EventHandler(ob_DataCompleted);
ReplyDeleteXDocument xdoc = XDocument.Load(e.Result);
IN ME ERROR ARA HY .. wht to do?