我的函数正常工作,直到我返回空或NULL结果,然后我收到错误:Conversion from string "" to type 'Date' is not valid.
VB.NET:
Public Function GetNextWaitListed(ByVal ClassName As String, ByVal ClassDate As Date, ByVal ClassTime As String) As String
Dim connStr As String = ConfigurationManager.AppSettings.Get("TechTrainingConn")
Dim conn As New Data.OleDb.OleDbConnection(connStr)
Try
conn.Open()
Dim sql As String = "SELECT Min(SubmitTime) FROM [EnrollmentsTbl]" & _
" WHERE [ClassName] = """ & ClassName & """" & _
" AND [ClassDate] = #" & ClassDate & "#" & _
" AND [ClassTime] = """ & ClassTime & """" & _
" AND [Waitlisted] = True" & _
" AND [Completed] = False" & _
" AND [Enrolled] = True"
Dim comm As New Data.OleDb.OleDbCommand(sql, conn)
Dim result As Date = comm.ExecuteScalar()
If Not String.IsNullOrEmpty(result) Then
Return result
End If
Catch ex As Exception
Response.Write(ex)
Finally
conn.Close()
End Try
End Function
我尝试过以下操作但是因为我返回date
If Not String.IsNullOrEmpty(result) Then
Dim oDate As DateTime = Convert.ToDateTime(result)
Return oDate
End If
答案 0 :(得分:0)
对我来说最好的解决方案是返回对象,如果对象为空,则使用虚拟日期
执行其他操作Public Function GetNextWaitListed(ByVal ClassName As String, ByVal ClassDate As Date, ByVal ClassTime As String) As String
Dim connStr As String = ConfigurationManager.AppSettings.Get("TechTrainingConn")
Dim conn As New Data.OleDb.OleDbConnection(connStr)
Try
conn.Open()
Dim sql As String = "SELECT Min(SubmitTime) FROM [EnrollmentsTbl]" & _
" WHERE [ClassName] = """ & ClassName & """" & _
" AND [ClassDate] = #" & ClassDate & "#" & _
" AND [ClassTime] = """ & ClassTime & """" & _
" AND [Waitlisted] = True" & _
" AND [Completed] = False" & _
" AND [Enrolled] = True"
Dim comm As New Data.OleDb.OleDbCommand(sql, conn)
Dim Obj As Object = comm.ExecuteScalar()
If (Obj IsNot Nothing) AndAlso (Obj IsNot DBNull.Value) Then
Dim matches As String = Obj.ToString
Dim result As Date = Convert.ToDateTime(matches)
Return result
Else
Dim result As Date = Convert.ToDateTime("01/01/1900")
Return result
End If
Catch ex As Exception
Response.Write(ex)
If Not conn Is Nothing Then
conn.Close()
End If
Finally
conn.Close()
End Try
End Function