Microsoft Knowledge Base Email Alertz

KBAlertz.com: (313156) - When you view an .aspx page that contains a DataGrid control, the data grid may not appear. Alternately, only the header of the data grid may appear, but the data grid may not contain any data.

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
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



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



Expert Web Design & Graphic Design
Design44.com

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
The ad says 3 - but 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: 313156 - Last Review: October 30, 2003 - Revision: 3.2

PRB: DataGrid Control Does Not Display Correctly in .aspx Page

This article was previously published under Q313156
This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Data.SqlClient

On This Page

SYMPTOMS

When you view an .aspx page that contains a DataGrid control, the data grid may not appear. Alternately, only the header of the data grid may appear, but the data grid may not contain any data.

CAUSE

Data Grid Does Not Appear

The data grid does not appear at all if:
  • You do not set the DataSource property of the DataGrid control. -or-

  • You do not call the DataBind method of the DataGrid control or the DataBind method of the Page object from your code.

Data Grid Header Appears But Data Grid Is Empty

The data grid header appears, but the data grid does not contain any data if:
  • The data source does not contain any data. -or-

  • You do not fill the dataset that is bound to the DataGrid with data at run time.

    NOTE: You must write code to explicitly fill the dataset so that the DataGrid is populated with data from the database at run time. You can do this when you use the designer to add the DataAdapter object and to generate a dataset at design time.
This problem occurs because the .aspx page retrieves the details of the header fields from the DataSetName.xsd file. The DataSetName.xsd file is created for each dataset when you generate a dataset at the design time. This file contains the Extensible Markup Language (XML) representation of the dataset. The .aspx page retrieves the header field details from this file. Therefore, the data grid header appears but the data grid is empty.

RESOLUTION

Data Grid Does Not Appear

To resolve this problem, follow these steps:
  1. Set the DataSource property of the DataGrid to point to the appropriate data source. For example:

    Visual Basic
    DataGrid1.DataSource = dataSet11.Tables("Authors")
    						
    Visual C#
    DataGrid1.DataSource = dataSet11.Tables["Authors"] ;
    						
    NOTE: If you programmatically retrieve the data from the data source, assign the data source to the DataSource property of the DataGrid.
  2. If you use the designer to add a DataAdapter to the Web form and to generate a dataset, open the DataGrid property page, and then select the appropriate DataSet or DataView object in the DataSource property.
  3. Call the DataBind method of the DataGrid control or the DataBind method of the Page object. For example:

    Visual Basic
    'Use one of the following lines.
    'Page.DataBind()
    DataGrid1.DataBind()
    						
    Visual C#
    //Use one of the following lines. 
    //Page.DataBind();
    DataGrid1.DataBind();
    					

Data Grid Header Appears But Data Grid Is Empty

To resolve this problem, follow these steps:
  1. Make sure your that your data source contains data and that your query returns data.
  2. Use a DataAdapter to fill the dataset. For example:

    Visual Basic
    sqlDataAdapter1.Fill(dataSet11, "Authors")
    						
    Visual C#
    sqlDataAdapter1.Fill(dataSet11,"Authors");
    					

STATUS

This behavior is by design.

MORE INFORMATION

You must bind the DataGrid Web server control to a data source through the DataSource property of the DataGrid to correctly render the data grid on the page. After you set the data source, you must also explicitly call the DataBind method of the DataGrid control or the DataBind method of the Page object from your code.

Steps to Reproduce the Behavior

Data Grid Does Not Appear

  1. Create a new ASP.NET Web Application project in Visual Basic .NET or Visual C# .NET.
  2. Drag a DataGrid control from the toolbox, and drop the control onto the form.
  3. In the designer, double-click the .aspx page to open the code-behind window. Add the following code to the top of the code-behind window to add the required namespace references:

    Visual Basic
    Imports System.Data
    Imports System.Data.SqlClient
    						
    Visual C#
    using System.Data.SqlClient;
    					
  4. Replace the code in the Form_Load event with the following code:

    Note You must change uid =<username> and pwd =<strong password> to the correct values before you run this code. Be sure that UID has the appropriate permissions to perform this operation on the database.

    Visual Basic
    Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
         'Write code to access the database.
         Dim myConnection As New SqlConnection("server=(local);database=pubs;uid=<username>;pwd=<strong password>")
         Dim myDataAdapter As New SqlDataAdapter("select * from authors", myConnection)
         Dim myDataSet As New DataSet()
         myConnection.Open()
         myDataAdapter.Fill(myDataSet)
    
        'Uncomment the following line to resolve this problem. 
        'DataGrid1.DataSource = myDataSet
        DataGrid1.DataBind()
    End Sub
    						
    Visual C#
    private void Page_Load(object sender, System.EventArgs e)
    {
    //Write code to access the database.
    SqlConnection myConnection = new SqlConnection("server=(local);database=pubs;uid=<username>;pwd=<strong password>");
    SqlDataAdapter myDataAdapter = new SqlDataAdapter("select * from authors", myConnection);
    DataSet myDataSet = new DataSet();
    myConnection.Open();
    myDataAdapter.Fill(myDataSet);
    
    //Uncomment the following line to resolve this problem.
    //DataGrid1.DataSource = myDataSet;
    DataGrid1.DataBind();
    }
    					
  5. Modify the connection string as appropriate for your environment.
  6. Build the project, and then view the .aspx page in the browser. Notice that data grid does not appear.

Data Grid Header Appears But Data Grid Is Empty

  1. Create a new ASP.NET Web Application project in Visual Basic .NET or Visual C# .NET.
  2. Drag a DataGrid control from the toolbox, and drop the control onto the form.
  3. Add a SqlDataAdapter control to the Web form. Complete the following steps in the DataAdapter Configuration Wizard to set the connection properties:
    1. Click New Connection to create a new connection to your Microsoft SQL Server database. Select the server, type the logon information, and then select the Pubs database.
    2. Under Query type, click SQL Statements, and then click Next.
    3. Type the following SQL statement:
      Select * From Authors
    4. Click Finish.
  4. Right-click the DataAdapter, and then click Generate Dataset.
  5. Set the DataSource property of the DataGrid to the dataset that is generated.
  6. In the designer, double-click the .aspx page to open the code-behind window. Add the following code to the Page_Load event:

    Visual Basic
    DataGrid1.DataBind()
    						
    Visual C#
    DataGrid1.DataBind();
    					
  7. Build the project, and then view the .aspx page in the browser. Notice that the header of the data grid appears, but the data grid does not contain any data.

REFERENCES

For more information about the DataGrid control and for samples that use this control, refer to the following Microsoft Web sites:
DataGrid
http://samples.gotdotnet.com/quickstart/aspplus/samples/webforms/ctrlref/webctrl/datagrid/doc_datagrid.aspx (http://samples.gotdotnet.com/quickstart/aspplus/samples/webforms/ctrlref/webctrl/datagrid/doc_datagrid.aspx)

Server-Side Data Access
http://samples.gotdotnet.com/quickstart/aspplus/doc/webdataaccess.aspx (http://samples.gotdotnet.com/quickstart/aspplus/doc/webdataaccess.aspx)

DataGrid Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControlsDataGridClassTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControlsDataGridClassTopic.asp)

APPLIES TO
  • Microsoft ASP.NET 1.0
  • Microsoft ASP.NET 1.1
Keywords: 
kbdatabinding kbprb kbservercontrols KB313156
       

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