Hi, I have this question about how to retrieve a column with DateTime from MySQL as String and put the data in a textbox.

:| So I've been using something like this...

Private Function check_ResitDate_ResitNumber()
        Dim SQLquery As String
        Dim rrnRD As OdbcDataReader
        Dim DiTi As DateTime
        
        SQLquery = "SELECT ResitNom, ResitDate, SUBSTRING(ResitTime,12,5) AS RTime " & _
                   "FROM TableReceipt " & _
                   "WHERE ResitNom = '" & Me.txtResitNom.Text & "' " & _
                   "AND ResitDate = '" & CSQLDate(Me.dtpResitDate.Value) & "' "


        Dim rCmd As New OdbcCommand(SQLquery, connCounter)
	'connCounter has been declared as a database connection in another main module

        rrnRD = rCmd.ExecuteReader()

        If rrnRD.HasRows Then
             DiTi = Format(rrnRD.Item("RTime"), "HH:mm")
             Me.txtWaktuResit.Text = DiTi                                  
        End If

	rrnRD.Close()
      

End Function

But it doesnt work. It gives me this error:

System.InvalidCastException: Specified cast is not valid.
at Microsoft. VisualBasic.Strings.Format(Object Expression, String Style)
at [blahblahblah] line [###]

The MySQL data for column "ResitTime" looks like this:

2011-01-07 15:02:07

I want to use this to cross reference with other table in other database, but to do that i need to copy the data into a textbox as string or a datetimepicker as datetime. But string would be more convenient. :p

I appreciate the help, thank you.

Recommended Answers

All 2 Replies

Since you convert date and time to a string, declare DiTi as string. Also read the ResitTime as it is and after reading value successfully, take hours and minutes

Dim ResitTime As String = "2011-01-07 15:02:07" ' Debug
Dim TempDate As DateTime ' ResitTime as DateTime
Dim DiTi As String ' "HH:mm"

DiTi = ""
' No need to TryParse if ResitTime is in the correct datetime format
' Test first null value
If Not IsDBNull(ResitTime) AndAlso Date.TryParse(ResitTime, TempDate) Then
  DiTi = TempDate.ToString("HH:mm") ' Convert datetime to string
End If

HTH

Yes! Thanks a lot Teme64! You saved my day!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.