How to store and display time only? C# Mysql

Asked

Viewed 77 times

0

I need to take an input time and an output two textboxs, store in the bank and later display, my code is like this:

//Cadastro
//No evento do botão cadastrar
entradaSaida.HrEntrada = Convert.ToDateTime(txtHrEntrada.Text);
entradaSaida.HrSaida = Convert.ToDateTime(txtHrSaida.Text);

//No método insert do banco
cmd.Parameters.AddWithValue("@hrentrada", entradaSaida.HrEntrada);
cmd.Parameters.AddWithValue("@hrsaida", entradaSaida.HrSaida);

//Pegando do banco
entradaSaida.HrEntrada = (DateTime)dr["hrentrada"];
entradaSaida.HrSaida = (DateTime)dr["hrsaida"];

//Exibindo no ListView
materialListView1.Items.Add(new ListViewItem(new string[] {item.HrEntrada.ToShortTimeString(), item.HrSaida.ToShortTimeString()}));

When I store, what comes from the textbox is something like "13:00", but when it comes to displaying in Listview all times are reset "00:00". If anyone can help me, I’d appreciate it

  • Only 13:00 is stored in txtHrEntrada.Text? or a full date with time?

  • Another thing, where did the "item" of your Listview come from? It is missing some piece of your code I think. Post the whole thing so we can help you.

  • txtBox is only stored "13:00" anyway. About the Item, this code is from a foreach in the list, so I only got the part that adds the same item

  • Dear Rafael you are recording as, you do as CRUD operations?

1 answer

1

How to store and display time only?

To store only time in Mysql you can use the type TEAM specific to this, as reported in the commentary.


There is no error in the part where you convert the date to time only. It’s coming 00:00 why is what is stored in the value you recovered in the foreach who omitted from the question.

There’s no way to do a more thorough analysis than that.

var horario1 = Convert.ToDateTime("13:00");
var horario2 = Convert.ToDateTime("15:19");

//Quando você converte uma string apenas com a hora o resultado é a data de hoje e o horário da string:
Console.WriteLine(horario1); //resultado 4/24/2019 1:00:00 PM
Console.WriteLine(horario2); //resultado 4/24/2019 3:19:00 PM

//Se seu código está correto, quando for imprimir a hora dos items que armazenou acima com ToShortTimeString o resultado vai ser oque você espera mesmo:
Console.WriteLine(horario1.ToShortTimeString());
Console.WriteLine(horario2.ToShortTimeString());

See here the code above running and printing times normally.

By evaluating all this, your error is probably in the form you are storing and/or retrieving the information from the database.

See the microsoft documentation about Toshorttimestring and debug your code after you understand everything you’re using to find your problem.

  • 2

    Where you saw that you don’t have the type to record hours in Mysql, because there is yes!

  • 1

    Specific time type yes: https://dev.mysql.com/doc/refman/8.0/en/time.html

  • 1

    Thanks @Virgilionovic I will correct that information.

  • 1

    @hkotsubo error of mine, I do not use Mysql on the day and my quick query the documentation was quite flawed, I only read this excerpt (https://dev.mysql.com/doc/refman/8.0/en/datetime.html), TIME would be the next type addressed. Thanks!

Browser other questions tagged

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