-2

I do not know why it doesn't show the form?

 public static void Rezervacija(int voziloId, int kupacId)
  {
    SqlConnection konekcija = new SqlConnection();
    try
    {
        konekcija.ConnectionString = CONNECTION_STRING;
        konekcija.Open();

        string insertUpit = "INSERT INTO Iznajmljivanje(VoziloId, KupacId) "
                            + "VALUES(@VoziloId, @KupacId) ";

        SqlCommand komanda = new SqlCommand(insertUpit, konekcija);
        komanda.Parameters.Add("@VoziloId",voziloId);
        komanda.Parameters.Add("@KupacId", kupacId);

        komanda.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        konekcija.Close();
    }
}

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Iznajmljivanje_Kupac". The conflict occurred in database ... table "dbo.Kupac", column 'KupacId'. The statement has been terminated.

Izzy
  • 6,740
  • 7
  • 40
  • 84
user1798078
  • 15
  • 1
  • 3
  • 11
  • 3
    You are trying to insert a value that doesn't exist in the table referred to by the foreign key constraint. It is a little hard to say more, without knowing the table definitions or contents. – Gordon Linoff Sep 15 '14 at 12:01

1 Answers1

3

You are trying to insert a value in the KupacId column which does not exist as a key in dbo.Kupac and there is a foreign key constraint on this relationship

This means that the value has to exist in dbo.Kupac (in I assume the Id column) if you want to insert it into Iznajmljivanje.KupacId

David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • I dont know how to fix it? Yes in Iznajmljivanje have KupacId – user1798078 Sep 15 '14 at 12:09
  • @user1798078 for `KupacId` column, choose values which exists in `dbo.Kupac` table. – Bharadwaj Sep 15 '14 at 12:19
  • @user1798078 I am sorry my friend, without seeing what you are trying to insert and what data is in the table, I cannot help you. It is not a code problem, it is a data issue. – David Pilkington Sep 15 '14 at 12:24
  • Ok i'm resolve this my code is past. But not show me values in my gridview Rezervacija. SELECT Kupac.Ime + ' ' + Kupac.Prezime AS PunoIme, Kupac.IdentifikacioniBroj, Vozilo.Marka, Vozilo.Model, Vozilo.RegistarskiBroj, Vozilo.Izdato, Iznajmljivanje.IznajmljivanjeId FROM Iznajmljivanje INNER JOIN Kupac ON Iznajmljivanje.KupacId = Kupac.KupacId INNER JOIN Vozilo ON Iznajmljivanje.VoziloId = Vozilo.VoziloId WHERE (Iznajmljivanje.DatumIznajmljivanja IS NULL) – user1798078 Sep 15 '14 at 12:25