Search code examples
sqlvb.netdataviewrowfilter

VB.NET Error: "Operand Missing" when using RowFilter Command


I am trying to filter a dataview based on two different columns: StartTime(DateTime) and EndTime(DateTime)...

Code:

 Dim dvLosses As New DataView(dsLossData.Tables("AllData").DefaultView.ToTable(True, New String(){"ID", "Name", "StartTime", "EndTime", "Loss"}), "", "StartTime desc", DataViewRowState.CurrentRows)

 dvLosses.RowFilter = "Where StartTime > '" + hfFrom.Value + "' and EndTime < '" + hfTo.Value + "'"

Error: Exception details: Syntax error: Missing operand after 'StartTime' operator. at System.Data.ExpressionParser.Parse() at System.Data.DataExpression..ctor(DataTable table, String expression, Type type) at System.Data.DataView.set_RowFilter(String value)


Solution

  • The RowFilter property is an expression more or less like the WHERE expression but it doesn't require the word WHERE. Just type the conditional expression

     dvLosses.RowFilter = "StartTime > '" + hfFrom.Value + 
                          "' and EndTime < '" + hfTo.Value + "'"
    

    Instead I am more concerned on the fact that you treat two fields that appear to be a datetime as they were strings. If they are really datetime fields then you need a different syntax and quoting character

     dvLosses.RowFilter = "StartTime > #" + hfFrom.Value.ToString("M/d/yyyy") + 
                          "# and EndTime < #" + hfTo.Value.ToString("M/d/yyyy") + "#"
    

    (Assuming that you hfFrom.Value and hfTo.Value are DateTime variables)

    You can find more info on the syntax accepted by the RowFilter property looking at the Expression property of a DataColumn