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)
}