0

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

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.

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.

I'm programming in c# and using Sql Server 2008.

How can I solve this?

Thank you all

Here are some more info:

this is my sql command:

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 < 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);

and here is c# code for creating document object:

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;

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)

So, here is the error that I get:

The parameterized query '(@GlassFK int,@FaiFK int,@ProjectOrderFK int,@OrderNumberFK nvar' expects the parameter '@OrderNumberFK', which was not supplied.

OrderNumberID is the primary key of table Project. Its foreign keys is OrderNumberFK

4

0 回答 0