Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42454

VS 2010 Trouble with dates

$
0
0
Hello,

I am having some troubles with dates.

I have a piece of code that queries and fills a datagridview.
On the form I have a textbox where I fill in the date of birth in dutch format (dd-mm-yyyy).
In my sql server 2008 database the field date of birth is stores as a datetime format US (yyyy-mm-dd hh.mm.ss.ccc)

When I fill in the dateOfBrth I want to search in the textbox txtGebDatVan (22-02-1970) I get the error:
"The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value."

I hope you guys can push me in the right direction.
Thanks in advance.

Code:

Private Sub VulDgv()
    If IsDatum(Me.txtGebDatVan) Then
    Else
      Exit Sub
    End If

    If IsDatum(Me.txtGebDatTot) Then
    Else
      Exit Sub
    End If

    Dim ZoekGeboorteDatum As String = String.Empty
    If Me.txtGebDatVan.TextLength = 0 And Me.txtGebDatTot.TextLength = 0 Then
      ZoekGeboorteDatum = String.Empty
    ElseIf Me.txtGebDatVan.TextLength > 0 And Me.txtGebDatTot.TextLength = 0 Then
      If IsDate(txtGebDatVan.Text) Then
        ZoekGeboorteDatum = "AND M.geboortedatum >= convert(DateTime,@DatumVan)"
      Else
        MsgBox("Geen geldig datum formaat")
      End If
    ElseIf Me.txtGebDatVan.TextLength > 0 And Me.txtGebDatTot.TextLength > 0 Then
      If IsDate(txtGebDatVan.Text) And IsDate(txtGebDatTot.Text) Then
        ZoekGeboorteDatum = "AND m.geboortedatum BETWEEN @DatumVan and @DatumTot"
      Else
        MsgBox("Geen geldig datum formaat")
      End If
    ElseIf Me.txtGebDatVan.TextLength = 0 And Me.txtGebDatTot.TextLength > 0 Then
      MsgBox("Het veld 'Geboortedatum van' is leeg.")
      Me.txtGebDatVan.Focus()
      Exit Sub
    End If

    Dim sql As String = String.Format("SELECT * FROM tblMEDEWERKER AS M " & _
                                      "WHERE 1=1 {1} ", ZoekGeboorteDatum)

    Using connection As New SqlConnection(_ConnectionstringSQL),
      cmdMedewerker As New SqlCommand(sql, connection)

      Dim dsMedewerker As New DataSet
      Dim dvMedewerker As DataView
      Dim daMedewerker = New SqlDataAdapter()
      ' Try
      With cmdMedewerker.Parameters
        .AddWithValue("@DatumVan", Me.txtGebDatVan.Text)
        .AddWithValue("@DatumTot", Me.txtGebDatTot.Text)
      End With

      daMedewerker.SelectCommand = cmdMedewerker
      daMedewerker.Fill(dsMedewerker)
      dvMedewerker = dsMedewerker.Tables(0).DefaultView

      With Dgv_Medewerker
        .DataSource = dvMedewerker
      End With
    End Using
  End Sub


Viewing all articles
Browse latest Browse all 42454

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>