Return multiple values from web service

S

Shawn Cutter

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
 
H

hellrazor

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
 
H

hellrazor

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top