473,372 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 5510
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.