Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 317427 - Last Review: March 10, 2006 - Revision: 1.2
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
This article was previously published under Q317427
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.
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.
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
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.
- Drag the Categories table to Service1.asmx.vb. Notice that two controls (SqlConnection1 and SqlDataAdapter1) are added to the Web Form.
- 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.
- 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
- On the Build menu, click Build Web Service1 to build the Web service.
- Create a new Visual Basic Windows Application project, and name it TestClient.
- Follow these steps to add a Web reference to the service project:
- In Solution Explorer, on the Project menu, click Add Web Reference.
- 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.
- 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.
- 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.
- 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.
- 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
- 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.
- To resolve this problem, comment the following line of code:
and uncomment one of the workarounds.
APPLIES TO
- Microsoft ADO.NET (included with the .NET Framework)
- 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
| kbdataadapter kbprb kbsqlclient kbsystemdata KB317427 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please