Error Converting String to DateTime on a System Console

Flávio Rocha 21 Reputation points
2021-07-19T04:17:17.287+00:00

Hi Folks, please help me with that please, I'm not figuring out what is going on.

Here is my code.

using System;
using System.Data.SqlClient;
using System.Globalization;
using System.Collections.Generic;

namespace ConexaoBD
{
class Program
{
static void Main(string[] args)
{
CultureInfo provider = CultureInfo.InvariantCulture;
provider = new CultureInfo("pt-BR");

        DateTime datahoje;

        SqlConnection connection = new SqlConnection(@"data source=DESKTOP-3O98051; Integrated Security=SSPI; Initial Catalog=FastCashFlow");
        connection.Open();

        /* string strUpdate = "Update Users Set UserName = 'Amarildo' Where UserID = 1";
         SqlCommand cmdUpdate = new SqlCommand(strUpdate, connection);
         cmdUpdate.ExecuteNonQuery(); */

        /* string strDelete = "Delete from Users Where UserID = 1";
         SqlCommand cmdDelete = new SqlCommand(strDelete, connection);
         cmdDelete.ExecuteNonQuery(); */

        Console.Write("Digite o Nome do Usuário: ");
        string nome = Console.ReadLine();

        Console.Write("Digite o E-mail do Usuário: ");
        string email = Console.ReadLine();

        Console.Write("Digite o Login do Usuário: ");
        string Login = Console.ReadLine();

        Console.Write("Digite a senha do Usuário :");
        string senha = Console.ReadLine();

        Console.Write("Digite a data de cadastro: ");
        string datacadastro1 = Console.ReadLine();
        DateTime datacadastro = Convert.ToDateTime(datacadastro1);

        datahoje = DateTime.ParseExact(datacadastro.ToString(), "dd/MM/yyyy HH:mm", provider);


        string strInsert = String.Format("Insert INTO Users(UserName, UserEmail, UserLogin, UserPassword, Date) VALUES('{0}','{1}','{2}','{3}','{4}')", nome, email, Login, senha, datahoje);
        SqlCommand cmdInsert = new SqlCommand(strInsert, connection);
        cmdInsert.ExecuteNonQuery();

        string strSelectusers = "Select * from Users";
        SqlCommand cmdSelect = new SqlCommand(strSelectusers, connection);
        SqlDataReader reader = cmdSelect.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine("Id: {0}, Nome: {1}, E-mail: {2}, Login: {3}, Senha: {4}, Data: {5}",  reader["UserID"], reader["UserName"], reader["UserEmail"], reader["UserLogin"], reader["UserPassword"], reader["Date"]);
        }
    }
}

}

Thanks and best regards.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,905 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2021-07-19T06:14:57.793+00:00

    Hi FlvioRocha-5621,
    First, the date string(datacadastro1 )you enter need to meet the "dd/MM/yyyy HH:mm" format.
    Then it is better to use DateTime.ParseExact or DateTime.TryParseExact instead of Convert.ToDateTime method.
    Or you can use ToDateTime(String, IFormatProvider) to specify culture format information.

     string datacadastro1 = Console.ReadLine();  
    // the datacadastro1 needs to meet the "dd/MM/yyyy HH:mm" format.  such as 19/07/2021 14:30  
    DateTime datacadastro = Convert.ToDateTime(datacadastro1, System.Globalization.CultureInfo.GetCultureInfo("pt-BR").DateTimeFormat);  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.