Problem of Types DateTime C # + sqlserver

8

Good morning, I have the following problem. In a sqlserver database, I have an entity with datetime fields, I generated 2 new date fields and updated my model from the database. The fields were imported without problems, I add them to the corresponding class, and when doing the query it tells me:

  

Can not convert the System.DateTime type? in System.DateTime, Ya   there is an explicit conversion

This is how the fields that work are declared:

 public DateTime FechaCreacion {get;set;}
 public DateTime FechaModif {get;set;}

And the ones I'm wanting Add:

public DateTime FechaAsign { get; set; }
public DateTime FechaCierre { get; set; }

And my Query:

query = (from q in _conexion.Ticket_Cab where [bla..bla..bla] 
select new Ticket {[bla..bla] FechaCreacion = q.tik_fechacreacion,
FechaModif = q.tik_fechamodif, FechaAsign = q.tik_fechaAsign,
FechaCierre = q.tik_fechaCierre}).ToList();

The last two fields (q.tik_fechaAsign , q.tik_fechaCierre ) highlights them in red and throws me the error I quote above. Does anyone know what may be happening?

    
asked by FederHico 05.06.2017 в 19:50
source

3 answers

9

Your problem occurs when assigning a value of type Nullable<DateTime> with DateTime , currently, you declare the dates in the following way:

public DateTime FechaAsign { get; set; }
public DateTime FechaCierre { get; set; }

DateTime is a structure that can not be assigned null , I think the same thing happens with any structure.

So you must declare the variable as a value capable of receiving null by value and you can do it in the following way 1 :

Nullable<DateTime> FechaAssign { get; set; }
Nullable<DateTime> FechaCierre { get; set; }

Or the "pretty" :

DateTime? FechaAssign { get; set; }
DateTime? FechaCierre { get; set; }

This happens because in the declaration of the table in your database, the table accepts null by value in these fields.

If you want the fields to be the same as the previous ones, you should try to add the clause NOT NULL in the definition of the table in your database.

You must bear in mind that having a type Nullable<T> in your class, you do not directly access the member DateTime internal, but you must use the property .Value of the object.

To access the actual value of DateTime that you define in the query, you must do something like the following:

DateTime AlgunSitioDondeUsar = FechaAssign.Value; // Accede al DateTime interno.

Not to mention that you need to do a check of null to check the current value of the field.

Another Alternative:

Instead of altering the definition of your class Ticket , you can simply do a check in the query and it should work perfectly, with the exception that you will not have values null in the class, you can use a value as 01/01/1865 00:00:00 AM to represent a false value or null .

The query would look like the following:

query = (from q in _conexion.Ticket_Cab 
         where [bla..bla..bla] 
         select new Ticket { [bla..bla] FechaCreacion = q.tik_fechacreacion,
                              FechaModif = q.tik_fechamodif, 
                              FechaAsign = q.tik_fechaAsign ?? DateTime.Parse("01/01/1800"),
                              FechaCierre = q.tik_fechaCierre ?? DateTime.Parse("01/01/1800") }
        ).ToList();

Where 01/01/1800 is the date that indicates that your value is false or null .

1 : You must do using System; when using this method.

    
answered by 05.06.2017 / 19:57
source
4

In your database, they can surely be nulls, which leads you to edit:

public DateTime? FechaAsign { get; set; }
public DateTime? FechaCierre { get; set; }

And if in case you throw error in the query

FechaAsign = q.tik_fechaAsign.HasValue ? q.tik_fechaAsign.Value : (DateTime?)null

The same play you repeat in the next field

FechaCierre = q.tik_fechaCierre.HasValue ? q.tik_fechaCierre.Value : (DateTime?)null
    
answered by 05.06.2017 в 19:56
3

Where you use the data q.tik_fechaAsign and q.tik_fechaCierre put .Value so that it is as follows:

query = (from q in _conexion.Ticket_Cab where [bla..bla..bla] 
select new Ticket {[bla..bla] FechaCreacion = q.tik_fechacreacion,
FechaModif = q.tik_fechamodif, FechaAsign = q.tik_fechaAsign.Value,
FechaCierre = q.tik_fechaCierre.Value}).ToList();

This is because q.tik_fechaAsign and q.tik_fechaCierre are declared as DateTime? , that is, they are Nullable , they may or may not contain a value and when making use of them it is required to give them the value with which You are working.

    
answered by 05.06.2017 в 19:56