Error comparing Vb.net dates

Asked

Viewed 16 times

0

I made a date comparison function. It compares the date of an input with a date column of a datatable. It follows the code:

    Public Shared Function GetNextDiaUtil(d As Date) As Date
            Dim feriados As DataTable = ManipulaDB.ConsultaSql("SELECT CAMPO2 FROM TAB_FERIADOS")
            If d.DaysOfWeek = DaysOfWeek.Saturday Then
                d = d.AddDays(2)
            ElseIf d.DayOfWeek = DaysOfWeek.Sunday Then
                d = d.AddDays(1)
            End If

            For Each data As DataRow In feriados.Rows
                If d.Equals(CDate(data.ToString)) Then
                    d = d.AddDays(1)
                End If
            Next
        Return d
    End Function

The following error is released: Conversion from string "System.Data.Datarow" to type 'Date' is not Valid. If instead of data.ToString put a string as 07-09-2021 is compared correctly. How do I compare a type Date with a Datarow?

  • holidays. Rows I think this returns a collection so can’t just give a toString and also have to check via debug what is inside data

  • CAMPO2 is of what type?

  • @novic the CAMPO2 is of type DATE in the database.

1 answer

0

Use "Datagridviewrow" instead of "Datarow" to be able to go precisely to the value you need... Cells(0) means that the value you want is in column 0... If it is in column 1, then use Cells(1) and so on. The code below is to work

  Public Shared Function GetNextDiaUtil(d As Date) As Date
            Dim feriados As DataTable = ManipulaDB.ConsultaSql("SELECT CAMPO2 FROM TAB_FERIADOS")
            If d.DaysOfWeek = DaysOfWeek.Saturday Then
                d = d.AddDays(2)
            ElseIf d.DayOfWeek = DaysOfWeek.Sunday Then
                d = d.AddDays(1)
            End If

            For Each data As DataGridViewRow In feriados.Rows
                If d = CDate(data.Cells(0).Value) Then
                    d = d.AddDays(1)
                End If
            Next
        Return d
    End Function

Browser other questions tagged

You are not signed in. Login or sign up in order to post.