我的代码有问题吗?我有一个" 转换类型' DBNull'输入' String'无效"错误。
调用的函数(GetNoofHoursOfTImeIn):
Public Shared Function GetNoofHoursofTImeIn(ByVal search As String, ByVal dfrom As DateTime, ByVal dto As DateTime) As DataTable
Dim dtoffenseinfo As New DataTable
If Not DBConnection.State = ConnectionState.Open Then
DBConnection.Open()
End If
Using cmd As New OleDbCommand
cmd.Connection = DBConnection
cmd.CommandText = _
"SELECT SUM(No_of_Hour) AS THour FROM EmployeeAttendance " & _
"WHERE EmployeeID = ? " & _
"AND WorkingDate BETWEEN ? AND ?"
cmd.Parameters.AddWithValue("?", search)
cmd.Parameters.AddWithValue("?", dfrom.Date)
cmd.Parameters.AddWithValue("?", dto.Date)
Using adapter As New OleDbDataAdapter(cmd)
adapter.Fill(dtoffenseinfo)
End Using
End Using
DBConnection.Close()
Return dtoffenseinfo
End Function
代码背后:
txtPresentDays.Text = ClsAttendance.GetNoofHoursofTImeIn(EmployeeID.Text, MyDateString, MyDateString2).Rows(0).Item("THour")
我知道这个错误很常见,但直到现在我还不知道我是如何或为什么会这样做的。
感谢任何启蒙!
答案 0 :(得分:1)
当零记录与条件匹配时,您将获得空值。您可以使用iif
函数将该值转换为零:
SELECT IIF(SUM(No_of_Hour) Is Null, 0, SUM(No_of_Hour)) ...