bonjour tout le monde,

ça fait plusieurs jours que j'essaie d’exécuter une transaction à partir des TableAdapters, mais sans réussir.

ma transaction consiste à l'enregistrement d'une Vente, ses Détails et la mis à jour du stock. Elle se compose des instructions suivantes:


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 
              private void ExecuteVenteTransaction()
        {
            //DataTables for manipulation
            DTSPharmacie.VenteDataTable ventesDT = new DTSPharmacie.VenteDataTable();
            DTSPharmacie.DetailsVenteDataTable detailsVenteDT = new DTSPharmacie.DetailsVenteDataTable();
            DTSPharmacie.ProduitDataTable produitDT = new DTSPharmacie.ProduitDataTable();
            //DataTableAdapters
            DTSPharmacieTableAdapters.VenteTableAdapter venteTA = new DTSPharmacieTableAdapters.VenteTableAdapter();
            DTSPharmacieTableAdapters.DetailsVenteTableAdapter detailsVenteTA = new DTSPharmacieTableAdapters.DetailsVenteTableAdapter();
            DTSPharmacieTableAdapters.ProduitTableAdapter produitTA = new DTSPharmacieTableAdapters.ProduitTableAdapter();
 
            //Add new row to Orders table
            DTSPharmacie.VenteRow ventesRow = ventesDT.NewVenteRow();
            ventesRow.DateVente = DateTime.Now ;
            ventesRow.Montant = float.Parse(txtTotal.Text);
            ventesRow.NomClient = "Zone nom du client";
            ventesDT.AddVenteRow(ventesRow);
 
            //Setup connection
            conn = new SqlConnection(Properties.Settings.Default.DBPharmacieConnectionString);
            conn.Open();
 
            //Setup transaction
            transaction = conn.BeginTransaction();
            HelperTA.SetTransaction(venteTA, transaction);
            HelperTA.SetTransaction(detailsVenteTA, transaction);
            HelperTA.SetTransaction(produitTA, transaction);
 
            produitTA.Fill(produitDT);
 
            //Update Orders table
            venteTA.Update(ventesDT);
 
            //Add new row to OrderDetails
            foreach (DataGridViewRow dgvr in dgvVente.Rows)
            {
                //if(dgvr.
                DTSPharmacie.DetailsVenteRow detailsVenteRow = detailsVenteDT.NewDetailsVenteRow();
                detailsVenteRow.NumProd = (int)dgvr.Cells["NumProdVente"].Value;
                detailsVenteRow.PUA = (float)dgvr.Cells["PUAMVente"].Value;
                detailsVenteRow.PUV = (float)dgvr.Cells["PUVVente"].Value; ;
                detailsVenteRow.Quantite = (int)dgvr.Cells["quantiteVente"].Value;
                detailsVenteRow.Montant = detailsVenteRow.PUV * detailsVenteRow.Quantite;
                detailsVenteRow.NumVente = ventesRow.NumVente;
                detailsVenteDT.AddDetailsVenteRow(detailsVenteRow);
                foreach (DTSPharmacie.ProduitRow pr in produitDT)
                {
                    if (pr.NumProd == (int)dgvr.Cells["NumProdVente"].Value)
                    {
                        pr.Quantite = pr.Quantite - (int)dgvr.Cells["quantiteVente"].Value;
                        pr.EndEdit();
                        produitTA.Update(pr);
                    }
                }
            }
            //Update DetailsVente table
            detailsVenteTA.Update(detailsVenteDT);
            //Commit transaction
            transaction.Commit();
 
        }
ET c'est MON erreur :
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_DetailsVente_Produit". The conflict occurred in , table "dbo.Produit", column 'NumProd'.