title: C# System.Data.SqlClient.SqlException Cannot insert the value NULL into column ERROR FOREIGN KEY
body: <p>Error Message showing up after trying to call foreign key, that is a primary key in another table.</p>
  
  <p>There's two tables and whats supposed to link them is the foreign key.</p>
  
  <p>The SQL Server database does not allow, making the identity of both the primary key and the foreign key to true so it can auto increment</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException   HResult=0x80131904   Message=Cannot insert the value NULL into column 'client_ID', table ; column does not allow nulls. </p>
  </blockquote>
  
  <p>Code for the database class:</p>
  
  <pre><code>public static void AddVehicle(string serial_Number, string Make, string Model, string Year, string Color)
  {
          string insStmt = "INSERT INTO VEHICLE (serial_Number, Make,  Model, Year, Color) VALUES (@serial_Number, @Make, @Model, @Year, @Color)";
          SqlConnection conn = GetConnection();
          SqlCommand insCmd = new SqlCommand(insStmt, conn);
          insCmd.Parameters.AddWithValue("@serial_Number", serial_Number);
          insCmd.Parameters.AddWithValue("@Make", Make);
          insCmd.Parameters.AddWithValue("@Model", Model);
          insCmd.Parameters.AddWithValue("@Year", Year);
          insCmd.Parameters.AddWithValue("@Color", Color);
  
          try 
          { 
               conn.Open(); 
               insCmd.ExecuteNonQuery(); 
          }
          catch (SqlException ex) 
          {
               throw ex; 
          }
          finally 
          { 
               conn.Close(); 
          }
  }
  
  public static List&lt;Vehicle&gt; GetVehicle()
  {
          List&lt;Vehicle&gt; vehicleList = new List&lt;Vehicle&gt;();
          SqlConnection conn = GetConnection();
          string selStmt = "SELECT * FROM VEHICLE ORDER BY Make, Model";
          SqlCommand selcCmd = new SqlCommand(selStmt, conn);
  
          try
          {
              conn.Open();
              SqlDataReader reader = selcCmd.ExecuteReader();
              while (reader.Read())
              {
                  Vehicle vehicle = new Vehicle();
  
                  //vehicle.Vehicle_ID = (int)reader["Vehicle_ID"];
                  vehicle.Vehicle_ID = reader["Vehicle_ID"].ToString();
                  vehicle.Serial_Number = reader["Serial_Number"].ToString();
                  vehicle.MAke = reader["MAke"].ToString();
                  vehicle.MOdel = reader["MOdel"].ToString();
                  vehicle.YEar = reader["YEar"].ToString();
                  vehicle.COlor = reader["COlor"].ToString();
                  vehicle.ClientID = (int)reader["ClientID"];
                  vehicleList.Add(vehicle);
              }
              reader.Close();
          }
          catch (SqlException ex) { throw ex; }
          finally { conn.Close(); }
  
          return vehicleList;
  }
  </code></pre>
  
  <p>Code for the windows form is:</p>
  
  <pre><code>private void Form2_Load(object sender, EventArgs e)
  {
          listView.Items.Clear();
          List&lt;Vehicle&gt; vehicleList;
  
          try
          {
              vehicleList = CARDB.GetVehicle();
  
              if (vehicleList.Count &gt; 0)
              {
                  Vehicle vehicle;
  
                  for (int i = 0; i &lt; vehicleList.Count; i++)
                  {
                      vehicle = vehicleList[i];
                      //listView.Items.Add(vehicle.Vehicle_ID.ToString());
                      listView.Items[i].SubItems.Add(vehicle.Vehicle_ID);
                      listView.Items[i].SubItems.Add(vehicle.Serial_Number);
                      listView.Items[i].SubItems.Add(vehicle.MAke);
                      listView.Items[i].SubItems.Add(vehicle.MOdel);
                      listView.Items[i].SubItems.Add(vehicle.YEar);
                      listView.Items[i].SubItems.Add(vehicle.COlor);
                      listView.Items.Add(vehicle.ClientID.ToString());
                  }
              }
              //else { MessageBox.Show("Enter client Details", "Alert:");}
          }
          catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); }
  }
  
  private void btnSubmit2_Click(object sender, EventArgs e)
  {
          CARDB.AddVehicle(txtSerialNumber.Text, txtMake.Text, txtModel.Text, txtYear.Text, txtColor.Text);
          txtSerialNumber.Text = "";
          txtMake.Text = "";
          txtModel.Text = "";
          txtYear.Text = "";
          txtColor.Text = "";
  
          this.Form2_Load(this, null);
  }
  </code></pre>
  
  <p>SQL code for the table is:</p>
  
  <pre><code>CREATE TABLE [dbo].[Vehicle] 
  (
      [Vehicle_ID]    INT IDENTITY (1, 1) NOT NULL,
      [client_ID]     INT           NOT NULL,
      [Serial_Number] NVARCHAR(MAX) NULL,
      [Make]          NVARCHAR(MAX) NULL,
      [Model]         NVARCHAR(MAX) NULL,
      [Year]          NVARCHAR(MAX) NULL,
      [Color]         NVARCHAR(MAX) NULL,
  
      PRIMARY KEY CLUSTERED ([Vehicle_ID] ASC),
      CONSTRAINT [FK_Vehicle_Client] 
           FOREIGN KEY ([client_ID]) REFERENCES [dbo].[Client] ([clientID])
  );
  </code></pre>