473,466 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem returning container class with DataSet as object

I am implementing a web service and thought it may be a good idea to return a
more complex class (which I have called 'ServiceResponse') in order to wrap
the original return value along with two other properties...

bool error;
string lastError;

My whole class looks like this...

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace My.WebServices
{
[Serializable]
public class ServiceResponse
{
public bool error = false;
public string lastError = "";
public object returnObject = null;

public ServiceResponse()
{

}
}
}

A sample method on my WebService looks like...

[WebMethod]
public ServiceResponse Ping(string deviceId)
{
ServiceResponse resp = new ServiceResponse();
if (IsValidDevice(deviceId))
{
resp.ReturnValue = true;
}
else
{
resp.Error = true;
resp.LastError = DEVICE_UNKNOWN_ERROR;
resp.ReturnValue = false;
}
return resp;
}

and a client side call looks like...

My.WebServices.Service svc = new My.WebServices.Service();
My.WebServices.ServiceResponse resp;
resp = (My.WebServices.ServiceResponse)svc.Ping(deviceId) ;
if (!resp.Error)
{
//do something
}

So, this works lovely... and solves my needs. However, if I have a web
service method that returns a DataSet as an object in the ServiceResponse...
such as...

[WebMethod]
public ServiceResponse GetUsers(string deviceId)
{
ServiceResponse resp = new ServiceResponse();
if (IsValidDevice(deviceId))
{
try
{
string sql = "select * from [user] where enabled = 1";
SqlCommand cmd = new SqlCommand(sql);
SqlDatabase db = new SqlDatabase(CONNECTION_STRING);
DataSet ds = db.ExecuteDataSet(cmd);
resp.ReturnValue = ds;
}
catch (Exception ex)
{
resp.Error = true;
resp.LastError = ex.Message;
}
}
else
{
resp.Error = true;
resp.LastError = DEVICE_UNKNOWN_ERROR;
}
return resp;
}

I get an error calling this method...

System.InvalidOperationException: There was an error generating the XML
document. ---> System.InvalidOperationException: The type System.Data.DataSet
may not be used in this context. To use System.Data.DataSet as a parameter,
return type, or member of a class or struct, the parameter, return type, or
member must be declared as type System.Data.DataSet (it cannot be object).
Objects of type System.Data.DataSet may not be used in un-typed collections,
such as ArrayLists.
at
System.Xml.Serialization.XmlSerializationWriter.Wr iteTypedPrimitive(String
name, String ns, Object o, Boolean xsiType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write2_ServiceResponse(String
n, String ns, ServiceResponse o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write4_ServiceResponse(Object o)
at
Microsoft.Xml.Serialization.GeneratedAssembly.Serv iceResponseSerializer1.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(T extWriter
textWriter, Object o)
at System.Web.Services.Protocols.XmlReturnWriter.Writ e(HttpResponse
response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.W riteReturns(Object[]
returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.Wr iteReturns(Object[]
returnValues)
at System.Web.Services.Protocols.WebServiceHandler.In voke()

I think I understand that the problem is because my
ServiceResponse.ReturnValue is of type 'object', SOAP does not know how to
format a complex type such as a DataSet?

So, at this point, Im sort of at a loss on which direction to head. Here are
the options that I see I have but Im not clear on which are best or even
feasible.

1. Somehome make my ServiceResponse class serialize complex types manually
by implementing the iSerializable interface?
2. Drop this return wrapper and use exceptions on the WebService side to
throw errors and post messages.
3. Some other method that I have not though of??

Thank you for any suggestions... Merry Christmas!
Dec 23 '05 #1
1 5515
I did come up with a slight solution however, it is a 'hack' on the dataset
object. If anyone has any thoughts on a complete solution that may include
other classes, Im still open to suggestions.

Basically, since the dataset is just being returned as XML and strings to
work, I simply return an XML formatted string instead of a dataset. Such as...

DataSet ds = db.ExecuteDataSet(cmd);
MemoryStream stream = new MemoryStream();
ds.WriteXml(stream, XmlWriteMode.WriteSchema);
StreamReader reader = new StreamReader(stream);
stream.Position = 0;
resp.ReturnValue = reader.ReadToEnd();

The reason I use this more 'complex' code over ds.WriteXmlSchema is that on
the receiving end I still just want to use the simple ds.ReadXml which
expects XML formatted with ds.WriteXml.
"J. Askey" wrote:
I am implementing a web service and thought it may be a good idea to return a
more complex class (which I have called 'ServiceResponse') in order to wrap
the original return value along with two other properties...

bool error;
string lastError;

My whole class looks like this...

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace My.WebServices
{
[Serializable]
public class ServiceResponse
{
public bool error = false;
public string lastError = "";
public object returnObject = null;

public ServiceResponse()
{

}
}
}

A sample method on my WebService looks like...

[WebMethod]
public ServiceResponse Ping(string deviceId)
{
ServiceResponse resp = new ServiceResponse();
if (IsValidDevice(deviceId))
{
resp.ReturnValue = true;
}
else
{
resp.Error = true;
resp.LastError = DEVICE_UNKNOWN_ERROR;
resp.ReturnValue = false;
}
return resp;
}

and a client side call looks like...

My.WebServices.Service svc = new My.WebServices.Service();
My.WebServices.ServiceResponse resp;
resp = (My.WebServices.ServiceResponse)svc.Ping(deviceId) ;
if (!resp.Error)
{
//do something
}

So, this works lovely... and solves my needs. However, if I have a web
service method that returns a DataSet as an object in the ServiceResponse...
such as...

[WebMethod]
public ServiceResponse GetUsers(string deviceId)
{
ServiceResponse resp = new ServiceResponse();
if (IsValidDevice(deviceId))
{
try
{
string sql = "select * from [user] where enabled = 1";
SqlCommand cmd = new SqlCommand(sql);
SqlDatabase db = new SqlDatabase(CONNECTION_STRING);
DataSet ds = db.ExecuteDataSet(cmd);
resp.ReturnValue = ds;
}
catch (Exception ex)
{
resp.Error = true;
resp.LastError = ex.Message;
}
}
else
{
resp.Error = true;
resp.LastError = DEVICE_UNKNOWN_ERROR;
}
return resp;
}

I get an error calling this method...

System.InvalidOperationException: There was an error generating the XML
document. ---> System.InvalidOperationException: The type System.Data.DataSet
may not be used in this context. To use System.Data.DataSet as a parameter,
return type, or member of a class or struct, the parameter, return type, or
member must be declared as type System.Data.DataSet (it cannot be object).
Objects of type System.Data.DataSet may not be used in un-typed collections,
such as ArrayLists.
at
System.Xml.Serialization.XmlSerializationWriter.Wr iteTypedPrimitive(String
name, String ns, Object o, Boolean xsiType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write2_ServiceResponse(String
n, String ns, ServiceResponse o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write4_ServiceResponse(Object o)
at
Microsoft.Xml.Serialization.GeneratedAssembly.Serv iceResponseSerializer1.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(T extWriter
textWriter, Object o)
at System.Web.Services.Protocols.XmlReturnWriter.Writ e(HttpResponse
response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.W riteReturns(Object[]
returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.Wr iteReturns(Object[]
returnValues)
at System.Web.Services.Protocols.WebServiceHandler.In voke()

I think I understand that the problem is because my
ServiceResponse.ReturnValue is of type 'object', SOAP does not know how to
format a complex type such as a DataSet?

So, at this point, Im sort of at a loss on which direction to head. Here are
the options that I see I have but Im not clear on which are best or even
feasible.

1. Somehome make my ServiceResponse class serialize complex types manually
by implementing the iSerializable interface?
2. Drop this return wrapper and use exceptions on the WebService side to
throw errors and post messages.
3. Some other method that I have not though of??

Thank you for any suggestions... Merry Christmas!

Dec 27 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Morten | last post by:
Hi! I have a problem displaying some values in a datagrid. I have an array that consists of a number of objects. Each object has 2 properties: Name and a list of web addresses. (e.g: Name:...
0
by: Albert | last post by:
******************** My code: ******************** <%@ Page Language="VB" debug="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script...
2
by: Stephen Miller | last post by:
I am using the OnItemDataBound event of Repeater control to nest a DataGrid within the Repeater. When I attempt to bind to the DataGrid using the DataSource method I get the error message "Object...
8
by: I am Sam | last post by:
Hi everyone, This problem is making me old. I don't want to get any older. I have a multi-nested repeater control as follows: <asp:Repeater ID="clubRep1" Runat="server">...
4
by: Girish | last post by:
Im trying to create a grid within a grid programmatically. Ive been successful in doing this but I need the embedded grid to fire its ItemDataBound event so I can handle it. The event does not seem...
7
by: Girish | last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a huge learning experience.. My problem. I have a requirement to display a gird with a gird. Within the embedded grid,...
1
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes...
4
by: Brad Baker | last post by:
I'm going a little crazy :) I'm trying to bind a repeater control to a dataset on page load using the following code: if (Request.QueryString != null) { string customerid = Request.QueryString;...
0
by: uncensored | last post by:
Hello everyone, I'm fairly new at .Net and I have a repeater inside a repeater problem. I will attach my code to this message but basically what I am able to tell when I run my page it tells me...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.