- Back to Home »
- Windows Phone apps »
- Windows Phone 8 C# code to consume asmx webservice with ADO.NET code
Posted by :
Sudhir Chekuri
Saturday, 23 November 2013
I added web service in references of my windows phone 8 app.
DbakingsReference is the ServiceName i gave. This is used as namespace in C# code.
RechargePlansService is the class inside my webservice.
GetData is the web method i created in webservice which returns data in form of a user defined type named data.
//return type
DbakingsReference is the ServiceName i gave. This is used as namespace in C# code.
RechargePlansService is the class inside my webservice.
GetData is the web method i created in webservice which returns data in form of a user defined type named data.
Here is the code in web service asmx file containing web method that returns data which is retreived from sql server database using ADO.NET
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class RechargePlansService : System.Web.Services.WebService {
public RechargePlansService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconnection"].ConnectionString);
[WebMethod]
public data GetData()
{
data d = new data(); ;
SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
int i = 0;
List<string> l1 = new List<string>();
for ( i = 0; i < ds.Tables[0].Rows.Count; i++)
{
l1.Add( ds.Tables[0].Rows[i][0].ToString() + "," + ds.Tables[0].Rows[i][1].ToString() );
}
d.l = l1;
return d;
}
//return type
public class data
{
public List<string> l;
}
}
Code in Windows phone app to display data retreived from web service in a message box
private void Btn1_Click(object sender, EventArgs e)
{
DbakingsReference.RechargePlansServiceSoapClient MyClient = new DbakingsReference.RechargePlansServiceSoapClient();
MyClient.GetDataCompleted += MyClient_GetDataCompleted;
MyClient.GetDataAsync();
}
private void MyClient_GetDataCompleted(object sender, DbakingsReference.GetDataCompletedEventArgs e)
{
DbakingsReference.data d= e.Result;
MessageBox.Show ( d.l[0]);
}