0

basically the problem is duplicated, but i need the solution

Back-end: C# - asp.net MVC5 - EF6.13 Framework Visual studuio : 2013 SQL SERVER : 2008

when i want to insert data into database through two classes, the message be generated :

(An exception of type 'System.NullReferenceException' occurred in BusinessErpMVC.dll but was not handled in user code

Additional information: La référence d'objet n'est pas définie à une instance d'un objet.) 

knowing that both classes have a lazy join ,i will now show you my calsses :

CommandeClt class

 public partial class CommandeClt
    {
        public CommandeClt()
        {
            this.CommandeClt_Ligne = new HashSet<CommandeClt_Ligne>();
        }

    public int CommandeCltID { get; set; }

     [Display(Name = "Code Cmd")]
    public int Cd_CommandeClt { get; set; }

      [Display(Name = "Ref Cmd")]
    public string Ref_CommandeClt { get; set; }

    [Display(Name = "Date Cmd")]
    [DataType(DataType.Date)]
    public Nullable<System.DateTime> Date_CommandeClt { get; set; }

    [Display(Name = "Societe")]
    public byte Id_Societe { get; set; }

    public string Nom_Societe { get; set; }
    public string Adresse_Societe { get; set; }
    public string CodePostal_Societe { get; set; }

    [Display(Name = "Client")]
    public int Id_CltOrigine { get; set; }
    public  string  Nom_CltOrigine { get; set; }
    public string  Adresse_CltOrigine { get; set; }
    public string CodePostal_CltOrigine { get; set; }
    public Nullable<int> Id_CltLivre { get; set; }
    public string Nom_CltLivre { get; set; }
    public string Adresse_CltLivre { get; set; }
    public string CodePostal_CltLivre { get; set; }
    public Nullable<int> Id_CltFacture { get; set; }
    public string Nom_CltFacture { get; set; }
    public string  Adresse_CltFacture { get; set; }
    public string CodePostal_CltFacture { get; set; }
    public int Id_Devise { get; set; }
    public decimal Taux_Devise { get; set; }
    public Nullable<byte> Id_ModeLivraison { get; set; }
    public Nullable<byte> Id_ModeExpedition { get; set; }
    public Nullable<byte> Id_ModeReglement { get; set; }
    public Nullable<System.DateTime> Date_Livraison { get; set; }
    public Nullable<System.DateTime> Date_Expedition { get; set; }
    public Nullable<decimal> Taux_Remise { get; set; }
    public Nullable<decimal> Montant_Remise { get; set; }
    public Nullable<decimal> Taux_Escompte { get; set; }
    public Nullable<decimal> Montant_Escompte { get; set; }
    public Nullable<decimal> Montant_Net_HT { get; set; }
    public Nullable<decimal> Montant_TVA { get; set; }
    public Nullable<decimal> Montant_TTC { get; set; }
    public Nullable<decimal> Acompte { get; set; }
    public Nullable<decimal> Montant_Net_Apayer { get; set; }
    public Nullable<decimal> Poids_Total { get; set; }
    public Nullable<decimal> Volume_Total { get; set; }
    public Nullable<decimal> Metrage_Total { get; set; }
    public Nullable<bool> IsActual { get; set; }
    public string  Commentaire { get; set; }
    public System.DateTime Date_Creation { get; set; }
    public Nullable<System.DateTime> Date_Update { get; set; }
    public Nullable<System.DateTime> Date_Validation { get; set; }
    public Nullable<int> Id_Creator { get; set; }
    public Nullable<int> Id_Updator { get; set; }
    public Nullable<int> Id_Validator { get; set; }
    public Nullable<byte> Etat_CommandeClt { get; set; }

    public virtual ICollection<CommandeClt_Ligne> CommandeClt_Ligne { get; set; }

     [ForeignKey("Id_Societe")]
    public virtual Societe Societe { get; set; }

     [ForeignKey("Id_CltOrigine")]
     [InverseProperty("Id_ClientOrigine")]
     public virtual Partenaire ClientOrigine { get; set; }

     [ForeignKey("Id_CltLivre")]
     [InverseProperty("Id_CltLivre")]
     public virtual Partenaire CltLivre { get; set; }

     [ForeignKey("Id_CltFacture")]
     [InverseProperty("Id_CltFacture")]
     public virtual Partenaire CltFacture { get; set; }

     [ForeignKey("Id_ModeLivraison")] 
     public virtual ModeLivraison ModeLivraison { get; set; }

}

CommandeClt_Ligne class

public partial class CommandeClt_Ligne
    {
        public int CommandeCltID { get; set; }
        public byte Id_CommandeClt_Ligne { get; set; }
        public int Id_Article { get; set; }
        public string Lib_Article { get; set; }
        public Nullable<decimal> Qte_Demandee { get; set; }
        public Nullable<decimal> Qte_Livree { get; set; }
        public Nullable<decimal> Qte_ALivrer { get; set; }
        public Nullable<decimal> Qte_Poids { get; set; }
        public Nullable<decimal> Qte_Volume { get; set; }
        public Nullable<decimal> Qte_Metrage { get; set; }
        public Nullable<byte> Id_Unite { get; set; }
        public Nullable<decimal> Prix_HT { get; set; }
        public Nullable<decimal> Taux_Remise { get; set; }
        public Nullable<byte> Id_TVA { get; set; }
        public Nullable<decimal> Taux_TVA { get; set; }
        public Nullable<System.DateTime> Date_Livraison { get; set; }
        public Nullable<int> Id_DevisClt { get; set; }
        public Nullable<byte> Id_DevisClt_Ligne { get; set; }
        public Nullable<int> Id_CommandeOuverteClt { get; set; }
        public Nullable<byte> Id_CommandeOuverteClt_Ligne { get; set; }
        public string Commentaire { get; set; }
        public byte Etat_CommandeClt_Ligne { get; set; }

        public virtual CommandeClt CommandeClt { get; set; }
    }

C# code :

[HttpPost]
        public ActionResult Create(CommandeClt commandeclt)
        {
            try
            {
                ViewBag.SelectedSociete = new SelectList(MetiersRepository.SocieteAll(), "Id_Societe", "Cd_Societe");
                ViewBag.SelectedClient = new SelectList(MetiersRepository.ClientAll(), "Id_Partenaire", "Cd_Partenaire");
                ViewBag.SelectedDevise = new SelectList(MetiersRepository.DeviseAll(), "Id_Devise", "Lib_Devise");
                ViewBag.SelectedModeLivraison = new SelectList(MetiersRepository.ModeLivraisonAll(), "Id_ModeLivraison", "Lib_ModeLivraison");
                ViewBag.SelectedModeExpedition = new SelectList(MetiersRepository.ModeExpeditionAll(), "Id_ModeExpedition", "Lib_ModeExpedition");


                if (ModelState.IsValid)
                {

                dbMng.CommandeClts.Add(commandeclt);
                dbMng.SaveChanges();
                    return RedirectToAction("Index");
                }


            }
            catch (DataException /* dex */)
            {
                //Log the error (uncomment dex variable name after DataException and add a line here to write a log.
                ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator.");
            }
            return View(commandeclt);
        }

this is the picture for more explains

picture 1 :

enter image description here

many thanks

MOHAMED
  • 13
  • 5
  • hello everybody please help me – MOHAMED Nov 02 '17 at 16:27
  • The exception stack trace should tell you which line threw the null reference exception. Put a breakpoint there and inspect the value of the variables/expressions used there. You are getting `NULL` and you need to findout why. – Shyju Nov 02 '17 at 16:41
  • yes u are right – MOHAMED Nov 02 '17 at 16:45
  • i have updated my poste that i add two pictures – MOHAMED Nov 02 '17 at 16:58
  • Hi, if you change the question significantly, you have to ask a new question. – Danny Varod Nov 02 '17 at 18:17
  • i think the question is simple ,whats mean this message error when i insert the data with Entity framework 6 - mvc 5 [(An exception of type 'System.NullReferenceException' occurred in BusinessErpMVC.dll but was not handled in user code Additional information: La référence d'objet n'est pas définie à une instance d'un objet.)] – MOHAMED Nov 03 '17 at 12:17

0 Answers0