Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Somewhat strange .NET error involving datetime

I am getting this error on a remote server, but the same code executes fine locally. Please refrain from saying it sucks, or giving me your rant on dynamic sql, I didn't write it, just trying to figure out why it's throwing an exception. The highlighted error is line 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

Here is the exception, it seems like DateTime.Now.AddDays(-1) is failing being cast as a datetime? Regardless if it's a casting issue / date to string error, it's strange it fails only on the remote server, and not locally.

String was not recognized as a valid DateTime. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: String was not recognized as a valid DateTime.

Source Error:

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 was not recognized as a valid DateTime.] System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) +2291962 System.DateTime.Parse(String s, IFormatProvider provider) +26 System.Data.ConstNode..ctor(DataTable table, ValueType type, Object constant, Boolean fParseQuotes) +485 System.Data.ExpressionParser.Parse() +830 System.Data.DataExpression..ctor(DataTable table, String expression, Type type) +124 System.Data.DataView.set_RowFilter(String value) +161 controls_inspectionRequestChart.drpRange_SelectedIndexChanged(Object sender, EventArgs e) in xxxx controls_inspectionRequestChart.Page_Load(Object sender, EventArgs e) in xxxx 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) +2041

like image 724
Shawn Avatar asked Nov 28 '25 22:11

Shawn


1 Answers

This might be due to a regional datetime settings difference between the server and your dev PC. There is an implicit conversion between datetime and string in the code (remember it is inline sql (Edit: RowFilter syntax), you're not setting a parameter).

Try formatting the string

dv.RowFilter = "inspectionType='Buyer' AND postedDate >= #" & DateTime.Now.AddDays(-1).ToString("MMM dd yyyy hh.mm.ss") & "#"
like image 62
Christopher Edwards Avatar answered Nov 30 '25 11:11

Christopher Edwards



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!