error using NameValueCollection in a webservice

D

David

Hello,
I'm having difficulties using a NameValueCollection in a webservice.
I have tried using my code in a console app and it works fine, however
when I implement it as a webservice I get the following error:

System.InvalidOperationException: There was an error generating the
XML document. ---> System.InvalidOperationException: You must
implement the Add(System.String) method on
System.Collections.Specialized.NameValueCollection because it inherits
from ICollection.
at System.Xml.Serialization.TypeScope.GetCollectionElementType(Type
type)

etc etc.

Any ideas anybody?

Here is my code (minus the connection string and generated code)

public class Property : System.Web.Services.WebService
{
public SqlConnection sqlConnection1;

public Property()
{
InitializeComponent();
sqlConnection1 = new SqlConnection(//whatever);
}

[WebMethod]
public object getProperties(int VObjectId)
{
NameValueCollection properties;
//find the length of the bytestream from the database
int streamLength = getStreamLength(VObjectId);
if(streamLength != 0)
{
//deserialize the bytestream
properties = deSerialize(VObjectId, streamLength);
}
else
{
return null;
}
return properties;
}//end getProperties

private int getStreamLength(int VObjectId)
{
int x = 0;
SqlDataReader myReader = null;
SqlCommand mySqlCommand
= new SqlCommand("SELECT StreamLength FROM VObjects "
+"WHERE PK_ID = " +VObjectId, sqlConnection1);
try
{
sqlConnection1.Open();
myReader = mySqlCommand.ExecuteReader();
myReader.Read();
}

catch(Exception e)
{
return 0;
}

finally
{
x = (int)myReader["StreamLength"];
if (myReader != null)
{
myReader.Close();
}
if (sqlConnection1.State == ConnectionState.Open)
{
sqlConnection1.Close();
}
}
return x;
}//end getStreamLength

public NameValueCollection deSerialize(int VObjectId, int
streamLength)
{
NameValueCollection properties = null;
SoapFormatter format = new SoapFormatter();
byte[] byteStream = new byte[streamLength];

sqlConnection1.Open();
SqlCommand command = new SqlCommand("SELECT Properties FROM VObjects"
+" WHERE PK_ID = " +VObjectId, sqlConnection1);

SqlDataReader reader = command.ExecuteReader();
reader.Read();

try
{
reader.GetBytes(0,0,byteStream,0,streamLength);
}
catch(InvalidCastException ex)
{
Console.WriteLine(ex.Message);
}
catch(IndexOutOfRangeException ex)
{
Console.WriteLine(ex.Message);
}

MemoryStream memoryStream = new MemoryStream(byteStream);

try
{
properties =
(NameValueCollection)format.Deserialize(memoryStream);
}
catch(SerializationException ex)
{
Console.WriteLine(ex.Message);
}

return properties;

}//end deSerialize()


}//end class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top