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 'Column Name' does not allow nulls.
Note Column Name 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 'Column Name' does not allow nulls.
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.
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
This behavior is by design.
Steps to reproduce the behavior
Create a Microsoft Windows application
Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
On the File menu, point to New, and then click Project. The New Project dialog box appears.
Under Project Types, click Visual C# Projects.
Note In Visual Studio 2005, click Visual C#.
Under Templates, click Windows Application.
In the Name box, type the name of the project.
In the Location box, type the location where you want to save the project.
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.
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.
On the Choose a data connection page, click New Connection. The Data Link Properties dialog box appears.
Set up a connection to the database from the Data Form Wizard:
In the Data Link Properties dialog box, click the Connection tab.
In the Select or enter a server name box, select the name of the server where your database is located.
Under Enter information to log on to the server, click to select the Use Windows NT Integrated security option.
In the Select the database on the server box, select the database that you want to use.
On the Choose tables or views page, select the tables that you want to use.
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.
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.
Set the Data Form as the Startup Object of the project
In Solution Explorer, click Form1.cs.
On the View menu, click Code.
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()
In the Main function, replace the existing code with the following code.
Verify the Data Form
On the Debug menu, click Start. DataForm1 appears.
Click Load. The records from the table that is selected in the database appear in the controls.
Click Add. All the controls on DataForm1 are cleared.
Type valid data in the controls that appear on DataForm1, and then click Update. You receive the message that appears in the "Symptoms" section of this article.
Click the navigation buttons to move through the records. You receive the error message that appears in the "Symptoms" section of this article.
For more information about the Data Form Wizard, visit the following Microsoft Developer Network (MSDN) Web site:
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