Microsoft Knowledge Base Email Alertz

KBAlertz.com: The

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **


Community Site



We Send hundreds of thousands of emails using ASP.NET Email


ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








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

On This Page

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.

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
Keywords: 
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