1
votes

I have the following error when I call the update method on my dataAdapter, after deleting some rows :

The DELETE statement conflicted with the REFERENCE constraint "FK_Eleve_Classe". The conflict occurred in database "hogwarts", table "dbo.Eleve", column 'FK_classID'. The statement has been terminated.

Indeed, there is a foreign key defined, but since the primary key is in the "classe" table, why doesn't it allow me to delete my row in "eleve"?

Maybe does it try to delete in cascade? (then how to change this?)

edit 1 : SQL

CREATE TABLE [dbo].[Classe](
    [id] [int] NOT NULL,
 CONSTRAINT [PK_Classe] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[Eleve](
    [id] [int] NOT NULL,
    [nom] [varchar](50) NULL,
    [prenom] [varchar](50) NULL,
    [birthdate] [date] NULL,
    [FK_classID] [int] NULL,
 CONSTRAINT [PK_Eleve] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]



ALTER TABLE [dbo].[Eleve]  WITH CHECK ADD  CONSTRAINT [FK_Eleve_Classe] FOREIGN KEY([FK_classID])
REFERENCES [dbo].[Classe] ([id])

Edit 2 : some c# code

 DataSet ds = new DataSet();

 c.Open(); // SqlConnection


 SqlDataAdapter da = new SqlDataAdapter("Select * from eleve", (SqlConnection)c);
 da.Fill(ds, "eleves");
 da.SelectCommand = new SqlCommand("select * from cours", (SqlConnection)c);
 da.Fill(ds, "COURS");
 da.SelectCommand = new SqlCommand("select * from professeur", (SqlConnection)c);
 da.Fill(ds, "PROF");
 da.SelectCommand = new SqlCommand("select * from classe", (SqlConnection)c);
 da.Fill(ds, "CLASSE");
 c.Close();


 ds.Tables["eleves"].Rows[0].Delete();

edit 3 : deleting

here is the way i delete my element in my table :

ds.Tables["eleves"].Rows[0].Delete();

must I set the deleteCommand manually in addition to this?


and yes, hogwarts since I'm making a sample to explains it a funny way ;)

Thanks,

KiTe

1
hogwarts database, lol - hunter

1 Answers

0
votes

Do you have a trigger? This is the usual cause of such errors.

If eleve is a child table with child rows of classes (eg 0 to many rows in eleve for each row in classe) then this error should not happen. Unless the FK is the wrong way around

BTW: a cascade is from parent to child.