I get a an error when the Date is null but cannot understand why and how to fix it

0

This is driving me crazy:

            using (var context = new aContext())
            {
                _Paz = context.TPaz
                .Where(a => a.CodPaz == _PazV.CodPaz)
                .FirstOrDefault();
            }

I have 2 different rows, on the first one everything is perfect. On the second one I get the error

Here the definitions:

[Table("tPAZ")]
public partial class TPaz
{
    public TPaz()
    {
        TApp = new HashSet<TApp>();
        TApr = new HashSet<TApr>();
        TAsl = new HashSet<TAsl>();
        TCad = new HashSet<TCad>();
        TDia = new HashSet<TDia>();
        TEos = new HashSet<TEos>();
        TGen = new HashSet<TGen>();
        TImg = new HashSet<TImg>();
        TInt = new HashSet<TInt>();
        TIst = new HashSet<TIst>();
        TPlp = new HashSet<TPlp>();
    }

    [Key]
    [Column("COD_PAZ")]
    public int CodPaz { get; set; }
    [Required]
    [Column("COGNOME")]
    [StringLength(30)]
    public string Cognome { get; set; }
    [Required]
    [Column("NOME")]
    [StringLength(30)]
    public string Nome { get; set; }
    [Column("INDIR")]
    [StringLength(30)]
    public string Indir { get; set; }
    [Column("CITTA")]
    [StringLength(20)]
    public string Citta { get; set; }
    [Column("PROV")]
    [StringLength(2)]
    public string Prov { get; set; }
    [Column("CAP")]
    [StringLength(5)]
    public string Cap { get; set; }
    [Column("NAZIONE")]
    [StringLength(15)]
    public string Nazione { get; set; }
    [Column("NATOA")]
    [StringLength(20)]
    public string Natoa { get; set; }
    [Column("NATOP")]
    [StringLength(2)]
    public string Natop { get; set; }
    [Column("NATON")]
    [StringLength(15)]
    public string Naton { get; set; }
    [Column("NATODATA")]
    //        [StringLength(2048)]
    //        public string Natodata { get; set; }
    public DateTime Natodata { get; set; }
    [Column("TELEF1")]
    [StringLength(20)]
    public string Telef1 { get; set; }
    [Column("TELEF2")]
    [StringLength(20)]
    public string Telef2 { get; set; }
    [Column("FAX")]
    [StringLength(20)]
    public string Fax { get; set; }
    [Column("PRES_DA")]
    [StringLength(15)]
    public string PresDa { get; set; }
    [Column("COD_FIS")]
    [StringLength(16)]
    public string CodFis { get; set; }
    public short Status { get; set; }
    [Required]
    [StringLength(30)]
    public string Clinica { get; set; }
    [Required]
    [StringLength(15)]
    public string Assicurazione { get; set; }

    [NotMapped]
    public string NatodataS
    {
        get
        {
            if ((Natodata == null) || (DBNull.Value.Equals(Natodata)))
            //if (Natodata == null) 
            {
                return "";
            }
            else
            {
                return Natodata.ToString(GetCultureHelper.GetCurrentCulture().DateTimeFormat.ShortDatePattern);
            }
        }
    }

    [ForeignKey(nameof(Assicurazione))]
    [InverseProperty(nameof(TAssicurazioni.TPaz))]
    public virtual TAssicurazioni AssicurazioneNavigation { get; set; }
    [ForeignKey(nameof(Clinica))]
    [InverseProperty(nameof(TCliniche.TPaz))]
    public virtual TCliniche ClinicaNavigation { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TApp> TApp { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TApr> TApr { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TAsl> TAsl { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TCad> TCad { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TDia> TDia { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TEos> TEos { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TGen> TGen { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TImg> TImg { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TInt> TInt { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TIst> TIst { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TPlp> TPlp { get; set; }
}

In the context:

    public virtual DbSet<TPaz> TPaz { get; set; }

here is the data:

COD_PAZ COGNOME NOME    INDIR   CITTA   PROV    CAP     NAZIONE NATOA   NATOP   NATON   NATODATA                    TELEF1  TELEF2  FAX     PRES_DA COD_FIS Status  Clinica Assicurazione
6       AAAA    BBB  v. G.      NULL    Mo      NULL    NULL    (Va)    NULL    NULL    1940-06-08 00:00:00.0000000 1234    3456    NULL    abcde   NULL    6       .       .
2895    aaaaa   pippo   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL                        NULL    NULL    NULL    NULL    NULL    99      .       .

The second line reports the error:

System.Data.SqlTypes.SqlNullValueException
  HResult=0x80131931
  Message=Data is Null. This method or property cannot be called on Null values.
  Source=Microsoft.Data.SqlClient
  StackTrace:
   at Microsoft.Data.SqlClient.SqlBuffer.get_DateTime()
   at Microsoft.Data.SqlClient.SqlDataReader.GetDateTime(Int32 i)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
   at TitoDoc_A.Models.MTreeViewPaz.get_Paz() in E:\Users\Massimo\OneDrive\Development\Source\TitoDoc_A\TitoDoc_A\Models\MTreeViewPaz.cs:line 57
   at TitoDoc_A.Views.TitoDocPage.TitoDocPage_obj86_Bindings.Update_(MTreeViewPaz obj, Int32 phase) in E:\Users\Massimo\OneDrive\Development\Source\TitoDoc_A\TitoDoc_A\obj\x86\Debug\Views\TitoDocPage.g.cs:line 3589
   at TitoDoc_A.Views.TitoDocPage.TitoDocPage_obj86_Bindings.Update() in E:\Users\Massimo\OneDrive\Development\Source\TitoDoc_A\TitoDoc_A\obj\x86\Debug\Views\TitoDocPage.g.cs:line 3558
   at TitoDoc_A.Views.TitoDocPage.TitoDocPage_obj86_Bindings.DataContextChangedHandler(FrameworkElement sender, DataContextChangedEventArgs args) in E:\Users\Massimo\OneDrive\Development\Source\TitoDoc_A\TitoDoc_A\obj\x86\Debug\Views\TitoDocPage.g.cs:line 3498

  This exception was originally thrown at this call stack:
    [External Code]
    TitoDoc_A.Models.MTreeViewPaz.Paz.get() in MTreeViewPaz.cs
    [External Code]

I've checked and is due to the NATOD (datetime) that is NULL but how can I avoid the error?

sql-server
linq
null
entity-framework-core
asked on Stack Overflow Mar 30, 2020 by Massimo Savazzi

1 Answer

0

The field in database is allowed to be null, so I fixed it by making the field in the model nullable too.

[Column("NATODATA")]
public DateTime? Natodata { get; set; }

Using DateTime? worked.

answered on Stack Overflow Mar 30, 2020 by Massimo Savazzi • edited Mar 30, 2020 by Dharman

User contributions licensed under CC BY-SA 3.0