2017-01-03 65 views
-4

我有下面的代碼轉換」到「日期」是無效的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 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 

在運行時該代碼它給我的錯誤「從字符串轉換‘’要‘日期’是無效」

+3

嘗試調試,夥計。 – duDE

+2

你不明白的消息(在這種情況下谷歌它),或者你不明白如何調試它?某處想要將一個字符串(它是空的)轉換爲日期。我們無法看到您的數據或您的變量的值,並且我們看不到任何具有DateTime或Date類型的變量,並且您沒有給出行號,所以我們無法確定哪裏問題是。但蒂姆的猜測是明智的。 dtLeave本身的價值也可能是一個問題,如果這是一個字符串保存在數據庫中(而不是日期) – ADyson

+0

''「''代表什麼日期,在你個人的視野中? –

回答

0

嘗試改變你的代碼爲:

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