none
The string has not been recognized as valid DateTime RRS feed

  • Question

  • 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



    Wednesday, September 19, 2018 6:17 AM

Answers

  • Assign str.Item("DateTimePicker1") into a variable.  Then test to see if the variable is not nothing, is not dbnull.value, and is not a blank string.  If all those checks pass, then assign the value to the datetime picker.

    Dim itemValue = sdr.Item("DateTimePicker1")
    If itemValue IsNot Nothing
       If itemValue IsNot DBNull.Value
          If Not String.IsNullOrWhitespace(itemValue.ToString)
             DateTimePicker1.Text = itemValue.ToString()
          End If
       End If
    End If
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by KeesBlunder Thursday, September 20, 2018 2:20 PM
    Wednesday, September 19, 2018 1:48 PM
    Moderator

All replies

  • Hi,

    Do you want to make a judgment on the data is null?

    If sdr("Data") Is DBNull.Value Then
                        MsgBox("null")
                    Else
                        DateTimePicker1.Text = sdr("Data")
                    End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 19, 2018 7:22 AM
  • Assign str.Item("DateTimePicker1") into a variable.  Then test to see if the variable is not nothing, is not dbnull.value, and is not a blank string.  If all those checks pass, then assign the value to the datetime picker.

    Dim itemValue = sdr.Item("DateTimePicker1")
    If itemValue IsNot Nothing
       If itemValue IsNot DBNull.Value
          If Not String.IsNullOrWhitespace(itemValue.ToString)
             DateTimePicker1.Text = itemValue.ToString()
          End If
       End If
    End If
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by KeesBlunder Thursday, September 20, 2018 2:20 PM
    Wednesday, September 19, 2018 1:48 PM
    Moderator
  • Thanks for the reply Reed,

    Getting this error , am using the 3.5 Framework

    IsNullOrWhitespace' is not a member of 'String

    I already put this funtion in , but no luck

     Public Shared Function IsNullOrWhiteSpace(ByVal value As String) As Boolean
            If value IsNot Nothing Then
    
                For i As Integer = 0 To value.Length - 1
    
                    If Not Char.IsWhiteSpace(value(i)) Then
                        Return False
                    End If
                Next
            End If
    
            Return True
        End Function

    Wednesday, September 19, 2018 2:06 PM
  • Have you tried "System.String.IsNullOrWhiteSpace"

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Wednesday, September 19, 2018 3:34 PM
  • Alternatively maybe databinding would work for you. The last arg is value to use if null

    DateTimePicker1.DataBindings.Add("Value", BS, "JN_CREATION_DATE", True, DataSourceUpdateMode.OnValidation, Now)


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Wednesday, September 19, 2018 3:37 PM
  • Thanks Reed ,

    I changed the Frame work to 4.0 and it works.

    Thursday, September 20, 2018 2:20 PM