BUG: "Cannot implicitly convert type 'System.Data.DataSet'" error when application is built (815131)



The information in this article applies to:

  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft XML Classes (included with the .NET Framework 1.0)
  • Microsoft XML Classes (included with the .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:
  1. In Solution Explorer, click the Web Application project.
  2. On the Project menu, click Show All Files.
  3. In Solution Explorer, expand Web References, and then expand localhost.
  4. Expand the Reference.map node, and then double-click the proxy file Reference.cs or Reference.vb.
  5. 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 "More Information" section of this article:

    Visual C# .NET Code

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

    Visual Basic .NET Code

          <System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetMyObject", RequestNamespace:="http://tempuri.org/", ResponseNamespace:="http://tempuri.org/", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)> _
          Public Function GetMyObject() As System.Data.DataSet
             Dim results() As Object = Me.Invoke("GetMyObject", New Object(-1) {})
             Return CType(results(0), System.Data.DataSet)
          End Function
  6. 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("http://tempuri.org/GetMyObject", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
            public MyTestClass GetMyObject() {
                object[] results = this.Invoke("GetMyObject", new object[0]);
                return ((MyTestClass)(results[0]));
            }

    Visual Basic .NET Code

          <System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetMyObject", RequestNamespace:="http://tempuri.org/", ResponseNamespace:="http://tempuri.org/", 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("GetMyObject", New Object(-1) {})
             Return CType(results(0), MyTestClass)
          End Function

STATUS

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

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Visual Basic or Visual C# ASP.NET Web Service project, and name it MyTestWebService.
  2. In Solution Explorer, right-click Service1.asmx, and then click View Code.
  3. 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:="http://tempuri.org/")> _
    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
  4. On the Build menu, click Build Solution.
  5. On the File menu, point to Add Project, and then click New Project.
  6. Under Project Types, click Visual C# Projects or Visual Basic Projects, and under Templates, click ASP.NET Web Application.
  7. Name the project MyTestApplication, and then click OK.
  8. In Solution Explorer, right-click WebForm1.aspx, and then click View Code.
  9. 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
    
    #Region " Web Form Designer Generated Code "
    
       '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
    
    #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
  10. In Solution Explorer, right-click MyTestApplication, and then click Add Web Reference.
  11. In the Address text box, type http://localhost/MyTestWebService/Service1.asmx, and then press ENTER.
  12. Click Add Reference.
  13. On the File menu, click Save All.
  14. On the Build menu, click Build Solution. During compilation of MyTestApplication, you receive the error that is described in the "Symptoms" section.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:

Modification Type:MinorLast Reviewed:9/15/2005
Keywords:kbvs2002sp1sweep kbCodeGen kbWebServices kbbug KB815131 kbAudDeveloper