Microsoft KB Archive/317427

= 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 =

Article ID: 317427

Article Last Modified on 3/10/2006

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * 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.



Steps to Reproduce the Behavior
 Create a new Web service project in Microsoft Visual Basic .NET. On the View menu, click Server Explorer, and then connect to the Microsoft SQL Server Northwind sample database.</li> Drag the Categories table to Service1.asmx.vb. Notice that two controls (SqlConnection1 and SqlDataAdapter1) are added to the Web Form.</li> 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.</li>  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 </li> On the Build menu, click Build Web Service1 to build the Web service.</li> Create a new Visual Basic Windows Application project, and name it TestClient.</li> Follow these steps to add a Web reference to the service project: <ol style="list-style-type: lower-alpha;"> In Solution Explorer, on the Project menu, click Add Web Reference.</li> 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.</li> 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.</li></ol> </li> 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.</li> 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.</li>  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 </li> 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.</li>  To resolve this problem, comment the following line of code: DataSet11 = x.getdata and uncomment one of the workarounds. </li></ol>

Additional query words: no data in DataGrid

Keywords: kbsqlclient kbsystemdata kbdataadapter kbprb KB317427

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.