Click here to Skip to main content
15,904,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written the following code on text change event of txtDate textbox.
C#
DateTime dt1 = DateTime.Parse(txtVrDate.Text.Trim()).Date;
DateTime dt2 = DateTime.Now.Date;

if ((dt2 - dt1).Days > 2)
{

    ScriptManager.RegisterStartupScript(this, typeof(string), "myalert", "alert('InValid Date');", true);
    txtDate.Text = DateTime.Parse(DateTime.Now.ToShortDateString()).ToString("dd/MM/yyyy");
    txtDate.Focus();
}

Its working on local asp.net development server but when published on IIS it does not validate textbox.I m not able to figure out whats the problem.
Please help....

thanks in advance...
Posted
Updated 2-May-14 21:45pm
v2
Comments
DamithSL 3-May-14 3:47am    
you get any exception?

1 solution

Probably, it either user input or a localization problem: DateTime.Parse uses the locale of the PC it is running on - which will be your PC for development, but will be the hosting PC for production. Where the computer running IIS is located physically will determine what locale it is set for, and thus what date/time format it expects - this may not be the same as the client date/time format.

The best solution is to not use text boxes for date inputs: there is a Calendar control (or various similar) which does a better job, but which presents data in the client format, and returns a DateTime value directly to your code.
 
Share this answer
 
Comments
pwavell 3-May-14 6:02am    
i am using ajax calender control.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900