im a beginner in C sharp. im stuck with the following. i hope someone can get me on the track again.

i want to add a validation for user input such as making sure the year of the yearTextBox is not greater than the current year +2.

private void yearTextBox_Validating(object sender, CancelEventArgs e)
{
}

i think it should be something like this

if (yearTextBox.Text >= DateTime.......

current year + 2

but i cant figure out how to put this.

i think you can set validation in design page itself as Range validator ,

<asp:Label ID="lbl" runat="server"></asp:Label>
                <asp:TextBox ID="txt" runat="server"></asp:TextBox>
                <asp:RangeValidator id="rv" runat="server" ControlToValidate="txt" MaximumValue="2008" MinimumValue="2003" ErrorMessage="Range Exceeded"></asp:RangeValidator>

the max n min values only have change

the situation is as follows. i have a database. one table with the name listing. one of the column name is year.

i created a form with datagridview. when i enter new information it must validate the user input. i think this validation has to be created on form level. this validation must check the year is not greater than the current year + 2.

its not range validation what im looking for.

First Parse the Text into DateTime object. Then compare the DateTime objects

string sDateTime = "20/8/2010"; // yearTextBox.Text

    DateTime textDateTime = DateTime.Parse(sDateTime) ;
    DateTime compareDateTime = DateTime.Now.AddYears (2);
    if (textDateTime.CompareTo(compareDateTime) >= 0)
    {
        MessageBox.Show("It is greater"); 
    }

when i add your code like this to the Validating event from yearTextBox.Text

private void yearTextBox_Validating(object sender, CancelEventArgs e)
        {

           string sDateTime = yearTextBox.Text;

            DateTime textDateTime = DateTime.Parse(sDateTime);
            DateTime compareDateTime = DateTime.Now.AddYears(2);
            if (textDateTime.CompareTo(compareDateTime) >= 0)
            {
                MessageBox.Show("It is greater");
            }

        }

i get format exception.

DateTime textDateTime = DateTime.Parse(sDateTime);

String was not recognized as a valid DateTime.

what am i doing wrong?

I think the Format of the Date is not correct or It may contains invalid date so the exception happens.

If you give the input as "20/8/2010", is this working?

I think year is the only the Input and Not Date, like "20/8/2010". Just want to check only year?

If so, try this

int givenYear = int.Parse( yearTextBox.Text );
     
    int currentYear = DateTime.Today.Year ;
    if ( givenYear > currentYear + 2 )
    {
        MessageBox.Show("It is greater than 2 year");
    }
Input : 2008
  Output : No Message Box

  Input :2011
  Output: MessageBox "It is greater than 2 year"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.