I am developing an app for colleges, a part of the app is to set the weekly schedule of classes.
My parameters are Classroom number, Day of the week, and Time Slot.
So I have a range of classrooms, each classroom has a number.
in the Run-Time, the app generates some buttons based on how many classrooms I have on my database and sets classroom numbers on each Button.
What I want to do is to Label each Classroom button with "Red" BackColor if that certain classroom is full in the given Day of the Week and the given Time Slot.
I have accomplished what I wanted to do and my code works with no errors, but my only problem is now the performance.
Here is my Code:
private OleDbConnection Connection = new OleDbConnection();
private void SomeMethod(string Day, string Time)
{
int MaxIndex = 0;
string str1 = "select Max([Row Index]) from Table";
OleDbCommand Command1 = new OleDbCommand(str1, Connection);
Connection.Open();
if (Command1.ExecuteScalar() == DBNull.Value)
MaxIndex = 1;
else
MaxIndex = Convert.ToInt32(Command1.ExecuteScalar());
Connection.Close();
for (int i = 0; i < MaxIndex; i++)
{
string str = "select [classroom Number] from Table where [Day] = @ParamDay and [Time] = @ParamTime and [Row Index] = @ParamIndex";
OleDbCommand Command = new OleDbCommand(str, Connection);
Command.Parameters.Add("@ParamDay", Day);
Command.Parameters.Add("@ParamTime", Time);
Command.Parameters.Add("@ParamIndex", i + 1);
Connection.Open();
OleDbDataReader reader = Command.ExecuteReader();
if (reader.Read())
{
foreach (Button btn in ButtonsPanel.Controls)
{
if (btn.Text == reader["classroom Number"].ToString())
{
btn.BackColor = Color.Red;
}
}
Connection.Close();
}
}
}
so this code takes about 13 seconds if I have 200 rows which I expect to have.
The question is... Is there anything I can do to my code so that these 13 seconds will reduce to at least 2-4 seconds?
For information: I have searched a lot on the internet, but could not find the solution to my issue here.