Convert data to C #

5

Well folks I have a problem with my code. In my database I have a field of type date only that after calling it to the program it also passes me the time, it's something of the genre: 03-18-2015 00:00:00. What I wanted was just the part of the date and before I came here I tried it

DateTime data = Convert.ToDateTime(reader["DATA"]);
                DateTime dataOnly = data.Date;
                string dataFinal = dataOnly.ToString("d");
                Console.WriteLine(DateTime.Now.ToString("dd-MM-yyyy"));

And this did exactly what I wanted but in the program I have to do it only inserts the last date. I'll leave the code below and an image for you to understand better

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace experiencia
{
    class Program
    {
        static void Main(string[] args)
        {

            SageNGCOApi.BaseNext SageNextAPI = new SageNGCOApi.BaseNext();
            SageNextAPI.PercIni = "C:\ProgramData\Sage\2070\Next\";
            SageNextAPI.PercLOG = "C:\ProgramData\Sage\2070\Next\";
            SageNextAPI.Empresa = "FDISQL";
            SageNextAPI.Password = "teste";
            SageNextAPI.Login = "teste";
            int resultado;
            resultado = SageNextAPI.Iniciar();
            if (resultado != 0) // 0: Sucesso
            {
                Console.WriteLine("Erro ao iniciar Sage Next API");
            }
            else
            {
                Console.WriteLine("SUCESSO!");
            }

            string text = System.IO.File.ReadAllText(@"C:\Users\Hugo\Desktop\teste\config.ini");
            SqlConnection conn = new SqlConnection(text);
            conn.Open();
            var cmd = new SqlCommand(@"SELECT * from my_cab", conn);
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                SageNGCOApi.DocumentoComercial documento = new SageNGCOApi.DocumentoComercial();
                documento.ActivarGrelhasDesconto = false;
                documento.ActivarLinhasBonus = true;
                documento.ActivarLinhasPreco = true;


                DateTime data = Convert.ToDateTime(reader["DATA"]);
                DateTime dataOnly = data.Date;
                string dataFinal = dataOnly.ToString("d");
                Console.WriteLine(DateTime.Now.ToString("dd-MM-yyyy"));




                documento.cab.Sector = Convert.ToString(reader["SETOR"]);
                documento.cab.TipoDocumento = Convert.ToString(reader["DOCUMENTO"]);
                documento.cab.Serie = 1;
                documento.cab.Data = DateTime.Now.ToString("dd-MM-yyyy");
                documento.cab.NossoNoDocumento = 0; // 0 lê numerador
                documento.cab.Terceiro = Convert.ToString(reader["TERCEIRO"]);
                documento.cab.Moeda = "EUR";
                documento.cab.RegimeIva = documento.Cliente.REGIVA;
                documento.Origem = SageNGCOApi.eOrigemDocumento.NaoAplicavel;



                SqlCommand cmd_1 = new SqlCommand("SELECT * FROM MY_LIN WHERE REGISTO=@reg", conn);
                cmd_1.Parameters.AddWithValue("@reg", reader["REGISTO"]);
                SqlDataReader le = cmd_1.ExecuteReader();

                while (le.Read())
                {

                    SageNGCOApi.DocumentosGcLin linha = new SageNGCOApi.DocumentosGcLin();

                    linha.TipoDeLinha = 1; // 1-Movimento; 2-Abatimento; 3-Despesa; 4-Informativo
                    linha.Armazem = Convert.ToString(le["ARMAZEM"]);
                    linha.Artigo = Convert.ToString(le["ARTIGO"]);
                    linha.Unidade = Convert.ToString(le["UNIDADE"]);
                    linha.PrecoUnitario = Convert.ToDecimal(le["PRECO"]);
                    linha.Valor = Convert.ToDecimal(le["VALOR"]);
                    linha.Quantidade = Convert.ToDecimal(le["QNT"]);
                    linha.Desconto = Convert.ToString(le["DESCONTO"]);
                    linha.TaxaDesconto = Convert.ToDecimal(le["T_DESC"]);
                    documento.SugereValoresLin(linha, false, true, true, false, false, false, true);
                    documento.AdicionaLinha(linha);
                }


               if (documento.Validar() == 0)
                {
                    if (documento.Inserir() == 0)
                        if (resultado == 0) // 0: Sucesso
                        {
                            Console.WriteLine("Documento inserido com sucesso!");
                        }
                        else
                        {
                            Console.WriteLine("Erro ao inserir documento: " + documento.UltimaMensagem());
                        }
                }
               else
               {
                   Console.WriteLine("Erro ao validar documento: " + documento.UltimaMensagem());
               }
            }
            Console.ReadLine();
        }
    }
}

If I use the date as the current date DateTime.Now.ToString("dd-MM-yyyy") it works and inserts the dates all but if I use the date as the date that comes from the database and then converted it just inserts the last one. I leave some images to understand better and if they knew thank you for helping me. Thanks!

    
asked by anonymous 18.07.2015 / 12:40

3 answers

3

Simply type

NomeVariavel.ToShortDateString();

You do not have to make it difficult when there are native features for these situations.

    
28.10.2016 / 02:49
1

If you just want to print the date on the screen, try the following:

Console.WriteLine(Convert.ToDateTime(reader["DATA"]).ToString("dd-MM-yyyy"));

This way you show the date value that came from the DB without the time.

    
19.07.2015 / 01:17
1

You can use TryParseExact () to do this your code would look like this.

DateTime valor;
DateTime.TryParseExact(reader["DATA"], "dd/MM/yyyy",
    CultureInfo.InvariantCulture, DateTimeStyles.None, out valor);

    ....
    documento.cab.Serie = 1;
    documento.cab.Data = valor;

    ....
    
28.10.2016 / 03:01