字符串未被识别为有效的DateTime

时间:2021-11-20 10:34:09

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!

我有一个搜索功能(由我的前任编写),它将日期范围,ID,可用程序作为输入,并在gridview中显示结果。该功能在大多数情况下工作正常(我已经测试过)但是对于我的应用程序的一个用户,它给出了此错误消息。我无法自己重现此错误以便修复它。不知道出了什么问题!

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)

抛出了类型'System.Web.HttpUnhandledException'的异常。 System.FormatException:String未被识别为有效的DateTime。在System.DateTimeParse.Parse(String s,DateTimeFormatInfo dtfi,DateTimeStyles样式)的位于System.Convert.ToDateTime(字符串值)的APP_ViewFollowupWorkload.GetFilterString()在d:\ SharedServices \ APP \ ViewFollowupWorkload.aspx.cs:第1415行在APP_ViewFollowupWorkload系统中的System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,Object o,Object t,EventArgs e)中的d:\ SharedServices \ APP \ ViewFollowupWorkload.aspx.cs:第268行中的.Page_Load(Object sender,EventArgs e)位于System.Web.UI.Page的System.Web.UI.Control.LoadRecursive()的System.Web.UI.Control.OnLoad(EventArgs e)上的.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,EventArgs e)。 System.Web.UI.Page.ProcessRequest处的System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)上的System.Web.UI.Page.HandleError(Exception e)中的ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint) Boolean includeStagesBeforeAsyncPoint,位于System.Web.UI.Page.UI.Page.ProcessRequest()的System.Web.UI.Page.ProcessRequest()处的System.Web.UI.Page.ProcessRequest()处于System.Web.UI.Page.ProcessRequest(HttpContext context)的ASP.app_viewfollowupworkload_aspx.ProcessRequest处的布局includeStagesAfterAsyncPoint( HttpContext上下文)在C:\ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727 \ Temporary ASP.NET Files \ root \ bad754dd \ a11f74ff \ App_Web_viewfollowupworkload.aspx.ae7ca9bd.uwyek3vs.0.cs:System.Web中的第0行。 System.Web.HttpApplication.ExecuteStep中的HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()(IExecutionStep step,Boolean&completedSynchronously)

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

这是生成错误的.c​​s文件代码:

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 个解决方案

#1


5  

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

该错误表示txtDateTo的值传递不是有效的DateTime - 比如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.

您可以使用DateTime.TryParse重载之一将代码更改为不会引发异常的代码。这不会解析无效值,但会避免抛出异常 - 您仍需要确定在这种情况下要执行的操作。

#2


0  

Try to use ParseExact instead - here (MSDN) .

尝试使用ParseExact - 这里(MSDN)。

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

也许他们使用一些不寻常的日期时间格式询问您的用户文本框中的哪个值用于转换为datetime。

#1


5  

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

该错误表示txtDateTo的值传递不是有效的DateTime - 比如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.

您可以使用DateTime.TryParse重载之一将代码更改为不会引发异常的代码。这不会解析无效值,但会避免抛出异常 - 您仍需要确定在这种情况下要执行的操作。

#2


0  

Try to use ParseExact instead - here (MSDN) .

尝试使用ParseExact - 这里(MSDN)。

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

也许他们使用一些不寻常的日期时间格式询问您的用户文本框中的哪个值用于转换为datetime。