0

I´m having problems with the next error

Stack Trace:

Exception type: InvalidCastException 
   Exception message: The specified conversion is not valid.
  on System.Data.SqlClient.SqlBuffer.get_DateTime()
  on System.Data.SqlClient.SqlDataReader.GetDateTime(Int32 i)
  on Read_Table_Zona(ObjectMaterializer`1 )
  on System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
  on System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
  on System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
  on ...ReporteDA.GetZona()

I generated my database scheme from Server Explorer update it from the production env, but while it's on production sometimes this error appears and throws the site

Code:

public static List<Table_Zona> GetZona()
{
    dc = new DBDataContext();
    var list = dc.Table_Zona.ToList();
    return list;
}

DBDataContext:

public DBDataContext() :
        base(global::System.Configuration.ConfigurationManager.ConnectionStrings["BD_RConnectionString"].ConnectionString, mappingSource)
{
    OnCreated();
}

Table_Zona:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Table_Zona")]
public partial class Table_Zona : INotifyPropertyChanging, INotifyPropertyChanged
{
        
    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
        
    private int _id;
        
    private string _Zona;
        
    private string _Descripcion;
        
    private System.Nullable<System.DateTime> _fListaContador;
        
    private System.Nullable<System.DateTime> _fListaAsesor;
        
    private string _TableName;
        
    private EntitySet<Table_Agencia> _Table_Agencias;
    // ...
}

Database:

CREATE TABLE [dbo].[Table_Zona](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [Zona] [varchar](20) NOT NULL,
    [Descripcion] [varchar](50) NULL,
    [fListaContador] [datetime] NULL,
    [fListaAsesor] [datetime] NULL,
    [TableName] [nvarchar](150) NULL
)
id Zona Descripcion fListaContador fListaAsesor TableName
1 Zona A Zona A NULL NULL Table_ReportesZA
2 Zona B Zona B NULL NULL Table_Reportes

I saw that the problems are about trying to get DataTime, but I looked and the data is correct in the database. I'm working on .NET Framework 4.7.2

Edit: This error occurs not only when getting data from Table_zone but also in other tables where there's a DateTime column. The error is sporadic and i usually fix it by restarting IIS. I have not been able to replicate the error in dev

7
  • Can you also include your DBDataContext()? The error is saying there is a field in your DBSet<Table_Zona> is in DateTime type but the data retrieved from database by your ToList() cannot convert it. Make sure the data is in correct date time format. Commented Mar 3 at 23:01
  • I saw that the problems are about trying to get DataTime, but I looked and the data is correct in the database. - clearly not the case... this is caused by invalid data... Commented Mar 3 at 23:11
  • 2
    "My database schema is correct" needs proof. Indications are a mismatch of types between the C# class and the SQL table. Please show both. Commented Mar 3 at 23:38
  • 3
    Please share the CREATE TABLE for the relevant table. Do not guess - use the SQL Server tooling to generate it. Commented Mar 4 at 0:30
  • There are two common reasons for this error. First, if you have a C# class expecting a DateTime, but the corresponding database column is defined with a varchar or similar. Second, if the DateTime column defined in SQL is nullable, but the C# class is defined as not nullable. Either of those will result in this exact error. However, the code posted here is not doing either. I would go and check again that the code in your project does actually match, and isn't perhaps mapping to a slightly different C# class, without the System.Nullable option for the DateTime properties. Commented Mar 5 at 18:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.