How can I check the date I pick from a DateTimePicker control exists in a SQL table?
I have a DateTimePicker on my ASP.NET page where I can select a date. I need to check if this selected date exists in a SQL table (in a table called blockdate). If the selected date does exist, I need to enter the corresponding values from the date and reason columns in the blockdate table into a DataGridView.
Can anybody tell me how I can do this?
This is the code I have so far to check if the selected date is a weekend.
DatePicker.SelectedDate.ToString("dd-MM-yyyy");
DateTime date = DatePicker.SelectedDate.Date;
DateTime weekend;
if (date.DayOfWeek == DayOfWeek.Saturday)
{
weekend = date.AddDays(+2);
}
if (date.DayOfWeek == DayOfWeek.Sunday)
{
weekend = date.AddDays(+1);
}
else
{
weekend = date;
}
I think calling the database on every time when the
DatePickervalues change not going to be an ideal solution.You can select all the dates from you sql table to a list in pageload.
Then when the
DatePickervalue changed, you can match the value against your list.Hope this helps