Error: String not recognized as valid datetime

Asked

Viewed 2,725 times

1

I’m trying to convert a string q I pull from the database with the instruction

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

And makes the following mistake:

Character string not recognized as valid datetime.

The entire method 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;
    }
  • 2

    The reader["PrevisaoDataInicio"] is receiving what?

  • What type of column PrevisaoDataInicio in the database?

  • 2

    my friend, before converting the string into Datetime, I recommend that you use a variable to receive its value, put a breakpoint in the created line, make a test and show us what such a variable is bringing.

  • The type of the Preview column is datetime also in the database.

2 answers

0

First of all, the Preview property should be of the Datetime type, right. According that the conversion you do is correct, but you need to see if any value comes Reset["Previsaostart date"], what I believe is coming, and this preference field has to be a field of type Character.

0


The Preview value cannot be converted to a date time,

It may be that it is NULL (if your column allows this in SQL) or you are holding it wrong in the bank, 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)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.