2011-06-10 80 views
0

我有一个搜索功能(由我的前任编写),它将日期范围,ID,可用程序作为输入并在GridView中显示结果。该功能在大多数时间都正常工作(我已经测试过),但是对于我的应用程序的用户之一,它提供了此错误消息。我无法自己重现此错误以解决此问题。不知道什么是错的!字符串未被识别为有效日期时间

你们能帮忙吗?

类型'System.Web.HttpUnhandledException'的异常被抛出。 System.FormatException:字符串未被识别为有效的DateTime。 在System.DateTimeParse.Parse(字符串s的DateTimeFormatInfo dtfi,DateTimeStyles样式)
在System.Convert.ToDateTime(字符串值) 在APP_ViewFollowupWorkload.GetFilterString()在d:\ SharedServices \ APP \ ViewFollowupWorkload.aspx.cs:在APP_ViewFollowupWorkload.Page_Load(对象发件人,EventArgs e)在d线1415
:\ SharedServices \ APP \ ViewFollowupWorkload.aspx.cs:线268
在错误帮助(IntPtr的FP,对象O, Object t,EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
在System.Web.UI.Control.LoadRecursive()
在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)
在System.Web.UI.Page.HandleError(例外五)
在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)
在System.Web.UI.Page.ProcessRequest(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)
在System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.Pro cessRequest(HttpContext上下文)
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:线0
在System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
在System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔& completedSynchronously)

这里是生成错误的.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)) + "''"; 

    } 
+1

尝试记录日期,并让用户发挥他的魔力。另一种方法是,用try/catch将它包围起来,然后将堆栈+用户输入发送到您的邮件。然后你会在下次发生错误时得到通知,并且你会得到越野车日期。 – mslot 2011-06-10 21:15:24

回答

5

的错误指示的txtDateTo值的通行证是不是一个有效DateTime - 说32/11/2011

您可以将代码更改为不会通过使用DateTime.TryParse重载之一而抛出异常的内容。这不会解析无效的值,但会避免抛出异常 - 您仍然需要确定在这种情况下要做什么。

+1

同意但由于这是一个用户输入的值,我建议使用'CompareValidator'与'CompareValidator.Operator =“DataTypeCheck”'和'CompareValidator.Type =“Date”'以防止用户输入无效的日期字符串首先。不要忘记实施服务器端验证。 – pseudocoder 2011-06-10 21:32:01

0

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

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

相关问题