Bonjour, j'ai un soucis avec mon projet de site e-commerce, je dois mettre à jour mon stock après la commande d'un client mais une fois que j'arrive au savechange() j'ai une erreur "la référence d'objet n'est pas défini à une instance d'objet". J'ai pourtant bien vérifier il n'y a rien de null il me récupère bien le produit et modifie bien le stock mais il ne veux pas m'enregistrer la modification (Je précise que je n'est pas de problème avec ma connexion à la bdd car juste avant il m'enregistre bien la commande en bdd.
Voici mon code pour la maj du stock.

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
 
protected void btnretour_Click(object sender, EventArgs e)
        {
            int idcom = (int)ViewState["idcom"];
 
            List<Commande_Produit> com = (from c in bdd.Commande_Produit
                            where c.idCommande == idcom
                            select c).ToList();
 
 
            foreach(Commande_Produit cs in com)
            {
                Produit prod = (from c in bdd.Produits
                                where c.idProduit == cs.idProduit
                                select c).FirstOrDefault();
 
                if (prod != null)
                {
                    int? newstock = prod.Stock - cs.Quantite;
                    prod.Stock = (int)newstock;
                }
 
                bdd.SaveChanges();
 
 
            }
 
        }
Si quelqu'un à la solution il me sauverait la vie, merci.