Microsoft KB Archive/819346

= You receive an &quot;Object reference not set to an instance of an object&quot; exception for the DataGrid control in Visual Basic .NET =

Article ID: 819346

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



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).



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.
 * 1) On the View menu, click Server Explorer.
 * 2) Right-click Data Connections, and then click Add Connection.
 * 3) Click the Connection tab, type your local Microsoft SQL Server name, and then click to select Northwind.
 * 4) On the Data Connections window, expand your local data connection.
 * 5) Expand Tables.
 * 6) In Tables, drag the Employees table to Form1.

By default, SqlDataAdapter1 and SqlConnection1 are created.
 * 1) Right-click SqlDataAdapter1, and then click Generate DataSet.
 * 2) On the Generate DataSet window, click OK.

By default, DataSet11 is created.
 * 1) From the toolbox, drag a DataGrid control to Form1.
 * 2) Right-click the DataGrid control, and then click Properties.
 * 3) In the Properties window, set the DataSource property to DataSet11.

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

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

The DataMember property is set to Employees.Photo, and an exception is displayed in DataGrid1.
 * 1) 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.

