Show date in Messagebox C#

Asked

Viewed 169 times

0

I have a date vector with the values already entered, but I wanted to show the values one by one, so I created a "for" that did this Dataavailable is my date vector, 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 receiving the value of the date format ( It seems that as the date comes in the dd/mm/yyyy format it cannot use the "/")

2 answers

1

because of @ in variable name ?

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);
  • The "@" in the variables was some tests that I forgot to remove. Well, I did as you described and still continued my error. My array is declared by [List<Datetime> Dataavailable = new List<Datetime>();]

  • Bars do not matter in the string. Post your complete code as is please

0

I am creating a Hotel system, for this I need to know if there is a reservation during the check in and check out period, so I explode the distance between dates (08 to 10 turns 08-09-10) if there is any reservation in this period I add value to a variable called reserve and also that same day in the unavailable dates array, there in front I perform the test, the problem is that I want to show all the values within the array in one line(ex: "There is reservation on days 21,22,23,24,25,26) Follows 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)
                      }

Browser other questions tagged

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