仅当String不为null或为空时,我才具有解析日期字段的代码,但出现以下异常
从字符串“”到类型“日期”的转换无效。
说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
异常详细信息:System.InvalidCastException:从字符串“”到类型“日期”的转换无效。
源错误:
Line 29:
Line 30: If (Not String.IsNullOrEmpty(last_login)) Then
Line 31: If String.Format("{0:MM/dd/yy H:mm:ss}", last_login) < Now.AddMinutes(-5) Then
Line 32: Return "height:100%;width:100% ;background-color:#FF0000;font-weight: bold; color: #000000"
Line 33: Else
有人请解释吗?
最佳答案
真是一团糟。您正在尝试在字符串上使用日期格式符号,以生成字符串并与日期进行比较。
让我们尝试一下。
Dim dLast As DateTime
If ((Not last_login Is Nothing) AndAlso DateTime.TryParse(last_login.Trim(), dLast)) Then
If (dLast < Now.AddMinutes(-5)) Then
Return "height:100%;width:100% ;background-color:#FF0000;font-weight: bold; color: #000000"
End If
End If
编辑:访问
null
字符串,然后再访问它。
关于c# - 空字符串的怪异情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3866321/