How to receive string array from asp.net web service in silverlight 2
Last post 05-17-2008 4:34 AM by jackbond. 6 replies.
Sort Posts:
05-16-2008 6:51 PM
How to receive string array from asp.net web service in silverlight 2

Hi,

 I have consumed asp.net web service in silverlight project and it returns array of string. So I want to receive that array of string

from silverlight application. how do i do that???

here is my code in page.xaml.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml;
namespace nav_TO
{
    public partial class Page : UserControl
    {
        public string[] received=new string[3];
       

        public Page()
        {
            InitializeComponent();
            jaykONE.getXMLSoapClient getContent = new nav_TO.jaykONE.getXMLSoapClient();
             getContent.gotXMLAsync();
        
            getContent.gotXMLCompleted += new EventHandler<nav_TO.jaykONE.gotXMLCompletedEventArgs>(getContent_gotXMLCompleted);
            place = 0;
        }

        void getContent_gotXMLCompleted(object sender, nav_TO.jaykONE.gotXMLCompletedEventArgs e)
        {
   received = e.Result ;

// above line creates problem it says can not implicitly convert string to string[].
         

        }

Jay

jay nanavati

Loading...
Joined on 05-01-2008
India
Posts 18
05-16-2008 6:56 PM
Re: How to receive string array from asp.net web service in silverlight 2

Are you sure it returns an array of string? It doesn't look that way from the error you received.


Bill Reiss - Client App Dev MVP (What's an MVP?)
Silverlight and XNA Game Development Tutorials at http://www.bluerosegames.com/brg

Bill Reiss

Loading...
Joined on 05-01-2007
Tampa, FL
Posts 676
05-16-2008 11:33 PM
Re: How to receive string array from asp.net web service in silverlight 2

I think you mixed up string[] and string

Do you want to set a string with length 3?

Actually, in your code behind, you create an array of string, which means three string.

However, your ASP.NET Service returns a string not an array of string.

Just to create public string receive = "";

Then you can do the trick.

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you


The significant problems we face cannot be solved by the same level of thinking that created them

SteveWong

Loading...
Joined on 03-27-2008
Hong Kong
Posts 969
05-17-2008 3:37 AM
Re: How to receive string array from asp.net web service in silverlight 2

Thank you Steve & Bill,

Here I am posting my web service code. You can see it returns array of string.

public class getXML : System.Web.Services.WebService
{
    public string[] tabData=new string[3];
    public int prevID;
    public bool received;
    public getXML()
    {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }


    [WebMethod]
    public string[] gotXML()
    {
      
        XmlReaderSettings xset = new XmlReaderSettings();
        XmlReader reader = XmlReader.Create("http://technologyopinion.com/Documents/smartTab_data.xml", xset);
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element: // The node is an element.
                    if (reader.Name == "tab")
                    {
                        reader.MoveToFirstAttribute();
                        if (reader.Value == "2")
                            prevID = 0;
                        if(reader.Value=="3")
                           prevID=1;
                        if (reader.Value == "4")
                            prevID = 2;
                    }
                    break;

                case XmlNodeType.Text: //Display the text in each element.

                    tabData[prevID] = tabData[prevID] + "  " + reader.Value;
                    break;


            }

        }
       
                //return tabData[0].ToString () + "*" + tabData[1].ToString () + "*" + tabData[2].ToString ();

               //Previously I was doing above thing, which concates values of three array element but after

                //receiving this at silverlight side I need to do heavy  string manipulation to separate each

            //string array data.So I would like to return whole array of string tabData.
       

return tabData;
    }
}

Jay

jay nanavati

Loading...
Joined on 05-01-2008
India
Posts 18
05-17-2008 3:42 AM
Marked as Answer
Re: How to receive string array from asp.net web service in silverlight 2

Did the service at one point return just a string? Have you tried updating the web reference?

jackbond

Loading...
Joined on 03-21-2007
Posts 389
05-17-2008 4:25 AM
Re: How to receive string array from asp.net web service in silverlight 2

Great jackbond,

 I tried couple of things and it worked.

For anyone else who may face this problem, I tried following,

1. first I formatted e.Result to ToArray():

 void getContent_gotXMLCompleted(object sender, nav_TO.jaykONE.gotXMLCompletedEventArgs e)
        {
   received = e.Result.ToArray();

        }

2. I updated the web reference by right clicking on service reference and choosing update.

 So I am now finally able to receive array of string in silverlight 2 project.

Jay

jay nanavati

Loading...
Joined on 05-01-2008
India
Posts 18
05-17-2008 4:34 AM
Re: How to receive string array from asp.net web service in silverlight 2

If you set a breakpoint on

 

jay nanavati:
received = e.Result.ToArray();

 what type is e.Result?

I'm guessing it's List<string> Depending on your ultimate usage, you can choose whether or not you want/need to cast it to an array.

jackbond

Loading...
Joined on 03-21-2007
Posts 389
Page view counter