Cannot insert the value NULL into column 'ID', table
<p>I have a table with a primary and foreign key of <code>ID</code>, primary key <code>id</code> from <code>LeaveCategory</code> and foreign key = <code>LeaveCategoryId</code>. But I get this error:</p>

<blockquote>
  <p>Cannot insert the value NULL into column 'ID', table 'dbo.Action'; column does not allow nulls. INSERT fails    </p>
</blockquote>

<p>But I really don't know how to fix this problem.</p>

<p>This is my error:</p>

<p><a href="http://i.stack.imgur.com/vuh36.jpg" rel="nofollow">click image</a></p>

<p>This is code:</p>

<pre><code>public partial class AddLeaveType : System.Web.UI.Page
{
    int id = 0;

    SqlConnection conn = null;
    SqlCommand cmd = null;

    string connectionString = null;

    protected void Page_Load(object sender, EventArgs e)
    {
            if (!IsPostBack)
            {
                SqlDataReader dr = null;

                connectionString = ConfigurationManager.ConnectionStrings["LeaveManagementCS"].ConnectionString;

                conn = new SqlConnection(connectionString);

                string sql = "SELECT * FROM LeaveCategory";

                try
                {
                    cmd = new SqlCommand(sql, conn);

                    conn.Open();

                    dr = cmd.ExecuteReader();

                    while(dr.Read())
                    {
                        ListItem item = new ListItem(dr["Category"].ToString(), dr["Id"].ToString());
                        ddlCategory.Items.Add(item);
                    }

                    dr.Close();
                }
                catch (Exception ex)
                {
                    lblOutput.Text = "Error Message: " + ex.Message;
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
            }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        connectionString = ConfigurationManager.ConnectionStrings["LeaveManagementCS"].ConnectionString;

        conn = new SqlConnection(connectionString);

        string sql = "INSERT INTO LeaveType (Type, Description, NumOfDays) ";
        sql += "VALUES (@type, @desc, @nod)";

        try
        {
                cmd = new SqlCommand(sql, conn);

                cmd.Parameters.AddWithValue("@type", tbType.Text);
                cmd.Parameters.AddWithValue("@desc", tbDesc.Text);
                cmd.Parameters.AddWithValue("@nod", tbNod.Text);

                conn.Open();

                int rows = cmd.ExecuteNonQuery();

                if (rows &gt; 0)
                {
                    lblOutput.Text = "Added successfully";
                }
        }
        catch (Exception ex)
        {
            lblOutput.Text = "Error Message:" + ex.Message;
        }
        finally
        {
            if (conn != null)
                conn.Close();
        }
    }
}
</code></pre>

<p>Correct output:</p>

<p><a href="http://i.stack.imgur.com/U3bWF.jpg" rel="nofollow">click image</a></p>