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)
}
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>();]
– Fabricio Malta
Bars do not matter in the string. Post your complete code as is please
– Rovann Linhalis