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
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