I have an error popping up for the DateTime (DateofBirth) & int (TelephoneNumber) fields.

Currently doing a simple library application to register new customer records within the library - trying to implement validation so when Save button is clicked (new record is created), the fields are checked to see if data was submitted into them.

For eg. I used the below code for the NAME field, which worked correctly.

Further below are the DateofBirth & TelephoneNumber fields, which I implemented the code similar to Name, however, they don't work (I am getting errors). I am still new to C#, help would be greatly appreciated.

Thank you very much.

Example that works.

Name = this.txtName.Text;

        if (string.IsNullOrWhiteSpace(Name))
        {
            MessageBox.Show("Customer's name is required. Please do not leave this field empty.");

            using (var erp_provider = new ErrorProvider())

                erp_Provider.SetError(this.txtName, "Customer's name is required. Kindly submit the details in the given field.");

            return;
        }

///// Issues with the below code:

DateofBirth = DateTime.Parse(msktxtDateofBirth.Text);

        if (DateofBirth == DateTime.MinValue)
        {
            MessageBox.Show("Customer's Date of Birth is required. Please do not leave this field empty.");

            using (var erp_provider = new ErrorProvider())

                erp_Provider.SetError(this.msktxtDateofBirth, "Customer's Date of Birth is required. Kindly submit the details in the given field.");

            return;
        }

TelephoneNumber = Convert.ToInt32(txtTelephoneNumber.Text);

        if (TelephoneNumber == null)
        {
            MessageBox.Show("Customer's Telephone Number is required. Please do not leave this field empty.");

            using (var erp_provider = new ErrorProvider())

                erp_Provider.SetError(this.txtTelephoneNumber, "Customer's Telephone Number is required. Kindly submit the details in the given field.");

            return;
        }

DateofBirth error:

(Exception Unhandled) System.FormatException: 'String was not recognized as a valid DateTime.'

TelephoneNumber error:

(Exception Unhandled) System.FormatException: 'Input string was not in a correct format.'

1 Answers

0
Steve On Best Solutions

When dealing with user input and validation of this input, you shouldn't use methods that raise an exception in case of invalid inputs. Instead the TryParse method's family allow you to try to convert the input and in case of error they don't raise an exception but return true or false.

if(!DateTime.TryParse(msktxtDateofBirth.Text, out DateTime DateofBirth))
{
    MessageBox.Show("Customer's Date of Birth is required. Please do not leave this field empty.");
    using (var erp_provider = new ErrorProvider())
        erp_Provider.SetError(this.msktxtDateofBirth, "Customer's Date of Birth is required. Kindly submit the details in the given field.");
    return;
}

if (!Int32.TryParse(txtTelephoneNumber.Text, out int TelephoneNumber))
{
    MessageBox.Show("Customer's Telephone Number is required. Please do not leave this field empty.");
    using (var erp_provider = new ErrorProvider())
        erp_Provider.SetError(this.txtTelephoneNumber, "Customer's Telephone Number is required. Kindly submit the details in the given field.");
    return;
}