Return multiple values from web service

Discussion in 'ASP .Net Web Services' started by Shawn Cutter, Jun 9, 2005.

  1. Shawn Cutter

    Shawn Cutter Guest

    Background:
    I am trying to return a list of ID's to the client requesting the web
    service. The list of ID's I want to return is a list of all relating
    ID's to the passed in PersonID.

    How it currently functions:
    Web Service (Server):
    I have a basic web service that receives a PersonID. The webservice
    then go to the DB to find all relating ID's. Currently I have the
    result stored in a dataset. This however may be overkill since the
    dataset consists of one datatable, which contains one column.

    Client:
    The client is a simple .NET page that uses the following to connect to
    the webservice. proxy.PERSONREQUEST(PersonID)

    I have successfully filled an array and can return one value from the
    array using ( txtResult.Text = proxy.PERSONREQUEST(PersonID) ). How
    can I return the entire result to the client all at once? I would like
    to return it in the format of:

    <PERSONRESULT>
    <ID>ADS34579354</<ID>
    <<ID>ASR34579354</<ID>
    <<ID>TYU34579354</<ID>
    </PERSONRESULT>

    Am I approaching this whole thing wrong in using a dataset to fill an
    array? Do I have to do something different on the client side so that
    it knwo how to receive the list of values.

    Any help would be appreciated.

    Thanks,
    Shawn
     
    Shawn Cutter, Jun 9, 2005
    #1
    1. Advertising

  2. Shawn Cutter

    hellrazor Guest

    "Shawn Cutter" <> wrote in
    news::

    > Background:
    > I am trying to return a list of ID's to the client requesting the web
    > service. The list of ID's I want to return is a list of all relating
    > ID's to the passed in PersonID.
    >
    > How it currently functions:
    > Web Service (Server):
    > I have a basic web service that receives a PersonID. The webservice
    > then go to the DB to find all relating ID's. Currently I have the
    > result stored in a dataset. This however may be overkill since the
    > dataset consists of one datatable, which contains one column.
    >
    > Client:
    > The client is a simple .NET page that uses the following to connect to
    > the webservice. proxy.PERSONREQUEST(PersonID)
    >
    > I have successfully filled an array and can return one value from the
    > array using ( txtResult.Text = proxy.PERSONREQUEST(PersonID) ). How
    > can I return the entire result to the client all at once? I would like
    > to return it in the format of:
    >
    > <PERSONRESULT>
    > <ID>ADS34579354</<ID>
    > <<ID>ASR34579354</<ID>
    > <<ID>TYU34579354</<ID>
    > </PERSONRESULT>
    >
    > Am I approaching this whole thing wrong in using a dataset to fill an
    > array? Do I have to do something different on the client side so that
    > it knwo how to receive the list of values.
    >
    > Any help would be appreciated.
    >
    > Thanks,
    > Shawn
    >
    >


    Here's the simplest way to accomplish what you want. Have your webservice
    return a simple string array:

    [WebMethod]
    public string[] getPersonIds(string Id)
    {
    //substitute code to actually populate the array with the dataset data
    string[] personIds = {"ADS34579354", "ASR34579354", "TYU34579354"};
    return personIds;
    }

    In the client:

    //testWs = proxy object.
    TestWs.TestWS testWs = new TestWs.TestWS();
    string[] personIds = testWs.getPersonIds();
    foreach (string personId in personIds)
    {
    Console.WriteLine(personId);
    }


    If you actually want to return an object called "PersonResult" with named
    instances of "ID" then here's what you do:

    in your WebServices code:

    create a class called "PersonResult", with the one single "Id" member,
    declared as a primitive array:


    class PersonResult
    {
    public string[] Id;
    }


    and your [WebMethod] function should look like this:

    [WebMethod]
    public PersonResult getPersonIds()
    {
    PersonResult personResult = new PersonResult();
    personResult.Ids.Add("ADS34579354");
    personResult.Ids.Add("ASR34579354");
    personResult.Ids.Add("TYU34579354");
    return personResult;
    }

    in the client, here's your code to obtain the instance of "PersonResult"
    from the webservice:

    TestWs.TestWS testWs = new TestWs.TestWS();
    //define a variable of type TestWs.PersonResult, as defined by the proxy
    class
    TestWs.PersonResult personResult = testWs.getPersonIds();
    foreach (string personId in personResult.Ids)
    {
    Console.WriteLine(personId);
    }


    Hope that helps!

    Jorge
     
    hellrazor, Jun 10, 2005
    #2
    1. Advertising

  3. Shawn Cutter

    hellrazor Guest

    hellrazor <> wrote in
    news:Xns9670ABAABDF0Cjorgeanotherworldcom@207.46.248.16:

    > "Shawn Cutter" <> wrote in
    > news::
    >
    >> Background:
    >> I am trying to return a list of ID's to the client requesting the web
    >> service. The list of ID's I want to return is a list of all relating
    >> ID's to the passed in PersonID.
    >>
    >> How it currently functions:
    >> Web Service (Server):
    >> I have a basic web service that receives a PersonID. The webservice
    >> then go to the DB to find all relating ID's. Currently I have the
    >> result stored in a dataset. This however may be overkill since the
    >> dataset consists of one datatable, which contains one column.
    >>
    >> Client:
    >> The client is a simple .NET page that uses the following to connect
    >> to the webservice. proxy.PERSONREQUEST(PersonID)
    >>
    >> I have successfully filled an array and can return one value from the
    >> array using ( txtResult.Text = proxy.PERSONREQUEST(PersonID) ). How
    >> can I return the entire result to the client all at once? I would
    >> like to return it in the format of:
    >>
    >> <PERSONRESULT>
    >> <ID>ADS34579354</<ID>
    >> <<ID>ASR34579354</<ID>
    >> <<ID>TYU34579354</<ID>
    >> </PERSONRESULT>
    >>
    >> Am I approaching this whole thing wrong in using a dataset to fill an
    >> array? Do I have to do something different on the client side so
    >> that it knwo how to receive the list of values.
    >>
    >> Any help would be appreciated.
    >>
    >> Thanks,
    >> Shawn
    >>
    >>

    >
    > Here's the simplest way to accomplish what you want. Have your
    > webservice return a simple string array:
    >
    > [WebMethod]
    > public string[] getPersonIds(string Id)
    > {
    > //substitute code to actually populate the array with the dataset
    > data string[] personIds = {"ADS34579354", "ASR34579354",
    > "TYU34579354"}; return personIds;
    > }
    >
    > In the client:
    >
    > //testWs = proxy object.
    > TestWs.TestWS testWs = new TestWs.TestWS();
    > string[] personIds = testWs.getPersonIds();
    > foreach (string personId in personIds)
    > {
    > Console.WriteLine(personId);
    > }
    >
    >
    > If you actually want to return an object called "PersonResult" with
    > named instances of "ID" then here's what you do:
    >
    > in your WebServices code:
    >
    > create a class called "PersonResult", with the one single "Id" member,
    > declared as a primitive array:
    >
    >
    > class PersonResult
    > {
    > public string[] Id;
    > }
    >
    >
    > and your [WebMethod] function should look like this:
    >
    > [WebMethod]
    > public PersonResult getPersonIds()
    > {
    > PersonResult personResult = new PersonResult();
    > personResult.Ids.Add("ADS34579354");
    > personResult.Ids.Add("ASR34579354");
    > personResult.Ids.Add("TYU34579354");
    > return personResult;
    > }
    >
    > in the client, here's your code to obtain the instance of
    > "PersonResult" from the webservice:
    >
    > TestWs.TestWS testWs = new TestWs.TestWS();
    > //define a variable of type TestWs.PersonResult, as defined by the
    > proxy class
    > TestWs.PersonResult personResult = testWs.getPersonIds();
    > foreach (string personId in personResult.Ids)
    > {
    > Console.WriteLine(personId);
    > }
    >
    >
    > Hope that helps!
    >
    > Jorge
    >
    >


    whoops.. made a mistake.. the PersonResult class should read as follows:

    public class PersonResult
    {
    public ArrayList Ids = new ArrayList();
    }


    ArrayList is cast down to string[] or object[]

    JOrge
     
    hellrazor, Jun 10, 2005
    #3
  4. Shawn Cutter

    Guest

    Thanks alot Jorge. You cleared everything up for me.

    Worked like a charm

    Shawn
     
    , Jun 10, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. scottrm

    how return list values from web service

    scottrm, Jan 5, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    389
    Erymuzuan Mustapa
    Jan 5, 2005
  2. Multiple web service calls from web service

    , Jan 5, 2006, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    197
    Rodrigo García
    Jan 9, 2006
  3. DonaldN
    Replies:
    1
    Views:
    583
    Josh Twist
    Feb 17, 2006
  4. AlBruAn

    Web Service doesn't return needed values

    AlBruAn, Aug 17, 2007, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    154
    AlBruAn
    Aug 17, 2007
  5. Leo Violette
    Replies:
    0
    Views:
    1,099
    Leo Violette
    Apr 17, 2009
Loading...

Share This Page