Datetime null 01/01/0001 - Error in selection (Winforms+Layers bll,dal,model) C#

Asked

Viewed 311 times

-1

I made a business rule where I pass some information from my service order to my cashier. However when I will perform the search in the box, my date passes 01/01/0001, and the error of characters.

    Public List<Model.caixa> Select()
    {

        List<Model.caixa> ListaCaixa = new List<Model.caixa>();

        SqlConnection conexao = new SqlConnection(strCon);

        string sql = "Select * from caixa;";

        SqlCommand cmd = new SqlCommand(sql, conexao);

        conexao.Open();

        try

        {
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            while (reader.Read())
            {
                Model.caixa caixa = new Model.caixa();
                caixa.CodCaixa = Convert.ToInt32(reader[0].ToString());
                caixa.CodSrv = Convert.ToInt32(reader["CodSrv"].ToString());
                caixa.TipoSrv = reader["TipoSrv"].ToString();
                **caixa.DataPagamento = Convert.ToDateTime(reader["DataPagamento"].ToString());**
                caixa.Status = reader["Status"].ToString();
                caixa.Parcelamento = reader["Parcelamento"].ToString();
                caixa.Pagamento = reader["Pagamento"].ToString();
                ListaCaixa.Add(caixa);
            }
        }
        catch
        {
            MessageBox.Show("Deu erro na seleção do caixa!");
        }
        finally
        {
            conexao.Close();
        }
        return ListaCaixa;
    }

When it comes caixa.DataPagamento = Convert.ToDateTime(reader["DataPagamento"].ToString()); already falls for catch.

If anyone can help, I’d be grateful!

1 answer

0

Probably your reader["DataPagamento"] is null and you are calling a method from a null instance which causes an exception.

Make this change:

if(reader["DataPagamento"]!=null)
   caixa.DataPagamento = Convert.ToDateTime(reader["DataPagamento"].ToString());
  • the first reading in the bank worked, but in the other already enters the catch.

  • What is the Exception that is giving? inserts in front of the catch this: (Exception ex) and take what’s in the ex property. Message

  • ex. Message "String not recognized as valid Datetime." string - this one

  • The problem is in formatting your date string in the database, you can see which format is in the database?

  • put Datetime

  • Take a text from the Datapayment column so I can test with the saved format

  • Preferably what’s going wrong

  • a question, where I get this text for you? I am in debug with it, if you tell me the place already send

  • In the debug, select reader["DataPagamento"] and right-click and select the option Add Watch

  • Or else you see right in the comic

  • Reader["Datapayment"] error CS0103: The name 'Reader' does not exist in the Current context

  • was this error message that appeared

  • You will need to put a pause break in it and have search there will work

  • All right, I’ll do it tomorrow I’ll tell you the result hugs

Show 9 more comments

Browser other questions tagged

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