Microsoft KB Archive/833902

= You receive a &quot;Column 'Column Name' does not allow nulls&quot; message when you add a record or when you move through records in a database by using a data form =

Article ID: 833902

Article Last Modified on 4/24/2007

-

APPLIES TO


 * Microsoft Visual Studio 2005 Professional Edition
 * Microsoft Visual Studio 2005 Standard Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * 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
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





SYMPTOMS
In a Microsoft Windows application, if you use a data form to add a new record to a database, you may receive the following message:

Column ' ' does not allow nulls.

Note  is a placeholder for the name of the column of a table in the database where the primary key constraint exists.

If you use the navigation buttons to move through the records of a database table on the data form, you may receive the following error message:

An unhandled exception of type 'System.Data.NoNullAllowedException' occurred in system.data.dll Additional information: Column ' ' does not allow nulls.



CAUSE
This behavior occurs because the primary key or the not NULL column of the database table was not selected in the Data Form Wizard. If the primary key or the not NULL column of the database table is not selected in the Data Form Wizard when you create the data form, the Add - Creates a new record option is not disabled.



WORKAROUND
To work around this problem, select the following columns of the selected table when you add a data form by using the Data Form Wizard:
 * The primary column
 * A column that does not permit NULLS



STATUS
This behavior is by design.



Create a Microsoft Windows application

 * 1) Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
 * 2) On the File menu, point to New, and then click Project. The New Project dialog box appears.
 * 3) Under Project Types, click Visual C# Projects.

Note In Visual Studio 2005, click Visual C#.
 * 1) Under Templates, click Windows Application.
 * 2) In the Name box, type the name of the project.
 * 3) In the Location box, type the location where you want to save the project.
 * 4) Click OK. By default, Form1.cs is added to the project.

Add a Data Form to the Windows application
 In Solution Explorer, right-click the project, point to Add, and then click Add New Item. The Add New Item dialog box appears.

Note In Visual Studio 2005, click New Items. The New Items dialog box appears Under Templates, click Data Form Wizard. In the Name box, type the name of the Data Form. Click Open. The welcome screen of the Data Form Wizard appears. Click Next.</li> On the Choose the dataset you want to use page, type a dataset name in the Create a new dataset named box, and then click Next.</li> On the Choose a data connection page, click New Connection. The Data Link Properties dialog box appears.</li> Set up a connection to the database from the Data Form Wizard: <ol style="list-style-type: lower-alpha;"> In the Data Link Properties dialog box, click the Connection tab.</li> In the Select or enter a server name box, select the name of the server where your database is located.</li> Under Enter information to log on to the server, click to select the Use Windows NT Integrated security option.</li> In the Select the database on the server box, select the database that you want to use.</li> Click OK.</li></ol> </li> Click Next.</li> On the Choose tables or views page, select the tables that you want to use.</li> Click Next.</li> On the Choose tables and columns to display on the form page, click to clear the check box for the primary key on the column that appears in the Columns box, and then click Next.

Note You can also click to clear the check box for the column that does not permit NULLS.</li> On the Choose the display style page, click to select the Single record in individual controls option. Notice that the Add-Creates a new record option is not disabled.</li> Click Finish.</li></ol>

Set the Data Form as the Startup Object of the project
<ol> In Solution Explorer, click Form1.cs.</li> On the View menu, click Code.</li> <li> In the code view of Form1.cs, locate the following line of code. This line is the main entry point for the application. static void Main </li> <li> In the Main function, replace the existing code with the following code. Application.Run(new DataForm1); </li></ol>

Verify the Data Form

 * 1) On the Debug menu, click Start. DataForm1 appears.
 * 2) Click Load. The records from the table that is selected in the database appear in the controls.
 * 3) Click Add. All the controls on DataForm1 are cleared.
 * 4) Type valid data in the controls that appear on DataForm1, and then click Update. You receive the message that appears in the &quot;Symptoms&quot; section of this article.
 * 5) Click the navigation buttons to move through the records. You receive the error message that appears in the &quot;Symptoms&quot; section of this article.

<div class="references_section">