1

I mam reading data from MySQL, one field is a datetime field in MySQL.

i want to get the value into a variable and convert to a string so i can get just the first 10 characters:

Dim cdr_date As String = reader.GetString(0)

cdr_date = cdr_date.Substring(0, 10)

but this is showing an error in vb.net saying unable to convert datetime to system datetime

ive also tried these but get the same error:

Dim cdr_date As DateTime = reader.GetDateTime(0).ToString()
cdr_date = cdr_date.Substring(0, 10)


Dim cdr_date As DateTime = reader.GetString(0).ToString()
cdr_date = cdr_date.Substring(0, 10)
5
  • what do you mean, just the first 10 chars? Are you trying to split off the Time? If the variable is DateTime, it is best served to leave it as DateTime. It wont act like a DateTime when in a string Commented Feb 29, 2016 at 22:53
  • my MySQL field is Date but when running a select query, in vb.net for some reason its showing as DateTime as it will return the date and then 00:00:00 so i am just trying to get the date, but its fine getting it as a string. once selected it no longer needs to be a Date Commented Feb 29, 2016 at 23:06
  • yeah i can do, why though? Commented Mar 1, 2016 at 0:14
  • 1
    aha ok lol - thanks for your help! Commented Mar 1, 2016 at 0:15
  • in the end it was bad data or something so it adds nothing Commented Mar 1, 2016 at 0:16

1 Answer 1

1

You cant just stuff one data type into a variable of another without converting. Some of the code won't compile under Option Strict, because it tries to do just that:

Dim cdr_date As DateTime = reader.GetDateTime(0).ToString()

It sounds like you are trying to strip the time from a db var, so get it as a DateTime then if you must use it as string somewhere convert it:

Dim myDT As DateTime
rdr.Read()

' these do the same thing - get it as date:
myDT = Convert.ToDateTime(rdr("zDateTime"))
myDT = rdr.GetDateTime(0)

A DateTime will always have a Date and a Time. To ignore the Time, use something like "MM/dd/yyyy" to ignore it either when displaying it or converting to string. SubString and the magic number of 10 is not needed.

TextBox13.Text = myDT.ToString("MM/dd/yyyy")

Or to save to a string variable:

Dim notADate As String
' from a DB Reader:
notADate = rdr.GetDateTime(0).ToString("MM/dd/yyyy")
' in general:
notADate = myDT.ToString("MM/dd/yyyy")

However, this: unable to convert datetime to system datetime indicates something else it wrong. The DB Provider object is apparently unable to convert the db data to a NET DateTime Type. The most likely cause is bad data.

Rather than null, MySQL can be told to store 01/01/0001 00:00:00 as a 'zero date". For this, you may need to modify your connection string to append this:

"server=....;convertzerodatetime=True;allowzerodatetime=True"

These enable the storing and conversion of otherwise illegal DateTime values. AllowZeroDateTime in particular will return a zero date time rather than throw an exception. In this case (resolved in comments), since the return was a Zero Date but not what what seen in the DB, deleting it and creating a new row resolved it.

There are many, many other options for the MySQL connection:

MySQL ConnectionString options

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.