Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从字符串“”到“日期”的转换无效vb.net_Vb.net - Fatal编程技术网

从字符串“”到“日期”的转换无效vb.net

从字符串“”到“日期”的转换无效vb.net,vb.net,Vb.net,我有以下代码 LastLeaveDt = DispName(con, "Select max(dtLeave) from EmpLeave where Empid='" & eid.Text & "' and year(dtleave)='" & cmbYear.Text & "'") Public Function DispName(cn As ADODB.Connection, mQRY As String) As String Dim xrs1 A

我有以下代码

LastLeaveDt = DispName(con, "Select max(dtLeave) from EmpLeave where Empid='" & eid.Text & "' and year(dtleave)='" & cmbYear.Text & "'")

Public Function DispName(cn As ADODB.Connection, mQRY As String) As String
    Dim xrs1 As New ADODB.Recordset
    Try
        xrs1.Open(mQRY, cn, ADODB.CursorTypeEnum.adOpenForwardOnly)
        If xrs1.EOF = False Then
            DispName = IIf(IsDBNull(xrs1(0).Value), "", xrs1(0).Value)
        End If
        xrs1.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
        Return False
    End Try
End Function

在运行时的这段代码中,从字符串到“日期”的转换是无效的,请尝试将代码更改为:

 If xrs1.EOF = False Then
        If IsDate (IsDBNull(xrs1(0).Value)) then
             DispName = xrs1(0).Value
         else
             DispName =""
        End If
   End If

试着调试一下,老兄。你不明白消息的意思吗?在这种情况下,用谷歌搜索一下,或者你不知道如何调试它?在某个地方,您正试图将空字符串转换为日期。我们看不到您的数据或变量的值,也看不到任何DateTime或Date类型的变量,而且您没有给出行号,因此我们无法确定问题在哪里。然而,蒂姆的猜测是明智的。dtLeave本身的值也可能是一个问题,如果它在DB中是一个字符串,而不是一个日期,那么在您个人的视野中,日期应该代表什么?作为旁注:应该使用if操作符来代替旧的IIf函数。使用Return语句比指定结果VB6样式更符合NET