locked
Using date condition to retrieve records from Access database RRS feed

  • Question

  • User-1627952579 posted

    I use Access as the database. (Running .net 1.1)

    I have used a code to retieve records that fall between a particular date. There is no error when I run the page.

    But the result shows all the records and the condition that I have specified does not have any effect.

    Below is the Query I have used in my ASP.net page

    Dim cmdAR1 As New OleDbCommand("select Username,EquipmentCode,EquipmentName,Department,Division,LastCertDate,CertDueDate,FreqCalibration,Remarks from register where CertDueDate> " & CDD_Date & " and CertDueDate < " & FCDD_Date & " order by CertDueDate asc", arConn)

    The Dates are stored in Variabled (CDD_Date and FCDD_Date)

    As this did work I even tried to hard code a date

    Dim cmdAR1 As New OleDbCommand("select Username,EquipmentCode,EquipmentName,Department,Division,LastCertDate,CertDueDate,FreqCalibration,Remarks from register where CertDueDate> 12/12/2007 order by CertDueDate asc", arConn)

    But even then it displayed all records (even those before YR 2006) in the datagrid that I use.

    I have been searching the net all day, but no luck.... What am I doing wrong here?

    Pls help.

     

     

    Monday, April 28, 2008 8:25 AM

Answers

  • User-821857111 posted

    If you insist on using dynamic SQL (variables concatenated into a string) you need to make sure the datatype delimiters are correct.  In this case, since the datatype is datetime, you should delimit your values using single quotes or octothorpes (#).  However, you can get round this problem, and produce much safer code by using parameters: http://www.mikesdotnetting.com?Article.aspx?ArticleID=26

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 28, 2008 8:56 AM