The string has not been recognized as valid DateTime

  • Thread starter Thread starter KeesBlunder
  • Start date Start date
K

KeesBlunder

Guest
Hello,

I store some Datetimepickers in a Sqlce database , i use this to make them empty at startup.

Private Sub SetDateTimePickerFormat(Optional format As String = " ",
Optional container As Control = Nothing)

If container Is Nothing Then container = Me
For Each c As Control In container.Controls()
If TypeOf c Is DateTimePicker Then
Dim d As DateTimePicker = DirectCast(c, DateTimePicker)
d.CustomFormat = format
d.Format = DateTimePickerFormat.Custom
If d.Tag Is Nothing Then
'only once
AddHandler d.ValueChanged, AddressOf DateTimePickerS_ValueChanged
d.Tag = "has handler"
End If
ElseIf c.Controls.Count > 0 Then 'container
'yes
SetDateTimePickerFormat(format, c)
'Else
'no
' Debug.WriteLine(c.Name)
End If
Next
End Sub
Private Sub DateTimePickerS_ValueChanged(sender As Object,
e As EventArgs)
Dim dtp As DateTimePicker = DirectCast(sender, DateTimePicker)
If dtp.CustomFormat.Trim = "" Then
dtp.CustomFormat = "dd-MM-yyyy"
End If
End Sub

That works fine, when i enter a date and save it to the database and back no problem.

But this is not a date(" ")



and the error is of course

The string has not been recognized as valid DateTime

The question is , how do i check if the data from the database is not Dbnull or ("") or (" ") before i fill the Datetimepicker ?

I fill with this

cmd = New SqlCeCommand("SELECT * FROM Gevarenleeg WHERE Id = '" & Gevarenid.Text & "'", con)
If con.State = ConnectionState.Closed Then con.Open()
Dim sdr As SqlCeDataReader = cmd.ExecuteReader()
While sdr.Read = True

DateTimePicker1.Text = sdr.Item("DateTimePicker1")

End While
sdr.Close()

If con.State = ConnectionState.Open Then con.Close()
End If


This is a sloppy way to do this and not the solution when i change it to textbox2

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
If TextBox2.Text = ("") Or TextBox2.Text = (" ") Then
Else
Datetimepicker1.Text = TextBox2.Text
End If
End Sub

Continue reading...
 
Back
Top