Error: String was not recognized as valid DateTime [closed]

1

I'm trying to convert a string that I pull from the database with the

DateTime PrevisaoDataInicio = DateTime.Parse(reader["PrevisaoDataInicio"].ToString())

And gives the following error:

  

String was not recognized as a valid DateTime.

The entire method where it is being used is below:

private List<EntendimentoDominio> TransformaReaderEmListaDeObjeto(SqlCeDataReader reader)
    {
        var entendimentos = new List<EntendimentoDominio>();
        while (reader.Read())
        {
            var temObjeto = new EntendimentoDominio()
            {
                CodEntendimento = int.Parse(reader["CodEntendimento"].ToString()),
                AreaResponsavel = reader["AreaResponsavel"].ToString(),
                Modulo = reader["Modulo"].ToString(),
                Projeto = reader["Projeto"].ToString(),
                Subprojeto = reader["Subprojeto"].ToString(),
                DescricaoResumida = reader["DescricaoResumida"].ToString(),
                DescricaoDetalhada = reader["DescricaoDetalhada"].ToString(),
                CustoEstimado = reader["CustoEstimado"].ToString(),
                NomeDocumentoAnexo = reader["NomeDocumentoAnexo"].ToString(),
                CaminhoDocumentoAnexo = reader["CaminhoDocumentoAnexo"].ToString(),
                Status = reader["Status"].ToString(),
                TempoEstimado = reader["TempoEstimado"].ToString(),
                PrevisaoDataInicio = DateTime.Parse(reader["PrevisaoDataInicio"].ToString()),
                PrevisaoDataFinal = DateTime.Parse(reader["PrevisaoDataFinal"].ToString())
            };

            entendimentos.Add(temObjeto);
        }

        reader.Close();
        return entendimentos;
    }
    
asked by anonymous 03.06.2015 / 13:49

2 answers

0

The value of ForecastInitial can not be converted to a date time,

It may be NULL (if your column allows this in SQL) or you are saving it wrongly in the database, I suggest you do something like this.

    EntendimentoDominio obj = new EntendimentoDominio();
    string previsaoDTInici = reader["PrevisaoDataInicio"].ToString();

    if(!string.IsNullOrEmpty(previsaoDTInici))
      obj.PrevisaoDataInicio = DateTime.Parse(previsaoDTInici)
    
03.06.2015 / 19:11
0

First, the HomePreview property must be of type DateTime, right. Secondly, the conversion you make is correct, but you need to see if there is some reader value, which I believe is coming, and this preference field has to be a Character field.

    
03.06.2015 / 19:10