title: C# how to insert null value for foreign key into SQL
body: <p>I have one table Documentation that has foreign keys to 3 other tables. This table, Documentation, should contain documents for different subjects (glass, aircraft and project). I didn't wan't to create 3 tables (table for documents for glass, another table for aircraft documents etc) so I created just one table that will have a reference(foreign keys) to other tables (Glass, Aircraft and Project tables). First of all, I'm not sure if this is a good solution...</p>
  
  <p>Anyway, problem is the following: I want to insert document that refers to glass. This means that foreign keys for Aircraft and Project will be null. Database columns accept null values.
  If I try to insert null values for those foreign keys (AircraftFK and ProjectFK) it does not allow me to do that. It's asking me to insert appropriate value for foreign key that already exists as primary key in tabel Aircraft and Project.</p>
  
  <p>These foreign keys (AircraftfK and ProjectFK) HAVE TO BE NULL because the document that I'm inserting has nothing to do with aircraft and project.</p>
  
  <p>I'm programming in c# and using Sql Server 2008.</p>
  
  <p>How can I solve this?</p>
  
  <p>Thank you all</p>
  
  <p>Here are some more info:</p>
  
  <p>this is my sql command:</p>
  
  <pre><code>komanda = new SqlCommand("insert into Documentation (GlassFK, FaiFK, ProjectOrderFK, OrderNumberFK, DocPath, DocName, DocSize, DocType, DocumentFile) values (@GlassFK, @FaiFK, @ProjectOrderFK, @OrderNumberFK, @DocPath, @DocName, @DocSize, @DocType, @DocumentFile)", konekcija);
  
          for (int i = 0; i &lt; listaDokumenata.Count; i++)
          {
              komanda.Parameters.Clear();
              komanda.Parameters.AddWithValue("@GlassFK", listaDokumenata[i].GlassFK);
              komanda.Parameters.AddWithValue("@FaiFK", listaDokumenata[i].FaiFK);
              komanda.Parameters.AddWithValue("@ProjectOrderFK", listaDokumenata[i].ProjectOrderFK);
              komanda.Parameters.AddWithValue("@OrderNumberFK", listaDokumenata[i].OrderNumberFK);
              komanda.Parameters.AddWithValue("@DocPath", listaDokumenata[i].PutanjaFajla);
              komanda.Parameters.AddWithValue("@DocName", listaDokumenata[i].ImeFajla);
              komanda.Parameters.AddWithValue("@DocSize", listaDokumenata[i].VelicinaFajla);
              komanda.Parameters.AddWithValue("@DocType", listaDokumenata[i].Ekstenzija);
              komanda.Parameters.AddWithValue("@DocumentFile", listaDokumenata[i].Fajl);
  </code></pre>
  
  <p>and here is c# code for creating document object:</p>
  
  <pre><code>Document sf = new Document();
                      sf.PutanjaFajla = putanjaFajla;
                      sf.ImeFajla = imeFajla;
                      sf.Ekstenzija = ekstenzija;
                      sf.VelicinaFajla = velicinaFajla;
                      sf.Fajl = wordFile;
                      sf.FaiFK = 5;
                      sf.GlassFK = 0;
                      sf.ProjectFK = null;
                      sf.ProjectOrderFK = 0;
  </code></pre>
  
  <p>This means that this document will refer to Glass. All other foreign keys has to be 0 or null (depends on a attribute type in sql)</p>
  
  <p>So, here is the error that I get:</p>
  
  <p>The parameterized query '(@GlassFK int,@FaiFK int,@ProjectOrderFK int,@OrderNumberFK nvar' expects the parameter '@OrderNumberFK', which was not supplied.</p>
  
  <p>OrderNumberID is the primary key of table Project. Its foreign keys is OrderNumberFK</p>