Show date in MessageBox C #

0

I have a vector of dates with values already inserted, but I wanted to show the values one by one, so I created a "for" that did this DatasIndisible is my vector of dates, with values already allocated;

string mens;
 for (int i = 0; i <= 5; i++)
  {
   mens += @DatasIndisponiveis[i].ToString();
  }
 MessageBox.Show(@mens,"Erro", MessageBoxButtons.OK, MessageBoxIcon.Information);

It simply does not run Messagebox right after it receives the value of the date format (it seems like the date comes in the format dd / mm / yyyy it can not use the "/")

    
asked by anonymous 14.06.2017 / 03:49

2 answers

1

why of @ in the name of variables?

If you have an array of dates, it would look like this:

DateTime[] datas = new DateTime[5];
string msg = "";
for(int i =0; i< datas.Lenght; i++)
     msg += datas[i].ToShortDateString()+"\r\n";

MessageBox.Show(msg);
    
14.06.2017 / 04:10
0

I am creating a hotel system, so I need to know if there is a reservation during the check in and check out period, so I explode the distance between the dates (08 to 10 turns 08-09-10) if there is any In this case, I would like to display all the values inside the array in a single line (eg " There is reserve on days 21,22,23,24,25,26) Here is the code:

// FUNCTION TO COMPARE CHECK IN AND CHECKOUT DATES

    public List<DateTime> retornadata(DateTime start, DateTime end)
    {
        var dates = new List<DateTime>();

        for (var dt = start; dt <= end; dt = dt.AddDays(1))
        {
            dates.Add(dt);
        }

        return dates;
    }

Now the date insertion code

try {

                    DateTime start = dtpCheInR.Value;
                    DateTime end = dtpCheOutR.Value;
                    List<DateTime> DatasIndisponiveis = new List<DateTime>();
                    List<DateTime> datas = retornadata(start, end);
                    int reservas = 0;
                    foreach (var data in datas)
                    {
                        string stringSQL = "SELECT * FROM reserva JOIN quarto on (quarto.IdQ = reserva.IdQ) WHERE quarto.NumeroQ= " + txtQuartoR.Text+ " AND CAST('"+ data.ToString("yyyy-MM-dd") +"' AS DATE) BETWEEN reserva.CheckinR and reserva.CheckoutR";

                        MySqlCommand comandoSQL = conectabanco.CreateCommand();
                         comandoSQL.CommandText = stringSQL;

                        comandoSQL.Connection = conectabanco; //usar a conexao mComm
                        MySqlDataReader dadosNumQuarto = comandoSQL.ExecuteReader();
                        try
                        {

                            dadosNumQuarto.Read();

                            NumQ = dadosNumQuarto["NumeroQ"].ToString();

                            dadosNumQuarto.Close();

                        }
                        catch (Exception)
                        {
                            // Caso ele não tenha encontrado valor ele cai neste catch que possibilita o fechamento do comando de leitura já aberto.
                            dadosNumQuarto.Close();
                            // throw;
                        }
                        if ((NumQ != null)&&(NumQ!=""))
                        {
                            DatasIndisponiveis.Add(data);
                            reservas++;

                        }
                        NumQ = "";
                    }


                    if(reservas > 0)
                    {
// O Erro acontece aqui, ele simplesmente trava

                        for (int i = 0; i < reservas; i++)
                        {
                            msg += @DatasIndisponiveis[i].ToShortDateString() + "\r\n";
                        }
                        MessageBox.Show(msg, "Erro Nova reserva", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                    else
                    {
//Realizo o insert aqui ( já funcionando)
                      }
    
15.06.2017 / 00:47