PDA

View Full Version : Sending an array of arrays to an Asp.Net webservice


jeremydore
05-25-2006, 11:53 AM
Hi folks,
This is a kind of 'does anyone know if this can be done...?' question!
I'm using the WebServiceConnector to extract an array of products from an Asp.Net webservice and then save them back when changed.
After I've triggered the web service to load the products into flash I use this listener:
private function loadRes(ev:Object){
var res = ev.target.results;
var objRef:Object;
for(var i=0; i<res.length; i++){
var tempObject:Object = new Object();
tempObject.product = res[i].product;
tempObject.productID = res[i].productID;
tempObject.productType = res[i].productType;
myProductArray.push(tempObject);
}
}
This works fine and in one single web service call I have loaded an entire arrray of products into my array (myProductArray) in Flash.

HOWEVER... I can't do the same back - each item in myProductArray needs a separate call to the relevant Asp.Net web service, so the whole process is very lengthy (and asynchronous!)
The problem seems to be that Asp.Net web services will only accept parameters that aren't arrays of user-defined objects (structs)

ie. on the Asp.Net side I can write a web service that does this:[C#]
[WebMethod]
public Boolean AddProduct(string name, int productID, int productType){
SqlConnection conn = new SqlConnection(_connString);
...etc and call it for every single product

but it won't work if I do it like this:[C#]
public struct Product{
public string name;
public int productID;
public int productType;
}
[WebMethod]
public Boolean AddProducts(Product[] myProducts){
...
I realise that this is an actionscript forum but someone must be using Asp.Net and facing this problem! Any help or even commiseration of 'I have that problem too' would be much appreciated!
Thanks!

hobbis
05-25-2006, 09:37 PM
I'm surprised this cannot be easily done in .NET as it is easy to do in PHP!

I pass an object to the service as you would like to do and my service function looks like this e.g.


function saveFlvsToShowcases($showcase_id, $obj) {
$ok = true;
if(!mysql_query("DELETE FROM flvs_to_showcases WHERE showcase_id='" . $showcase_id . "'")) { return false; }
for($i=0; $i<sizeof($obj); $i++){
$result = mysql_query("INSERT INTO flvs_to_showcases VALUES('', '".$showcase_id."', '".$obj[$i]['flv_id']."', '".$obj[$i]['ordering']."')");
if(!$result) $ok = false;
}
return $ok;
}


notice the $obj - and to get at it's values, I use $obj[$i]['PROPERTYNAME']

Have you tried using a String array?

jeremydore
05-25-2006, 10:29 PM
Hey, hobbis, thanks for replying!
Just wish I knew PHP - I'll try and get my head round what the equivalent Asp.Net would be like but I suspect the problem may be down to Asp.Net being very strictly typed.

Yes, I have tried passing a string array and that doesn't work. I've also tryed an ArrayList (as that's Asp.Net's equivalent of a dynamically resizing array) which takes the data but I can't seem to get it out...
public Boolean AddProducts [ArrayList myProducts){
...
I can trace out myProducts.Count.ToString() but this just gives the number of characters in the very first item. I can't get at the actual item or subsequent ones. Tracing myProducts[0].name.ToString() just gives an error and myProducts[0].ToString() gives 'System.Object'

Any further ideas much appreciated- thanks guys!:)

hobbis
05-26-2006, 08:29 AM
I'm probably not going to be much help here as I am a relative newcomer to .NET but is this possible: myProducts[0]['name']

Or maybe you need to create a enumerator? Not sure I'm afraid, I'm in the dark on this one. If you crack it though, let me know as I will soon be doing my services in .NET.

jeremydore
05-26-2006, 08:53 AM
Hobbis,
Thanks again for your reply. myProducts[0]['name'] doesn't work and I'm not sure that an enumerator will be appropriate here. I'll keep looking into it though and let you know if I find a solution.
If you're going to be doing a .Net service, I'd highly recommend the following book which has been brilliant, containing everything you need in clear not-too-lengthy format, apart from the answer to the above problem of course!
'ASP.Net for Flash' by Ryan Moore published by Friends of Ed

jeremydore
06-01-2006, 05:43 PM
I've finally solved it - Asp.net web services won't accept an array of structs it seems to me, but they will accept arrays of the primitive types such as string[] myStrings, int[] myInts, etc as parameters IN. So I have simply written a function to convert my array of (horizontal) struts to a number of (vetical) primitive type arrays and passed those.

On the acionscript side, the webserviceconnector.params line must have one dimensional arrays assigned to it where they're expected by the Asp.net webservice (to state the obvious)

See http://samples.gotdotnet.com/quickstart/aspplus/doc/webservicetypes.aspx for details and thanks again for the suggestions.

hobbis
06-02-2006, 08:04 AM
Well done jeremydore! Thank you for replying (some people forget to post the solution when they fix it). I'm sure this will come in handy when I start .NET services.