0
votes

I have a search feature (written by my predecessor) which takes date range, id, available programs as input and displays the results in a gridview. The feature works fine most of the time (I have tested it) but for one of the users of my application it gave this error message. I am not able to reproduce this error by myself in order to fix it. Don't know what's wrong!

Can you guys help?

Exception of type 'System.Web.HttpUnhandledException' was thrown. System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at System.Convert.ToDateTime(String value) at APP_ViewFollowupWorkload.GetFilterString() in d:\SharedServices\APP\ViewFollowupWorkload.aspx.cs:line 1415
at APP_ViewFollowupWorkload.Page_Load(Object sender, EventArgs e) in d:\SharedServices\APP\ViewFollowupWorkload.aspx.cs:line 268
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.app_viewfollowupworkload_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\bad754dd\a11f74ff\App_Web_viewfollowupworkload.aspx.ae7ca9bd.uwyek3vs.0.cs:line 0
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Here is the .cs file code where the error generating:

if (txtDateTo.ToString() != string.Empty)
        {
            if (txtDateTo.ToString().Length > 2)
                strFilter = strFilter + " AND submission_date <= ''" + Convert.ToString(Convert.ToDateTime(txtDateTo.ToString()) + new TimeSpan(1, 0, 0, 0)) + "''";

    }
2
Try to log the date, and let the user do his magic. Alternative, surround it with a try/catch and mail the stack tace + the user input to your mail. Then you get notified next time the error happens and you get the buggy date.mslot

2 Answers

5
votes

The error indicates that the passes in value of txtDateTo is not a valid DateTime - say 32/11/2011.

You can change the code to something that will not throw an exception by using one of the DateTime.TryParse overloads. This will not parse invalid values, but will avoid an exception being thrown - you still need to determine what to do in such a case.

0
votes

Try to use ParseExact instead - here (MSDN) .

Maybe they use some unusual date time format. Ask your user which value in textbox is used to convert to datetime.