我在远程服务器上收到此错误,但相同的代码在本地执行正常.请不要说它很糟糕,或者给我你动态sql的咆哮,我没有写它,只是想弄清楚为什么它会抛出异常.突出显示的错误是第56行.
Protected Sub drpDateRange_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpRange.SelectedIndexChanged Dim sql As String = "SELECT postedDate, inspectionType FROM tInspectionRequest WHERE source_lookupID = 'IRS_WEB' " If _franchiseID > 0 Then sql &= " and franchiseeID = " & _franchiseID.ToString Dim db As New Database Dim ds As DataSet = db.selectQuery(sql) Dim dv As New DataView(ds.Tables(0)) dv.RowFilter = "inspectionType='Buyer' AND postedDate >= #" & DateTime.Now.AddDays(-1) & "#" lblB1.Text = dv.Count End Sub
这是例外,似乎DateTime.Now.AddDays(-1)未能被强制转换为日期时间?无论它是一个转换问题/日期到字符串错误,奇怪的是它只在远程服务器上失败,而不是在本地.
字符串未被识别为有效的DateTime.描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
异常详细信息:System.FormatException:String未被识别为有效的DateTime.
来源错误:
Line 55: Dim dsInspectionHistory As DataSet = objDB.selectQuery(sqlInspectionHistory)
Line 56: Dim dvInspectionHistory As New DataView(dsInspectionHistory.Tables(0))
Line 57: dvInspectionHistory.RowFilter = "inspectionType='Buyer' AND postedDate >= #" & DateTime.Now.AddDays(-1).ToString & "#"
Line 58: lblB1.Text = dvInspectionHistory.Count
Line 59: dvInspectionHistory.RowFilter = "inspectionType='Seller' AND postedDate >= #" & DateTime.Now.AddDays(-1) & "#"
[FormatException:String未被识别为有效的DateTime.] System.DateTimeParse.Parse(String s,DateTimeFormatInfo dtfi,DateTimeStyles样式)+2291962 System.DateTime.Parse(String s,IFormatProvider provider)+26 System.Data.ConstNode. .ctor(DataTable表,ValueType类型,Object常量,Boolean fParseQuotes)+485 System.Data.ExpressionParser.Parse()+830 System.Data.DataExpression..ctor(DataTable表,String表达式,Type类型)+124 System. Data.DataView.set_RowFilter(String value)+ x1 xxxx中的controls_inspectionRequestChart.drpRange_SelectedIndexChanged(Object sender,EventArgs e)xxxx中的controls_inspectionRequestChart.Page_Load(Object sender,EventArgs e)System.Web.UI.Control.OnPreRender(EventArgs e)+2117788 System.Web.UI.Control.PreRenderRecursiveInternal()+86 System.Web.UI.Control.PreRenderRecursiveInternal()+170 System.Web.UI.Control.PreRenderRecursiveInternal()+170 System.Web.UI.Control.PreRenderRecursiveInternal()+170 System.Web.UI.Control.PreRenderRecursiveInternal()+ 170 System.Web.UI. Control.PreRenderRecursiveInternal()+170 System.Web.UI.Control.PreRenderRecursiveInternal()+170 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+2041Boolean includeStagesAfterAsyncPoint)+2041Boolean includeStagesAfterAsyncPoint)+2041
这可能是由于服务器和开发PC之间的区域日期时间设置差异造成的.代码中的datetime和string之间存在隐式转换(记住它是内联sql(编辑:RowFilter语法),你没有设置参数).
尝试格式化字符串
dv.RowFilter = "inspectionType='Buyer' AND postedDate >= #" & DateTime.Now.AddDays(-1).ToString("MMM dd yyyy hh.mm.ss") & "#"