You receive an "Object reference not set to an instance of an object" exception for the DataGrid control in Visual Basic .NET (819346)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2002)

SYMPTOMS

When you have a DataGrid control on a Microsoft Windows Form, and you set the DataMember property of the DataGrid to a column with binary data, you receive the following exception:
Object reference not set to an instance of an object
The DataMember property is set to the column, and the System.NullReferenceException exception is displayed in the DataGrid. Also, when you set the DataMember property to any other value in the list, the DataGrid does not refresh, and the DataGrid displays the exception instead the value that you selected.

CAUSE

This issue occurs because Microsoft Visual Basic .NET does not permit you to set the DataMember property of the DataGrid to a DataColumn that has binary data. Therefore, the DataGrid displays the exception instead of the value that you selected.

The DataGrid displays the exception even after you set the DataMember property to a value that is not a binary data value. The form is not repainted after you change the DataMember property of the DataGrid. Therefore, the DataGrid displays the exception that it receives when you set the DataMember to a DataColumn that has binary data.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This problem was corrected in Microsoft Visual Studio .NET (2003).

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Microsoft Visual Studio .NET Windows Application project by using Microsoft Visual Basic .NET or Microsoft Visual C# .NET.

    By default, Form1 is created.
  2. On the View menu, click Server Explorer.
  3. Right-click Data Connections, and then click Add Connection.
  4. Click the Connection tab, type your local Microsoft SQL Server name, and then click to select Northwind.
  5. On the Data Connections window, expand your local data connection.
  6. Expand Tables.
  7. In Tables, drag the Employees table to Form1.

    By default, SqlDataAdapter1 and SqlConnection1 are created.
  8. Right-click SqlDataAdapter1, and then click Generate DataSet.
  9. On the Generate DataSet window, click OK.

    By default, DataSet11 is created.
  10. From the toolbox, drag a DataGrid control to Form1.
  11. Right-click the DataGrid control, and then click Properties.
  12. In the Properties window, set the DataSource property to DataSet11.

    Note Do not click any other item before you click DataSet11.
  13. Set the DataMember property to Photo.

    Note Do not click any other DataMember before you click Photo.
  14. In the Microsoft Development Environment error message box, click OK.
  15. In the Properties Window, click Cancel.

    The DataMember property is set to Employees.Photo, and an exception is displayed in DataGrid1.
  16. In the Properties window, set the DataMember property to Employees.

    DataGrid1 displays the exception instead of displaying the Employees table.

    Note To resolve this problem, close and reopen Form1.

REFERENCES

For more information, visit the following MSDN Web site:

Modification Type:MinorLast Reviewed:2/3/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbfix kbProperties kbDesigner kbWindowsForms kbDataBinding kbCtrl kbControl KB819346 kbAudDeveloper