0

Good evening. I am working on a mini programme that requires the user to choose date and time on the Calendar Date picker of WPF. The user then should click the check-in button. I wish to disable the check-in button if the user has not selected any value for both check-in date and check-out date.

Images of main application: User interface of the application

My code: The if statement only contains checkinDate as I wish to check if the code works before I code for checkoutDate. ToString() is used as I need to convert the Calendar Date Picker value into a string for other functions to work. I am using visual studio 2017, which seems to have a major difference from the previous version.

        DateTime cid = DateTime.Parse(checkinDate.Date.ToString());
        DateTime cod = DateTime.Parse(checkoutDate.Date.ToString());
        Stay s = new Stay(cid, cod);
        if (checkinDate is null)
        {
            checkInbtn.IsEnabled = false;
        }

The code is not working as the Check In button is still not disabled when the check-in and check-out date contains no value. The error message given whenever the check-in button is clicked is: System.FormatException: 'String was not recognized as a valid DateTime.'

I have conducted my research all around google and on stack overflow and has used the following method for trial and error but to no avail.

Code Tried:

if (checkinDate.GetValue == null)
if (checkinDate.Value == null)
if (checkinDate.Date == null)
if (checkinDate.Date.ToSting()== null)
if (checkinDate.ToString() == null)
if (checkinDate.SelectedDate == null) // Selected date was underlined. Error message:CalendarDatePicker does not contain a definition of 'SelectedDate'

I have also set the null to other condition such as '0' and "". But to no avail.

Source researched: Date time picker validations , Check if DatePicker value is null , String was not recognized as a valid DateTime " format dd/MM/yyyy" ,

The complete piece of code for your reference:

private void CheckInbtn_Click(object sender, RoutedEventArgs e)
    {
        List<HotelRoom> list1 = new List<HotelRoom>();
        string name = guest.Text;
        string no = passportNo.Text;
        DateTime cid = DateTime.Parse(checkinDate.Date.ToString());
        DateTime cod = DateTime.Parse(checkoutDate.Date.ToString());
        Stay s = new Stay(cid, cod);
        if (checkinDate is null)
        {
            checkInbtn.IsEnabled = false;
        }
        for (int i = 0; i < guestList.Count; i++)
        {
            if (name != guestList[i].Name || no != guestList[i].PpNumber)
            {
                Membership m = new Membership("Ordinary", 0);
                Guest g = new Guest(name, no, s, m, true);
                guestList.Add(g);
            }
            else
            {
                guestList[i].HotelStay = s;
            }
        }
        if (guest.Text.Trim().Length > 0 && passportNo.Text.Trim().Length > 0)
        {
            foreach (HotelRoom r in selectedRoomList)
            {
                list1.Add(r);
            }
            foreach (HotelRoom hotel in list1)
            {
                hotel.IsAvail = false;
                s.AddRoom(hotel);
                selectedRoomList.Remove(hotel);
            }
            statusTxt.Text = "Check in Successful";
            AvailableRoomListView.ItemsSource = null;
            SelectedRoomListView.ItemsSource = null;
        }
    } 
CodeWhite
  • 1
  • 3
  • 2
    I think you should use an event (`DataChanged` in your case) and check if the value is empty or not (thus enabling / disabling your controls) from there. Read the docs: https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.calendardatepicker.datechanged – ymz Jan 27 '19 at 12:59

1 Answers1

0

Try this:

if(!String.IsNullOrEmpty(dateTimePicker1.Value.ToShortDateString()))
{
    //your logic here
}
Hasan
  • 247
  • 7
  • 22
Jovan
  • 64
  • 5