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