SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to return array in web service?

    I have a database result set I would like to return, so I create a new array for each row, and the rows into an arraylist Now when I compile it it works fine, but when I go to test out the web service it returns this nice message.

    Code:
    System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.String[] may not be used in this context.
       at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_ArrayOfAnyType(Object o)
       at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayListSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
       at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
       --- End of inner exception stack trace ---
       at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
       at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
       at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
       at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
       at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
       at System.Web.Services.Protocols.WebServiceHandler.Invoke()
    It doesn't seem to like the array[], I have no idea why.

    This is the web method I am trying to make work

    Code:
    [WebMethod]
            public ArrayList getResults()
            {
                string sql = "select * from tblreadings";
                MySqlCommand myCommand = new MySqlCommand(sql, this.con);
                con.Open();
                MySqlDataReader myReader;
                myReader = myCommand.ExecuteReader();
    
                string[] columns = null;
                // Fields in a row are stored in array
                int fieldCount = myReader.FieldCount;
    
                // Rows are stored in arraylist
                ArrayList rows = new ArrayList();
    
                //string results = ""; 
    
                try
                {
                    while (myReader.Read())
                    {
                        columns = new string[fieldCount - 1];
                        columns[0] = myReader.GetString(1);
                        columns[1] = myReader.GetString(2);
                        columns[2] = myReader.GetString(3);
                        rows.Add(columns);
                    }
                }
                finally
                {
                    myReader.Close();
                    con.Close();
                }
    
                return rows;
            }
    My First Website YouCritiqueIT.com
    - A unique approach to rating products

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Where are you declaring the size of the array?

    What you should probably do here is make a simple data object to contain the data then use a List<YourSimpleDataObject> rather than an array list of arrays. It will be much more readable on the wire.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The size of the array is declared in here:

    columns = new string[fieldCount - 1];


    The size of the array is set to the number of columns. I did get it to work with a dataset which is a better solution, but I'd like to know why an arraylist of arrays wasn't able to be xmlserialized when both an arraylist and array can be xmlserialized individually.
    My First Website YouCritiqueIT.com
    - A unique approach to rating products

  4. #4
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but I'd like to know why an arraylist of arrays wasn't able to be xmlserialized when both an arraylist and array can be xmlserialized individually
    Because the ArrayList is untyped, so ASP.NET has no way of knowing what type the elements will be. Hence, it cannot generate a schema for the type.

    Either use a type which is specific about its elements
    -or-
    Use an attribute to instruct ASP.NET about the type of the elements

    A dataset is a concrete type - one for which ASP.NET *can* generate a schema. Indeed, a dataset definition *is* a schema.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you be so kind as to post a very simple example of turning an arraylist into a type?
    My First Website YouCritiqueIT.com
    - A unique approach to rating products


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •