IT Community - Software Programming, Web Development and Technical Support

why the "InvalidOperationException" error when returning array value in webservice?

This is a discussion on why the "InvalidOperationException" error when returning array value in webservice? within the ASP and ASP.NET Programming forums, part of the Web Development category; I have a database result set I would like to return, so I create a new array for each row, ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-29-2007, 06:09 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Default why the "InvalidOperationException" error when returning array value in webservice?

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-29-2007, 06:10 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Exclamation Re: why the "InvalidOperationException" error when returning array value in webservic

The error is...

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()
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-29-2007, 06:13 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Question Re: why the "InvalidOperationException" error when returning array value in webservic

Can you put here your code here?
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-31-2007, 03:08 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Smile Re: why the "InvalidOperationException" error when returning array value in webservic

Ya sure…

Here is my code…

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;
        }
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 11-01-2007, 03:30 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Default Re: why the "InvalidOperationException" error when returning array value in webservic

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.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 11-04-2007, 11:01 PM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Post Re: why the "InvalidOperationException" error when returning array value in webservic

Hi,

The size of the array is declared in here:

Code:
columns = new string[fieldCount - 1];

The size of the array is set to the number of columns. 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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 11-12-2007, 11:11 PM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Default Re: why the "InvalidOperationException" error when returning array value in webservic

Hi buddy,

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.
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 11-14-2007, 10:39 PM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Exclamation Re: why the "InvalidOperationException" error when returning array value in webservic

Oh..thnx krishna...

Can you put an example for an arraylist into a type?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I keep getting "Data Missing" when I click the "back" button in my browser. How can I oxygen HTML, CSS and Javascript Coding Techniques 1 07-28-2007 01:12 AM
Difference between "vector" and "array"? Sabari C and C++ Programming 1 07-24-2007 04:33 AM
I get the error message "Unable to start debugging on the web server..." when I debug oxygen ASP and ASP.NET Programming 1 07-20-2007 04:50 AM
I get the error "The page cannot be displayed" and an HTTP 502 Proxy Error. Why? kingmaker ASP and ASP.NET Programming 1 07-20-2007 04:43 AM
Why do I get "HTTP 500" error(or "(DLL)initialization routine failed")in my browser? kingmaker ASP and ASP.NET Programming 1 07-20-2007 04:38 AM


All times are GMT -7. The time now is 06:48 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0