String was not recognized as a valid DateTime

Posted by Chandru_ra under ASP.NET on 9/10/2010 | Points: 10 | Views : 6683 | Status : [Member] | Replies : 2
how to rectify this problem?

am using this coding

#region Find Date Difference
string Date = "04/21/2005"; // in the format mm/dd/yyyy;
DateTime temp = DateTime.Parse(Date);
//DateTime temp = DateTime.Parse(dt.);
DateTime dtstart = Convert.ToDateTime(txtDate.Text.Trim());// this line Error
DateTime dtEnd = Convert.ToDateTime(BadateAmtreceived.SelectedDateFormatted.ToString());
TimeSpan Days = dtstart.Subtract(dtEnd);
DateDiff = Days.Days;
lblDateDiff.Text = DateDiff.ToString();
txtPenaltyAmt.Text = Convert.ToString(Convert.ToDouble(txtDailyInterest.Text) * DateDiff);



Posted by: SheoNarayan on: 9/10/2010 [Administrator] HonoraryPlatinum | Points: 25

Which line has a problem? Can you debug and find out ?

Looks like either you are not giving correct date format in the txtDate.Text or BadateAmtreceived dropdown/listbox. You will need to ensure that both places has correct date values (default mm/dd/yyyy) format.

Sheo Narayan

Chandru_ra, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 9/16/2010 [Member] Bronze | Points: 25

Always validate from server side too. You can have a function like as follows to check the date format.

 public static bool IsDate(string strDate)

DateTime outDate;
return DateTime.TryParse(strDate, out outDate);

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Chandru_ra, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response