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

Article translations Article translations
Article ID: 833902 - View products that this article applies to.
Expand all | Collapse all

On This Page

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

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.

MORE INFORMATION

Steps to reproduce the behavior

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#.
  4. Under Templates, click Windows Application.
  5. In the Name box, type the name of the project.
  6. In the Location box, type the location where you want to save the project.
  7. Click OK. By default, Form1.cs is added to the project.

Add a Data Form to the Windows application

  1. 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
  2. Under Templates, click Data Form Wizard.
  3. In the Name box, type the name of the Data Form.
  4. Click Open. The welcome screen of the Data Form Wizard appears.
  5. Click Next.
  6. 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.
  7. On the Choose a data connection page, click New Connection. The Data Link Properties dialog box appears.
  8. Set up a connection to the database from the Data Form Wizard:
    1. In the Data Link Properties dialog box, click the Connection tab.
    2. In the Select or enter a server name box, select the name of the server where your database is located.
    3. Under Enter information to log on to the server, click to select the Use Windows NT Integrated security option.
    4. In the Select the database on the server box, select the database that you want to use.
    5. Click OK.
  9. Click Next.
  10. On the Choose tables or views page, select the tables that you want to use.
  11. Click Next.
  12. 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.
  13. 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.
  14. Click Finish.

Set the Data Form as the Startup Object of the project

  1. In Solution Explorer, click Form1.cs.
  2. On the View menu, click Code.
  3. 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()
  4. In the Main function, replace the existing code with the following code.
    Application.Run(new DataForm1());

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 "Symptoms" section of this article.
  5. Click the navigation buttons to move through the records. You receive the error message that appears in the "Symptoms" section of this article.

REFERENCES

For more information about the Data Form Wizard, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/aa291437(VS.71).aspx

Properties

Article ID: 833902 - Last Review: April 24, 2007 - Revision: 2.4
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
Keywords: 
kbvs2005swept kbvs2005applies kberrmsg kbdatabase kbwindowsforms kbprb KB833902

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com