Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting the following error as :Cannot perform '>=' operation on System.String and System.DateTime. When I am trying to filter my datatable dtclone so that i can view the report within the date range.For selecting the dates my using datepicker as dtpfromdate and dtptodate. The code is working when i am selecting the system date shortdatepattern as 'M/d/yyyy' but it is giving error wen i switch to other short date pattern.error is coming on filter.Plz help me n say me if i m not clear in my question.The code is below:
'This event is fired when user want to display records within the date range selected
VB
Private Sub btnGetReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetReport.Click
 
        Try
            btnClearReport.Enabled = True
            btnGetReport.Enabled = False
           
            dv = New DataView(dtClone)
            Dim strCurCulture As String = System.Threading.Thread.CurrentThread.CurrentCulture.Name
            Dim culNew As New System.Globalization.CultureInfo(strCurCulture)
            culNew.DateTimeFormat.ShortDatePattern = "M/d/yyyy"
            culNew.DateTimeFormat.DateSeparator = "/"
          
            System.Threading.Thread.CurrentThread.CurrentCulture = culNew
            System.Threading.Thread.CurrentThread.CurrentUICulture = culNew
           
            Dim filter As String = "[Transaction Date] >= #" & fnGetClientDate(dtpFromDate.Text) & "# and [Transaction Date] <= #" & fnGetClientDate(dtpToDate.Text) & "#"
            dv.RowFilter = filter
            gvIPDClaimReportView.DataSource = dv
 
       
        Catch ex As Exception
            clsWriteLog.WriteLog("Exception in btnGetReport_Click")
            clsWriteLog.WriteLog("Exception : " + ex.Message)
        End Try
 
    End Sub
   End Sub
  'To get the client selected date in ddmmyyyy format
    Public Function fnGetClientDate(ByVal ddate As DateTime) As String
 
        Dim dte As DateTime = ddate
        ''Added as on 10-10-11 to get proper date for filter
        Dim day As String = IIf(Len(dte.Day.ToString()) = 1, "0" & dte.Day.ToString(), dte.Day.ToString())
        Dim month As String = CInt(dte.Month)
        Dim year As String = dte.Year
        ' Dim strCardDate As String = _day + "-" + _month + "-" + _year
        '' Dim strCardDate As String = day + month + year
        ''Dim strCardDate As String = _day + "/" + _month + "/" + _year
        Dim strCardDate As String = month + "/" + day + "/" + year
        Try
 
        Catch ex As Exception
            clsWriteLog.WriteLog("Exception in fnGetClientDate")
            clsWriteLog.WriteLog("Exception : " + ex.Message)
        End Try
        Return strCardDate
 
    End Function

 
On load am adding this code
Collapse | Copy Code

Dim strCurCulture As String = System.Threading.Thread.CurrentThread.CurrentCulture.Name
         Dim culNew As New System.Globalization.CultureInfo(strCurCulture)
         culNew.DateTimeFormat.ShortDatePattern = "M/d/yyyy"
         culNew.DateTimeFormat.DateSeparator = "/"
         System.Threading.Thread.CurrentThread.CurrentCulture = culNew
         System.Threading.Thread.CurrentThread.CurrentUICulture = culNew
 
         Me.StartPosition = FormStartPosition.CenterScreen
         'Loads the by default data fovr IPD Claim Report
         btnClearReport.Enabled = False
         btnGetReport.Enabled = True
         dtClone = objMDBLayer.fnGetIPDTransactionReports("")
         For i = 0 To dtClone.Rows.Count - 1
             Dim strTrDate As String = dtClone.Rows(i)(11).ToString()
             Dim format As String = "ddMMyyyy"
             'format.DateSeparator = "-"
             If (Not strTrDate.Contains("/")) Then
                 Dim dtTrDate As DateTime = DateTime.ParseExact(strTrDate, format, Nothing)
                 dtClone.Rows(i)(11) = dtTrDate.ToString("MM/dd/yyyy")
                 ' dtClone.Rows(i)(11) = dtTrDate
             End If
         Next
         gvIPDClaimReportView.DataSource = dtClone
Posted
Updated 7-Feb-12 22:35pm
v3

you allways have to compare the same objecttypes.

You can't compare apples and eggs we say in Holland
 
Share this answer
 
[Transaction Date] >= #" & fnGetClientDate(dtpFromDate.Text)

Make sure the type of the two fields is DateTime.
 
Share this answer
 
Comments
sofia3 8-Feb-12 5:52am    
Dim filter As String = "Convert([Transaction Date],'System.DateTime') >= #" & fnGetClientDate(dtpFromDate.Text) & "# and Convert([Transaction Date],'System.DateTime') <= #" & fnGetClientDate(dtpToDate.Text) & "#"


It is giving error that string is not a valid datetime
sofia3 8-Feb-12 5:57am    
it is coming like this :[Transaction Date] >= #2/07/2012# and [Transaction Date] <= #2/07/2012#

and transaction date is also in this format like :2/07/2012

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900