Reputation: 15
I have 24 buttons in which is written time, 24hours.
button1.Text="01:00"
button2.Text="02:00"
button3.Text="03:00"
...
button24.Text="00:00"
I have a label which shows Todays date
DateTime todayDate = DateTime.Now;
lblTodayDate.Text = todayDate.ToString("dd/MM/yyyy");
Whenever a user clicks to any of these 24 buttons, the user signs up for an appointment. For example, if the user clicks button3.Text="03:00"
it means the user made an appointment for (Today at 3:00) and this record will be saved in database MS-SQL. When another user visits this page this button shouldn't be enabled because one person already made an appointment for this time today but tomorrow it must be again enabled till one user clicks on it.
in PageLoad I would like to create a method which will go the database and check if the appointment with the same date and time exist if yes the button should not be enabled. Else button enabled.
protected void CheckIfOrderAvailable()
{
SqlConnection con = new SqlConnection();
string cs = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
con.ConnectionString = cs;
con.Open();
SqlCommand cmd = new SqlCommand();
var buttons = new Button[] { button1, button2...button24 };
foreach (var btn in buttons)
cmd.CommandText = "select * from clientOrder where TodayDateToCompare='" + lblTodayDate.Text + " and service_time='" + btn.Text + "'";
cmd.Connection = con;
SqlDataReader rd1 = cmd.ExecuteReader();
if (rd1.HasRows)
{
foreach (var btn in buttons)
{
btn.Enabled = false;
btn.BackColor = System.Drawing.Color.Red;
}
con.Close();
}
}
I would really appreciate it if someone will help to solve this problem.
Upvotes: 0
Views: 714
Reputation: 138
You use should ajax request. Page load event clear. aspx page is insert ajax request for every 5 minutes.
Ajax request get the appointments in today and if hour used then jquery selector to select button element and disabled
Upvotes: 0
Reputation: 6605
the loop is not correct.
foreach (var btn in buttons)
cmd.CommandText = "select * from clientOrder where TodayDateToCompare='" + lblTodayDate.Text + " and service_time='" + btn.Text + "'";
cmd.Connection = con;
SqlDataReader rd1 = cmd.ExecuteReader();
if (rd1.HasRows)
{
foreach (var btn in buttons)
{
btn.Enabled = false;
btn.BackColor = System.Drawing.Color.Red;
}
con.Close();
}
it should like below if I understand correctly:
foreach (var btn in buttons)
{
cmd.CommandText = "select * from clientOrder where TodayDateToCompare='" + lblTodayDate.Text + " and service_time='" + btn.Text + "'";
cmd.Connection = con;
SqlDataReader rd1 = cmd.ExecuteReader();
if (rd1.HasRows)
{
btn.Enabled = false;
btn.BackColor = System.Drawing.Color.Red;
}
}
con.close();
also, it should use "using" to make sure con close.
Upvotes: 0
Reputation: 1297
first of all you haven't provided your table structures and we do not know if you are using Entity framework or not so my answer will be mostly on psudo code..
since you are saving the appointments on the database what you should do is
1- on the webform page load event you should fetch the appointments from te database
2- get all the controls on the form with something like this
List<Button> buttonsList = new List<Button>();
foreach (var control in Controls)
{
if (control is Button bt)
{
. buttonsList.Add(bt);
}
}
3- itarate through all the buttons in buttonsList and check if appointment value is equal to button.Text
foreach (var bt in buttonsList)
{
foreach (appointment in Appointments) // appointments is fetch appointments from database
{
if (bt.Text = appointment )
{
bt.Enabled = false;
}
}
}
Hope this helps!
Upvotes: 1