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
DBDataContext()? The error is saying there is a field in yourDBSet<Table_Zona>is inDateTimetype but the data retrieved from database by yourToList()cannot convert it. Make sure the data is in correct date time format.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...CREATE TABLEfor the relevant table. Do not guess - use the SQL Server tooling to generate it.System.Nullableoption for the DateTime properties.