Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my web service
C#
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.Web.Configuration;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 Service : System.Web.Services.WebService
{
    List<wilaya> Wilayalist = new List<wilaya>();
    List<moughata> Moughataalist = new List<moughata>();
    List<commune> Communelist = new List<commune>();
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

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

    [WebMethod]
    public List<wilaya> GetWilaya()
    {
        // getting connection string
       
        string conStr = WebConfigurationManager.ConnectionStrings["SQLDbConnection"].ConnectionString;
        DataTable dt = new DataTable();
        SqlDataReader dr = null;
        // Creating Sql Connection
        using (SqlConnection conn = new SqlConnection(conStr))
        {
            // Creating insert statement
            string sql = string.Format(@"Select * from [WilayaMast]");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            conn.Open();

            dr = cmd.ExecuteReader();
            dt.Load(dr);
            conn.Close();
            cmd = null;
        }

        int countRow = dt.Rows.Count;

        foreach (DataRow drEmp in dt.Rows)
        {
            Wilaya objwilaya = new Wilaya();
            objwilaya.WCode = Convert.ToInt32(drEmp["Wcode"].ToString());
            objwilaya.WName = drEmp["WName"].ToString();
            objwilaya.WID = drEmp["WID"].ToString();
            Wilayalist.Add(objwilaya);
        }
        return Wilayalist;
    }

    [WebMethod]
    public List<commune> GetCommune()
    {
        // getting connection string

        string conStr = WebConfigurationManager.ConnectionStrings["SQLDbConnection"].ConnectionString;
        DataTable dt = new DataTable();
        SqlDataReader dr = null;
        // Creating Sql Connection
        using (SqlConnection conn = new SqlConnection(conStr))
        {
            // Creating insert statement
            string sql = string.Format(@"Select * from [CommuneMast]");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            conn.Open();

            dr = cmd.ExecuteReader();
            dt.Load(dr);
            conn.Close();
            cmd = null;
        }

        int countRow = dt.Rows.Count;

        foreach (DataRow drcommune in dt.Rows)
        {
            Commune objcommune = new Commune();
            objcommune.CCode = Convert.ToInt32(drcommune["CCode"].ToString());
            objcommune.CName = drcommune["CName"].ToString();
            objcommune.CID = drcommune["CID"].ToString();
            objcommune.MCode = Convert.ToInt32(drcommune["MCode"]);
            Communelist.Add(objcommune);
        }
        return Communelist;
    }

    [WebMethod]
    public List<moughata> GetMoughata()
    {
        // getting connection string

        string conStr = WebConfigurationManager.ConnectionStrings["SQLDbConnection"].ConnectionString;
        DataTable dt = new DataTable();
        SqlDataReader dr = null;
        // Creating Sql Connection
        using (SqlConnection conn = new SqlConnection(conStr))
        {
            // Creating insert statement
            string sql = string.Format(@"Select * from [MoughataaMast]");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            conn.Open();

            dr = cmd.ExecuteReader();
            dt.Load(dr);
            conn.Close();
            cmd = null;
        }

        int countRow = dt.Rows.Count;

        foreach (DataRow drmoughata in dt.Rows)
        {
            Moughata objmoughata = new Moughata();
            objmoughata.MCode = Convert.ToInt32(drmoughata["MCode"].ToString());
            objmoughata.MID = drmoughata["MID"].ToString();
            objmoughata.WCode = Convert.ToInt32(drmoughata["WCode"].ToString());
            objmoughata.MName = drmoughata["MName"].ToString();
            Moughataalist.Add(objmoughata);
        }
        return Moughataalist;
    }
}

I want to show data returned by this method into a list in android.
Posted
Updated 23-Jan-15 3:29am
v3

1 solution

Two things.

1) Change your Web Service code so that your WebMethod will return data in JSON format.

http://stackoverflow.com/questions/19563641/how-to-get-json-response-from-a-3-5-asmx-web-service

2) Then call your SOAP Web service in Android application as below.

Calling ASP.NET Webservice (ASMX) from an Android Application, the Simplest Way
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900