3

I have a report which can be filtered by StartDate + EndDate, BookingID, CardNumber and Amount.

These are represented with 2 date pickers and 4 textboxes (CardNumber is split into 2 textboxes) and there is a Search button to return the report. My Search click code below is working fine by I'm wondering can I combine some or all of my validation code to reduce the amount of lines of code? Most of the validation code is similar I.E checking that BookingID, CardNumber and Amount are entered as ints.

I'm aware this question is bordering on a code review, so please let me know if it should be asked elsewhere.

protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
{
    lblInputMessage.Visible = false;
    DateTime dtStart, dtEnd;
    bool isGood = DateTime.TryParse(txtEndDate.Text, out dtEnd);

    if (DateTime.TryParse(txtStartDate.Text, out dtStart))
    {
        if (txtEndDate.Text.Trim() == "")
        {
            dtEnd = DateTime.Now;
            isGood = true;
        }
    }

    if (txtBookingID.Text.Length > 0)
    {
        int newBookingID;
        if (int.TryParse(txtBookingID.Text, out newBookingID))
        {
            isGood = true;
        }

        else
        {
            lblInputMessage.Visible = true;
            lblInputMessage.Text = "Please enter a valid BookingID.";
            return;
        }
    }

    if (txtAmount.Text.Length > 0)
    {
        int newAmount;
        if (int.TryParse(txtAmount.Text, out newAmount))
        {
            isGood = true;
        }

        else
        {
            lblInputMessage.Visible = true;
            lblInputMessage.Text = "Please enter a valid Amount.";
            return;
        }
    }

    if (txtCardNumber1.Text.Length > 0 || txtCardNumber2.Text.Length > 0)
    {
        int newCardNumber1;
        int newCardNumber2;
        if (int.TryParse(txtCardNumber1.Text, out newCardNumber1) && (int.TryParse(txtCardNumber2.Text, out newCardNumber2)))
        {
            isGood = true;
        }

        else
        {
            lblInputMessage.Visible = true;
            lblInputMessage.Text = "Please enter a valid Card Number.";
            return;
        }
    }

    if (isGood)
    {
        if (dtStart > dtEnd)
        {
            lblInputMessage.Visible = true;
            lblInputMessage.Text = "End Date must be greater than Start Date.";
            return;
        }

        lblInputMessage.Visible = false;
        LoadGridData(true);
        ajaxCollapsiblePanel1.Collapsed = true;
        ajaxCollapsiblePanel1.ClientState = "true";
        pnlContainer.Visible = true;
    }

    else
    {
        lblInputMessage.Visible = true;
        lblInputMessage.Text = "Please enter a valid date.";
        return;
    }
}
6
  • 1
    Personally, I would rather opt for the IDataErrorInfo interface, as it supports validation of your input as well, combined with binding support on multiple presentation frameworks. On the other hand, if you are using this webbased, you might also look into this Commented May 26, 2017 at 9:47
  • Is it windows form application or ASP.NET ? For ASP.NET I would rather use validation controls to perform basic validation at the client side only. Commented May 26, 2017 at 9:49
  • @Icepickle, I will have a look at your suggestions thank you. Commented May 26, 2017 at 9:53
  • @Chetan Ranpariya it is ASP.NET. Sorry I should have tagged it originally in the question. Tag added now. Commented May 26, 2017 at 9:54
  • Did you try using Validationcontrols? Commented May 26, 2017 at 9:55

1 Answer 1

4

You could modify it to use IDataErrorInfo and do something like this.

public string Error
{
    get { return String.Empty; }
}

public string this[string columnName]
{
    get
    {
        String errorMessage = String.Empty;
        switch (columnName)
        {
            case "Variable1":
                if (String.IsNullOrEmpty(Variable1))
                {
                    errorMessage = "Variable1 is required";
                }
                break;
            case "Variable2":
                if (Variable2 < 10)
                {
                    errorMessage = "Variable2 can't be less than 10";
                }
                break;
        }
        return errorMessage;
    }
}

This tutorial https://tarundotnet.wordpress.com/2011/03/03/wpf-tutorial-how-to-use-idataerrorinfo-in-wpf/ covers the basics.

Sign up to request clarification or add additional context in comments.

1 Comment

This is what I was looking for. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.