Microsoft KB Archive/815131

= BUG: &quot;Cannot implicitly convert type 'System.Data.DataSet'&quot; error when application is built =

Article ID: 815131

Article Last Modified on 5/12/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-





Beta Information
This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.

No formal product support is available from Microsoft for this Beta product. For information about how to obtain support for a Beta release, see the documentation that is included with the Beta product files, or check the Web location from which you downloaded the release.



SYMPTOMS
You have a Web Service that has a Web Service method that returns an instance of a custom class. The class that is returned by the Web Service method implements the IXmlSerializable interface. If you use this Web Service in an application, you may receive the following error message when you build the application:

Cannot implicitly convert type 'System.Data.DataSet' to 'ClassLibrary.ClassName'



CAUSE
When you add a Web reference to a Web Service, Microsoft Visual Studio .NET incorrectly uses System.Data.DataSet instead of the class that implements the IXmlSerializable interface. Therefore, the Web Service method in the proxy class returns System.Data.DataSet instead of the custom class that implements the IXmlSerializable interface. Therefore, you receive the error message when you consume the Web Service method in your Web application.



RESOLUTION
To resolve this problem, manually replace System.Data.DataSet in the proxy class with the custom class that implements the IXmlSerializable interface. To do this, follow these steps:  In Solution Explorer, click the Web Application project. On the Project menu, click Show All Files. In Solution Explorer, expand Web References, and then expand localhost. Expand the Reference.map node, and then double-click the proxy file Reference.cs or Reference.vb.  Locate the method that incorrectly returns System.Data.DataSet. For example, locate the following code for the proxy file of the Web Service that is used in the &quot;More Information&quot; section of this article:

Visual C# .NET Code
[System.Web.Services.Protocols.SoapDocumentMethodAttribute(&quot;http://tempuri.org/GetMyObject&quot;, RequestNamespace=&quot;http://tempuri.org/&quot;, ResponseNamespace=&quot;http://tempuri.org/&quot;, Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] public System.Data.DataSet GetMyObject { object[] results = this.Invoke(&quot;GetMyObject&quot;, new object[0]); return ((System.Data.DataSet)(results[0])); }

Visual Basic .NET Code
 _ Public Function GetMyObject As System.Data.DataSet Dim results As Object = Me.Invoke(&quot;GetMyObject&quot;, New Object(-1) {}) Return CType(results(0), System.Data.DataSet) End Function </li>  Manually replace System.Data.DataSet with the name of your custom class that implements the IXmlSerializable interface. For example, modify the method that you located in step 5 as follows:

Visual C# .NET Code
[System.Web.Services.Protocols.SoapDocumentMethodAttribute(&quot;http://tempuri.org/GetMyObject&quot;, RequestNamespace=&quot;http://tempuri.org/&quot;, ResponseNamespace=&quot;http://tempuri.org/&quot;, Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] public MyTestClass GetMyObject { object[] results = this.Invoke(&quot;GetMyObject&quot;, new object[0]); return ((MyTestClass)(results[0])); }

Visual Basic .NET Code
<System.Web.Services.Protocols.SoapDocumentMethodAttribute(&quot;http://tempuri.org/GetMyObject&quot;, RequestNamespace:=&quot;http://tempuri.org/&quot;, ResponseNamespace:=&quot;http://tempuri.org/&quot;, Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)> _ Public Function GetMyObject As MyTestClass Dim results As Object = Me.Invoke(&quot;GetMyObject&quot;, New Object(-1) {}) Return CType(results(0), MyTestClass) End Function </li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
<ol> Create a new Visual Basic or Visual C# ASP.NET Web Service project, and name it MyTestWebService.</li> In Solution Explorer, right-click Service1.asmx, and then click View Code.</li>  Replace the existing code with the following code:

Visual C# .NET Code
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization;

namespace MyTestWebService {  public class MyTestWebService : WebService {     public MyTestWebService {     }      // Web Service method that Returns Custom Class [WebMethod] public MyTestClass GetMyObject {        MyTestClass myObj = new MyTestClass; return myObj; }  }

// Class that implements IXmlSerializable public class MyTestClass : IXmlSerializable {     // Constructor public MyTestClass {     }      // Implementing IXmlSerializable method GetSchema XmlSchema IXmlSerializable.GetSchema {        return null; }     // Implementing IXmlSerializable method ReadXml void IXmlSerializable.ReadXml(XmlReader reader) {     }

// Implementing IXmlSerializable method WriteXml void IXmlSerializable.WriteXml(XmlWriter writer) {     }   } }

Visual Basic .NET Code
Imports System Imports System.Collections Imports System.ComponentModel Imports System.Data Imports System.Diagnostics Imports System.Web Imports System.Web.Services Imports System.Xml Imports System.Xml.Schema Imports System.Xml.Serialization

<WebService(Namespace:=&quot;http://tempuri.org/&quot;)> _ Public Class MyWebServiceClass Inherits System.Web.Services.WebService

Public Sub New

End Sub

<WebMethod> _ Public Function GetMyObject As MyTestClass Dim myObj As New MyTestClass Return myObj End Function

End Class

' Class that implements IXmlSerializable Public Class MyTestClass Implements IXmlSerializable

' Default Constructor Public Sub New

End Sub

' Implementing IXmlSerializable method GetSchema Public Function GetSchema As XmlSchema Implements IXmlSerializable.GetSchema

End Function

' Implementing IXmlSerializable method ReadXml Public Sub ReadXml(ByVal reader As XmlReader) Implements IXmlSerializable.ReadXml

End Sub

' Implementing IXmlSerializable method WriteXml Public Sub WriteXml(ByVal writer As XmlWriter) Implements IXmlSerializable.WriteXml

End Sub End Class </li> On the Build menu, click Build Solution.</li> On the File menu, point to Add Project, and then click New Project.</li> Under Project Types, click Visual C# Projects or Visual Basic Projects, and under Templates, click ASP.NET Web Application.</li> Name the project MyTestApplication, and then click OK.</li> In Solution Explorer, right-click WebForm1.aspx, and then click View Code.</li>  Replace the existing code with the following code:

Visual C# .NET Code
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization;

namespace WebApplication47 {   public class WebForm1 : System.Web.UI.Page {       private void Page_Load(object sender, System.EventArgs e)        { localhost.MyObjectService obj = new localhost.MyObjectService; MyTestClass objTest; objTest = obj.GetMyObject; }

#region Web Form Designer generated code override protected void OnInit(EventArgs e)       { InitializeComponent; base.OnInit(e); }       private void InitializeComponent {               this.Load += new System.EventHandler(this.Page_Load); }       #endregion }

// Class that implements IXmlSerializable public class MyTestClass : IXmlSerializable {     // Constructor public MyTestClass {     }      // Implementing IXmlSerializable method GetSchema XmlSchema IXmlSerializable.GetSchema {        return null; }     // Implementing IXmlSerializable method ReadXml void IXmlSerializable.ReadXml(XmlReader reader) {     }

// Implementing IXmlSerializable method WriteXml void IXmlSerializable.WriteXml(XmlWriter writer) {     }   } }

Visual Basic .NET Code
Imports System Imports System.Xml Imports System.Xml.Schema Imports System.Xml.Serialization

Public Class WebForm1 Inherits System.Web.UI.Page


 * 1) Region &quot; Web Form Designer Generated Code &quot;

'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init InitializeComponent End Sub


 * 1) End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim obj As New localhost.MyWebServiceClass Dim objTest As MyTestClass objTest = obj.GetMyObject End Sub

End Class

' Class that implements IXmlSerializable Public Class MyTestClass Implements IXmlSerializable

' Default Constructor Public Sub New

End Sub

' Implementing IXmlSerializable method GetSchema Public Function GetSchema As System.Xml.Schema.XmlSchema Implements IXmlSerializable.GetSchema

End Function

' Implementing IXmlSerializable method ReadXml Public Sub ReadXml(ByVal reader As System.Xml.XmlReader) Implements IXmlSerializable.ReadXml

End Sub

' Implementing IXmlSerializable method WriteXml Public Sub WriteXml(ByVal writer As System.Xml.XmlWriter) Implements IXmlSerializable.WriteXml

End Sub End Class </li> In Solution Explorer, right-click MyTestApplication, and then click Add Web Reference.</li> In the Address text box, type http://localhost/MyTestWebService/Service1.asmx, and then press ENTER.</li> Click Add Reference.</li> On the File menu, click Save All.</li> On the Build menu, click Build Solution. During compilation of MyTestApplication, you receive the error that is described in the &quot;Symptoms&quot; section.</li></ol>

<div class="references_section">