C# .NET - How to return String[] in webservice and read it result from applicati

Asked By Suresh on 18-May-12 01:43 PM
Earn up to 30 extra points for answering this tough question.
Hi,


I would like to return a String array (String[]) from a webmethod in webservice. I coded it and able to read it in browser. But when I added the service reference in C# console application, it shows, Cannot convert from ArrayOfObjects to String Array.

How do I convert the return type from webservice to String array?

Please help me.

Thank you,
Sureshbabu
Robbe Morris replied to Suresh on 18-May-12 03:02 PM
Is this a WCF service or an old Web 2.0 Web Service?

Post your web method code.
Suresh replied to Robbe Morris on 18-May-12 03:28 PM
Hi Robbe,

It is a WCF service.
Below is my code.
[WebMethod]
    public String[] getDirectoryInfo(String Photopath)
    {
    return Directory.GetFiles(Photopath, "*.jpg");
    }


Thank you,
Sureshbabu
[)ia6l0 iii replied to Suresh on 18-May-12 10:46 PM
This is not a wcf service. This is an ASMX web service. I would suggest you to start reading about WCF. MSDN should be your first pit stop. Below is the url that will get you started.

And for the error, it is in your calling code. Provide the calling code. We will have a look. You are not casting/parsing the result to the type expected.

http://msdn.microsoft.com/en-us/library/ms731082.aspx 

Hope this helped.
Chintan Vaghela replied to Suresh on 19-May-12 03:44 AM

Hello,

 

Take string[] array object and assign Directory.GetFiles Function

[WebMethod]

    public String[] getDirectoryInfo(String Photopath)

    {

      string[] arrPhoto = Directory.GetFiles(Photopath, "*.jpg");

      return arrPhoto;

 

    }

 

 

Hope this helpful!

Thanks

 

 

 

Chintan Vaghela replied to Suresh on 19-May-12 03:48 AM

Hello,

 

Simply convert ToArray as following way

[WebMethod]

    public String[] getDirectoryInfo(String Photopath)

    {

      return Directory.GetFiles(Photopath, "*.jpg").ToArray();

    }

 

Hope this helpful!

Thanks

 

 

 

kalpana aparnathi replied to Suresh on 20-May-12 06:37 AM
hi,

My suggestion is that use of array of byte instead of string array.You need to casting imagepath to the array like this.

byte[] b = File.ReadAllBytes(photoPath);

Hope will helps!!!!!!!!!!!

Regards,
Suresh replied to kalpana aparnathi on 20-May-12 06:56 AM
Hi Kalpana,

Not sure whether I understood your suggestion.
Do you mean that I should convert entire string array into one single string and convert it into byte array?

Please clarify.

Thanks.
kalpana aparnathi replied to Suresh on 20-May-12 06:59 AM
no first your path is convert to byte array and after that byte array to the string array as you need from your posted code.

Hope you will understand .

Suresh replied to kalpana aparnathi on 20-May-12 09:45 AM
Sorry, i don understand why should I convert the parameter to byte array and how it is related to converting return array into byte array.

Could you please post a bit of code.
Jitendra Faye replied to Suresh on 21-May-12 12:29 AM

HI Suresh,

First check your web service code because   you are getting this error because from your web method you are returning result in Array of object format and in your application you are trying to assign result in string array.

It is must that you have to return result in string array format or you have to perform type casting.

TRy like this-


   [WebMethod]

    public String[] getData()

    {

    string[] arrString= your operation;

    return arrString;

    }

Try like this and let me know.