Problems returning array from a service

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Louisa55
    New Member
    • Mar 2008
    • 5

    Problems returning array from a service

    I've managed to write a service that returns an array in XML when tested by itself with no problems, but when I try and get my webpage to consume it I'm having difficulties

    Here's my service simplified:

    Code:
     public struct ListData
       {public string id, title, subtitle;}
    
    [WebMethod]
       public ListData[] GetListData()
       {
          int x = 0;
          int number = 0;
          ListData[] List = null;
          List = new ListData[5];
    
          while (x < 5)
                 {         
             List[x].id =  "subtitle test" + System.Convert.ToString(x);
             List[x].title =  "subtitle test" + System.Convert.ToString(x);
             List[x].subtitle = "subtitle test" + System.Convert.ToString(x);
             x++;
                }
    
          Return List;
       }

    The consuming page has had countless versions of this:

    Code:
     localhost.Service myws = new localhost.Service();
          localhost.ListData list = new localhost.ListData();
          list = myws.GetListData();
    
          Label1.Text = "Test data: <br/> " + list[0].Title + list[1].Subtitle + list[2].id;
    I've tried so many combinations and Googled everywhere but I just can't seem to get it to work :(
    I'm quite new at this so if anyone has any ideas I'd really appreciate it!
  • mikeyeli
    New Member
    • Oct 2007
    • 63

    #2
    Originally posted by Louisa55
    I've managed to write a service that returns an array in XML when tested by itself with no problems, but when I try and get my webpage to consume it I'm having difficulties

    Here's my service simplified:

    Code:
     public struct ListData
       {public string id, title, subtitle;}
    
    [WebMethod]
       public ListData[] GetListData()
       {
          int x = 0;
          int number = 0;
          ListData[] List = null;
          List = new ListData[5];
    
          while (x < 5)
                 {         
             List[x].id =  "subtitle test" + System.Convert.ToString(x);
             List[x].title =  "subtitle test" + System.Convert.ToString(x);
             List[x].subtitle = "subtitle test" + System.Convert.ToString(x);
             x++;
                }
    
          Return List;
       }

    The consuming page has had countless versions of this:

    Code:
     localhost.Service myws = new localhost.Service();
          localhost.ListData list = new localhost.ListData();
          list = myws.GetListData();
    
          Label1.Text = "Test data: <br/> " + list[0].Title + list[1].Subtitle + list[2].id;
    I've tried so many combinations and Googled everywhere but I just can't seem to get it to work :(
    I'm quite new at this so if anyone has any ideas I'd really appreciate it!
    Exactly what difficulties are you having?

    Comment

    • Louisa55
      New Member
      • Mar 2008
      • 5

      #3
      I can't seem to get the values into the "list" variable in the consuming website.
      I keep getting errors like
      "Cannot implicitly convert type 'localhost.List Data[]' to 'localhost.List Data'" or
      "Cannot apply indexing with [] to an expression of type 'localhost.List Data'"
      and seem to be getting nowhere

      Comment

      • mikeyeli
        New Member
        • Oct 2007
        • 63

        #4
        Originally posted by Louisa55
        I've managed to write a service that returns an array in XML when tested by itself with no problems, but when I try and get my webpage to consume it I'm having difficulties

        Here's my service simplified:

        Code:
         public struct ListData
           {public string id, title, subtitle;}
        
        [WebMethod]
           public ListData[] GetListData()
           {
              int x = 0;
              int number = 0;
              ListData[] List = null;
              List = new ListData[5];
        
              while (x < 5)
                     {         
                 List[x].id =  "subtitle test" + System.Convert.ToString(x);
                 List[x].title =  "subtitle test" + System.Convert.ToString(x);
                 List[x].subtitle = "subtitle test" + System.Convert.ToString(x);
                 x++;
                    }
        
              Return List;
           }

        The consuming page has had countless versions of this:

        Code:
         localhost.Service myws = new localhost.Service();
              localhost.ListData list = new localhost.ListData();
              list = myws.GetListData();
        
              Label1.Text = "Test data: <br/> " + list[0].Title + list[1].Subtitle + list[2].id;
        I've tried so many combinations and Googled everywhere but I just can't seem to get it to work :(
        I'm quite new at this so if anyone has any ideas I'd really appreciate it!
        ok i see you have several small bugs here.

        first thing you should see:
        Code:
        public ListData[] GetListData()
        this method returns a an array of ListData objects. not a single ListData object
        so the client app here:
        Code:
        list = myws.GetListData();
        expects you to assign the returning array to an array of ListData.

        so the solution to that would be:
        Code:
        localhost.ListData[] list;
        list = myws.GetListData();
        then i see you user different indexes to access info in the ListData Array
        Code:
        Label1.Text = "Test data: <br/> " + list[0].Title + list[1].Subtitle + list[2].id;
        I assume u wish to access attributes of a single and same ListData object
        so you should use it this way:
        Code:
        Label1.Text = "Test data: <br/> " + list[0].Title + list[0].Subtitle + list[0].id;
        Hope this helps!

        Comment

        • Louisa55
          New Member
          • Mar 2008
          • 5

          #5
          Thank you so much!
          Yes you were right, it just wasn't declaring it as an array!

          The random array values I was displaying were just so I could check I was using the array properly when reading into it from the database.

          Again, thank you

          Comment

          Working...