470,596 Members | 1,613 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

beginner: nned to change webservice return type

I have had a look around but I'm either not finding or not seeing the
answer to this, what i want to do is reurn the results as an array, or
some other worldly useful format that flash mx2004 can read. But when
I did thiss I was told datasets were the way to go, but needs an
array, prefer typed as were changing it.

Here is the current code: (shortened for readability)

[WebService(Namespace = "http://microsoft.com/webservices/", Name =
"Sample", Description = "Sample")]
public class WebService1{
public WebService1(){
}
private DataSet getDataStreams(string sqlQuery){
System.Data.OleDb.OleDbConnection oleConn1 = new
System.Data.OleDb.OleDbConnection();
oleConn1.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\db\\database.mdb";
OleDbDataAdapter da = new OleDbDataAdapter(sqlQuery,
oleConn1);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
//as you see, I set up and return the dataset, but I either
want to change the way this is built, so it just creates the array
here, or I want a way to convert this dataset, to a typed array. Even
field by field is acceptable at this point.
}
[WebMethod (Description="Gets a list as a dataset")]
public DataSet getOrderData(){
return getDataStreams("SELECT * from table1");
}
}

I don't want someone to do this for me, just point me in the right
direction, unless it's just a couple of lines, then please help. make
it a little quicker.
Any help appreciated.
Reagrds
Shaine
Nov 16 '05 #1
7 5570
Shaine,

In this case, you will have to cycle through the results yourself, and
return the array.

I think you can still use a data set, as I believe that flash will allow
you to process XML. If not, then using an array or something of that sort
is the best way to go. In this case, you might want to return a two
dimensional array (if the types are the same).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Shaine Fisher" <sh**********@madasafish.com> wrote in message
news:aa**************************@posting.google.c om...
I have had a look around but I'm either not finding or not seeing the
answer to this, what i want to do is reurn the results as an array, or
some other worldly useful format that flash mx2004 can read. But when
I did thiss I was told datasets were the way to go, but needs an
array, prefer typed as were changing it.

Here is the current code: (shortened for readability)

[WebService(Namespace = "http://microsoft.com/webservices/", Name =
"Sample", Description = "Sample")]
public class WebService1{
public WebService1(){
}
private DataSet getDataStreams(string sqlQuery){
System.Data.OleDb.OleDbConnection oleConn1 = new
System.Data.OleDb.OleDbConnection();
oleConn1.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\db\\database.mdb";
OleDbDataAdapter da = new OleDbDataAdapter(sqlQuery,
oleConn1);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
//as you see, I set up and return the dataset, but I either
want to change the way this is built, so it just creates the array
here, or I want a way to convert this dataset, to a typed array. Even
field by field is acceptable at this point.
}
[WebMethod (Description="Gets a list as a dataset")]
public DataSet getOrderData(){
return getDataStreams("SELECT * from table1");
}
}

I don't want someone to do this for me, just point me in the right
direction, unless it's just a couple of lines, then please help. make
it a little quicker.
Any help appreciated.
Reagrds
Shaine

Nov 16 '05 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:<#x**************@TK2MSFTNGP14.phx.gbl>...
Shaine,

In this case, you will have to cycle through the results yourself, and
return the array.


Sounds a bit numb, but how would I do that, the best way?
Nov 16 '05 #3
I can't follow this entire post, but I think you are asking for a way to return
a collection of custom types in a web service method... There are many
ways to do this, but I'll demonstrate the one it appears as if everyone is
talking about.

ArrayList list = new ArrayList(); // Using this becuase I don't know the count
of items here.
using(SqlConnection conn = new SqlConnection(...)) {
SqlCommand comm = new SqlCommand(..., conn);

conn.Open();
using(SqlDataReader dr = comm.ExecuteReader()) {
while(dr.Read()) {
list.Add(MyObject.Fill(dr));
}
dr.Close();
}
}
return (MyObject[]) list.ToArray(typeof(MyObject));

The above assumes a couple of things. First, MyObject has a static method named
Fill that
creates and fills an instance of MyObject based on the current state in the
SqlDataReader.
This is a fairly standard approach. Two, we are assuming that you don't know the
number of
objects in advance, and so I'm giving you the option of using an ArrayList...

For performance, you'd want to try and get the count. Every command in Sql
returns a
@@ROWCOUNT, and you can return that in an output parameter to preallocate your
array.

Another option for peformance is to forget count and return a fixed size array
where you
only fill those element slots you care about. The rest can be null, and in turn
get thrown
across the SOAP layer as nil objects. This isn't a huge increase in bandwidth
usage, but
you have to always return an array that is as large as the largest possible
array you might
return, OR, return the results paged so that if you overwhelm the array, return
that array
and tell the client there is more data.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Shaine Fisher" <sh**********@hotmail.com> wrote in message
news:3b**************************@posting.google.c om...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:<#x**************@TK2MSFTNGP14.phx.gbl>...
Shaine,

In this case, you will have to cycle through the results yourself, and
return the array.


Sounds a bit numb, but how would I do that, the best way?

Nov 16 '05 #4
> I think you can still use a data set, as I believe that flash will allow
you to process XML. If not, then using an array or something of that sort
is the best way to go. In this case, you might want to return a two
dimensional array (if the types are the same).

Thanks for the reply.
Flash mx2004 cannot use a dataset as a source with the webservice
connector, but flash remoting apparantly will, but this is overboard
at the moment.
Using google/deja today so a bit slow, but is there a quick clean way
to process the dataset (100 records, 48 fields in each) to a typed
array, a link to a tutorial, or sample code would be great, I'm not
not scared of learning, just a little worried that this small job is
taking so long to complete.

So if anyone can show me this, please help
Reagards
Shaine
Nov 16 '05 #5

"Justin Rogers" <Ju****@games4dotnet.com> wrote in message
news:uG**************@TK2MSFTNGP14.phx.gbl...
I can't follow this entire post, but I think you are asking for a way to
return
a collection of custom types in a web service method... There are many
ways to do this, but I'll demonstrate the one it appears as if everyone is
talking about.

ArrayList list = new ArrayList(); // Using this becuase I don't know the
count of items here.
using(SqlConnection conn = new SqlConnection(...)) {
SqlCommand comm = new SqlCommand(..., conn);

conn.Open();
using(SqlDataReader dr = comm.ExecuteReader()) {
while(dr.Read()) {
list.Add(MyObject.Fill(dr));
}
dr.Close();
}
}
return (MyObject[]) list.ToArray(typeof(MyObject));

Thanks for this, I'll work out the finer details of it for Access 2000 (i
know but what can you do) just a quick quiz though do I have to use
conn.open and conn.close for access, I have not used it in the code i
provded, but is there a reason you have of is it an sql specific thing.

This way also removes the need for the dataset, is there a way I could just
go through the dataset records and add them to an array of the dataset
length. With just an additoin to the code instead of rewriting the whoile
thing.

Thanks for your help.
Reagrds
Shaine
Nov 16 '05 #6

"Shaine Fisher" <sh**********@hotmail.com> wrote in message
news:3b**************************@posting.google.c om...
I think you can still use a data set, as I believe that flash will
allow
you to process XML.

Oops I misread that.. sorry, your right flash will allow me to use xml, I
just wanted to get a webservice running properly.
We had a great system, the webservice sent us the info, and we sent data
back and could update the database, and alter the dataset and reload it with
filtered results.

This is why I wanted to keep the dataset and process it into an array, so
all of the filtering and sorting could be done on the dataset and then it
would be converted to an array, for sending back to flash. I get the best of
all worlds. There is not filtering going on in this webservice yet, but
there is in others, and they must be converted too, but I need a solution to
this small problem so I can continue onwards with this project.
But, I have *a* solution, just not the one I wanted to hear. Although I may
not have explained things very well.

I'm going to give it one more try:
Here is the full code: BTW I am using the express tools from MS and after
i've figuired out if it's worth it, I am looking at buying vs.net, though I
may wait until the new version comes out before I do that. I am actually a
ColdFusion person normally.

<%@ WebService Language="C#" Class="WebService1" %>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Data.OleDb;
[WebService(Namespace = "http://microsoft.com/webservices/", Name = "None",
Description = "None")]
public class WebService1{
public WebService1(){
}
private DataSet getDataStreams(string sqlQuery)
{
System.Data.OleDb.OleDbConnection oleConn1 = new
System.Data.OleDb.OleDbConnection();
oleConn1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:\\database.mdb";
OleDbDataAdapter da = new OleDbDataAdapter(sqlQuery, oleConn1);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
[WebMethod (Description="...method1")]
public DataSet getOrderData(){
return getDataStreams("SELECT field1, field2, ... , field48 from
table1");
}
[WebMethod (Description="...method2")]
public DataSet getSOrderData(string passedvar){
return getDataStreams("SELECT field1, field2, ... , field48 from
table1 WHERE table1.var1='" + passedvar + "'");
}
[WebMethod (Description="...method3")]
public DataSet getMemberData(int passedvar2)
{
return getDataStreams("SELECT field1, field2, ... , field48 from
table1 WHERE table1.var1=" + passedvar2);
}
}
So that's it.... We have 3 methods for getting data from the database, each
one uses the same code to return a dataset. I would like to take that
dataset and turn it into something else, in this case it has to be an array,
so I can load it into the webservice connector in flash mx2004.
I cannot find code, or tutorials anywhere on how to take this dataset and
iterate through it passing the values to an array, hopefully strict typing
them on the way.

Please look at this code, and see if you can understand what I am trying to
do, I have had some great feedback, but I hope this explanation is slightly
clearer
Nov 16 '05 #7
Here is an update, I have got it to work in a fashion, it now returns a
readable array, that flash can use, don't quite understand how I got there,
just need 1 final push in the right direction, here is the new code section.

private ArrayList getDataStreams(string sqlQuery){

string mySelectQuery = sqlQuery;

OleDbConnection myConnection = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=D:\\Webspace\\customkit.net\\db\\CMX_Produc ts.mdb");

OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);

ArrayList list = new ArrayList();

myConnection.Open();

OleDbDataReader myReader;

myReader = myCommand.ExecuteReader();

while (myReader.Read()){

list.Add(myReader.GetString(1));

}

myReader.Close();

myConnection.Close();

return list;

}

It returns this:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAnyType xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://microsoft.com/webservices/">
<anyType xsi:type="xsd:string">1093963601</anyType>
<anyType xsi:type="xsd:string">1094027865</anyType>
<anyType xsi:type="xsd:string">-1178915986</anyType>
<anyType xsi:type="xsd:string">-1252773054</anyType>
<anyType xsi:type="xsd:string">1306755996</anyType>
<anyType xsi:type="xsd:string">135569893</anyType>
<anyType xsi:type="xsd:string">-1371539650</anyType>
<anyType xsi:type="xsd:string">-1371598016</anyType>
<anyType xsi:type="xsd:string">1384385168</anyType>
<anyType xsi:type="xsd:string">1404502826</anyType>
<anyType xsi:type="xsd:string">1469145929</anyType>
<anyType xsi:type="xsd:string">-1525322222</anyType>
<anyType xsi:type="xsd:string">1638466920</anyType>
<anyType xsi:type="xsd:string">-1669117341</anyType>
<anyType xsi:type="xsd:string">-1815307276</anyType>
<anyType xsi:type="xsd:string">-1824567740</anyType>
<anyType xsi:type="xsd:string">-1915951204</anyType>
<anyType xsi:type="xsd:string">-1940238693</anyType>
<anyType xsi:type="xsd:string">-197375379</anyType>
<anyType xsi:type="xsd:string">-265779925</anyType>
<anyType xsi:type="xsd:string">-305102370</anyType>
<anyType xsi:type="xsd:string">-369312721</anyType>
<anyType xsi:type="xsd:string">-597564863</anyType>
<anyType xsi:type="xsd:string">687999446</anyType>
<anyType xsi:type="xsd:string">703940518</anyType>
<anyType xsi:type="xsd:string">74151511</anyType>
<anyType xsi:type="xsd:string">-776864097</anyType>
<anyType xsi:type="xsd:string">853551591</anyType>
<anyType xsi:type="xsd:string">-9315851</anyType>
</ArrayOfAnyType>

Not exactly what I wanted, but nearer than we were yesterday. where it says
array of any type, dont understand that, xsd:string is what I asked for so
thats ok, would prefer that the anyType part was actually the name of the
column, can I do that. Obviously this the the list.Add(myReader.GetString()
part, but would like a little help here, as you can see making progress.

Thanks for all your time, sorry to be such a nuisense.

Regards
Shaine
Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by TempMan | last post: by
5 posts views Thread by mtv | last post: by
1 post views Thread by jens Jensen | last post: by
2 posts views Thread by Jannicke | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.