简体   繁体   中英

Can not Insert the NULL value into Auto Increment PrimaryKey Column

I have created the table Items in C# database as the property of the primary key is AutoIncrement=True, AutoIncrementStep=1, Unique=True, AutoIncrementSeeds=0;

CREATE TABLE [dbo].[Items] 
(
    [item_id]       INT          NOT NULL,
    [item_name]     VARCHAR (50) NULL,
    [item_model]    VARCHAR (50) NULL,
    [item_price]    VARCHAR (50) NULL,
    [item_quantity] VARCHAR (50) NULL,
    [entry_date]    VARCHAR (50) NULL,
    [user_id]       VARCHAR (50) NULL,

    PRIMARY KEY CLUSTERED ([item_id] ASC)
);

and I am inserting data into the Items table using this code:

string query = @"Insert into Items(item_name, item_model, item_price, item_quantity, entry_date, user_id)" +
                "VALUES(@name, @model, @price, @quantity, @date, @user)";

using (SqlConnection c = new SqlConnection(Properties.Settings.Default.Database1ConnectionString))
{
    try
    {
        c.Open();

        if (c.State == ConnectionState.Open)
        {
            SqlCommand command1 = new SqlCommand(query, c);

            command1.Parameters.AddWithValue("@name", name.Text.ToString());
            command1.Parameters.AddWithValue("@model", model.Text.ToString());
            command1.Parameters.AddWithValue("@price", price.Text.ToString());
            command1.Parameters.AddWithValue("@quantity", quantity.Text.ToString());
            command1.Parameters.AddWithValue("@date", dateTimePicker1.Text.ToString());
            command1.Parameters.AddWithValue("@user", added_by.Text.ToString());

            int k = command1.ExecuteNonQuery();

            if (k > 0)
            {
                MessageBox.Show("Data Added Successfully");
                c.Close();
                return;
            }
            else 
            {
                MessageBox.Show("Data was not added successfully - try again later");
                c.Close();
                return;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Failed To Open Connection :." + ex);
        return;
    }
}

When I run code and try to insert data I get error

Cannot Insert NULL value into column 'item_id' table

NOT NULL means you can't insert NULL into those columns you need to explicitly insert it.

Setting IDENTITY(1,1) will insert item_id automatically by seed of 1.

Change your definition like this

CREATE TABLE [dbo].[Items] (
[item_id]       INT          NOT NULL IDENTITY(1, 1),
[item_name]     VARCHAR (50) NULL,
[item_model]    VARCHAR (50) NULL,
[item_price]    VARCHAR (50) NULL,
[item_quantity] VARCHAR (50) NULL,
[entry_date]    VARCHAR (50) NULL,
[user_id]       VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([item_id] ASC)
);

OR

explicitly insert it into database

string query = @"Insert into Items(item_id,item_name,item_model,item_price,item_quantity,entry_date,user_id)" +
                "VALUES(@item_id,@name, @model, @price, @quantity, @date,@user)";

Stop using .AddWithValues Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM