You cannot obtain data by using the DataGrid component when you bind a DataGrid control to a DataSet object that is returned from a Web service (317427)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition

This article was previously published under Q317427

SYMPTOMS

The DataGrid control may not contain data under the following circumstances:
  • You bind a DataGrid control to an ADO.NET DataSet object by using the Form designer, and the designer creates an instance of a DataSet on the form. -and-

  • You set that instance variable to a DataSet that a Web method returns through a Web service in the Load event handler.

CAUSE

This problem occurs because the DataGrid control is still bound to the old instance of the DataSet on the form, not the new instance that the Web service returns.

RESOLUTION

To work around this problem, use one of the following methods:
  • Retrieve the DataSet that the Web service returns, and then merge this DataSet into the DataSet that is bound to the DataGrid control. For example, if DataSet11 is the instance variable that is bound to the DataGrid control, use the following code to merge DataSet11 into the DataSet that is bound to the DataGrid control:
    Dim myDS As New localhost.DataSet1()
    myDS = x.getdata
    DataSet11.Merge(myDS)
    					
  • Reset the DataSource property of the DataGrid control to point to the new instance of the DataSet that the Web service returns. For example:
    DataGrid1.DataSource = x.getdata.Categories
    					

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Web service project in Microsoft Visual Basic .NET.
  2. On the View menu, click Server Explorer, and then connect to the Microsoft SQL Server Northwind sample database.
  3. Drag the Categories table to Service1.asmx.vb. Notice that two controls (SqlConnection1 and SqlDataAdapter1) are added to the Web Form.
  4. On the Data menu, click Generate Dataset to generate a new DataSet. Click to select the Add this to the designer check box to include an instance in the designer.
  5. Right-click the Web Form, and then click View Code. Add the following code to Public Class Service1:
    <WebMethod()> Public Function getdata() As DataSet1
            Me.SqlDataAdapter1.Fill(DataSet11)
            Return DataSet11
        End Function
    					
  6. On the Build menu, click Build Web Service1 to build the Web service.
  7. Create a new Visual Basic Windows Application project, and name it TestClient.
  8. Follow these steps to add a Web reference to the service project:
    1. In Solution Explorer, on the Project menu, click Add Web Reference.
    2. In the Add Web Reference dialog box, type the URL for the XML Web service in the Address box, or select the URL from the list in the Address box, and then click the arrow button.
    3. Verify that the items in the Available References box are the items that you want to reference in your project, and then click Add Reference.
  9. Drag a DataSet control from the Data portion of the toolbox to the form. In the Add Dataset dialog box, click Typed dataset, and then click TestClient.localhost.DataSet1 in the Name list.
  10. Drag a DataGrid control from the toolbox to the form, and then set the DataSource property of the DataGrid to DataSet11.Categories in the Properties window.
  11. Add the following code to the Form Load event handler:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim x As New localhost.Service1()
    
            'Reset the instance variable DataSet11 to the DataSet that the 
            'Web service returns. To test the workaround, comment out this line.
            DataSet11 = x.getdata
    
            'Workaround 1: Retrieve the data, and then merge the data into DataSet11.
            'Dim myDS As New localhost.DataSet1()
            'myDS = x.getdata
            'DataSet11.Merge(myDS)
    
            'Workaround 2: Update the DataSource property of DataGrid1.
            'DataGrid1.DataSource = x.getdata.Categories
    
    End Sub
    					
  12. Compile and run the application. Notice that the DataGrid is not populated with data. If you insert a breakpoint in the End Sub of the Load event of the form, the DataSet is displayed correctly in the Watch window.
  13. To resolve this problem, comment the following line of code:
    DataSet11 = x.getdata
    						
    and uncomment one of the workarounds.

Modification Type:MinorLast Reviewed:3/10/2006
Keywords:kbDataAdapter kbprb kbSqlClient kbSystemData KB317427