无法将字符串识别为有效的DateTime Datetimepicker vb.net

时间:2019-05-09 06:38:25

标签: vb.net datetimepicker string-to-datetime

我需要在datetimepicker中显示从mysql数据库获取的日期。运行此代码时出现此错误。

  

系统。 FormatException:字符串未被识别为有效字符串   日期时间
      在System.DateTimeParse.ParseExact(String s,String format,       DateTimeFormatInfo dtfi,DateTimeStyIes样式)
      在系统。 DateTime.ParseExact(String s,字符串格式,IFormatProvider       提供者)

注意:当mysql读取器读取rdate值时,此值为2/13/2019 12:00:00 AM       rdatecorrected值为2/13/2019

我看到了一些类似问题的答案,它们使用一个M而不是MM和一个d而不是dd来匹配一个数字的月和日期值的格式。但是我全年都每天都有这些记录,所以我需要一种方法来完成这项工作,即使是像9那样的单值月份或像11那样的两个值月

我仍然是初学者,所以如果您能向我展示经过编辑的代码的解决方案,我将不胜感激。

imports: Imports System.IO
Imports System
Imports MySql.Data.MySqlClient
Imports System.Globalization

    If System.IO.File.Exists("D:\DBSystem\Connection\connconfig.dbs") = True Then

        Dim realstuno As Integer
        realstuno = ESD_Realstudentno.Text

        Dim strFile As String = "D:\DBSystem\Connection\connconfig.dbs"
        Dim sr As New IO.StreamReader(strFile)
        Dim ip As String
        Dim port As String
        Dim userid As String
        Dim password As String
        Dim database As String

        'reading
        ip = sr.ReadLine()
        port = sr.ReadLine()
        userid = sr.ReadLine()
        password = sr.ReadLine()
        database = sr.ReadLine()
        sr.Close()


        'default connection code
        Dim serverstring As String = "server=" & ip & ";port=" & port & ";database=" & database & ";user ID=" & userid & ";password=" & password & ";OldGuids=true"
        Dim sqlconnection As MySqlConnection = New MySqlConnection
        sqlconnection.ConnectionString = serverstring
        Dim Reader As MySqlDataReader
        Dim Rdate As String
        Dim SDA As New MySqlDataAdapter
        Dim DbDataSet As New DataTable
        Dim bSource As New BindingSource
        Dim rdatecorrected As String

        'custom date format
        'rdate
        ESD_Rdate.Format = DateTimePickerFormat.Custom
        ESD_Rdate.CustomFormat = "MM/dd/yyyy"


        Try
            If sqlconnection.State = ConnectionState.Closed Then
                sqlconnection.Open()

                'search
                Dim sqlstatement As String = "Select Registereddate FROM `SDS_Data` WHERE `StudentNo` ='" & realstuno & "'"
                Dim Command = New MySqlCommand(sqlstatement, sqlconnection)
                Reader = Command.ExecuteReader
                If Reader.HasRows() Then
                    While Reader.Read


                        'rdate
                        Rdate = Reader.GetString("Registereddate")
                        rdatecorrected = Rdate.Remove(Rdate.Length - 12, 12)
                        Dim dt As DateTime = DateTime.ParseExact("" & rdatecorrected & "", "MM/dd/yyyy", CultureInfo.InvariantCulture)
                        ESD_Rdate.Value = dt

                    End While
                Else
                    MessageBox.Show("No records found!", "Error")
                End If
            Else
                sqlconnection.Close()
                MsgBox("Connection Error!", "Error")

            End If

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    Else
        MessageBox.Show("Do not have any previous database configurations!", "Error",
           MessageBoxButtons.OK)
    End If

0 个答案:

没有答案