Pooveshin Pooveshin - 5 months ago 17
SQL Question

Disable past dates using texbox Asp.Net C#

I am working on an Asp.Net C# web app pulling data from from SQL Server. I am using a textbox with properties set as date for uses to select a date, I need a method or code that will disable the user from selecting a past day from the present?

Or alternatively when the user selects a date and clicks save the database must give an error for the user to select a future date and not accept what has been saved.

Any help is much appreciated.

Answer

Well, having a textbox will only hold the date value, your validation logic, which is not allowing past date to be saved, should be implemented at possibly form submit event or textbox mouse click out event. It could be like

DateTime inputDateTime = convert.ToDateTIme(TextBox1.Value); 
if (inputDateTime<DateTime.Now) 
{ 
  Throw Exceptoin("you have entered the wrong value");
}
 else 
{ 
  save...
}
Comments