【发布时间】:2014-06-15 12:37:22
【问题描述】:
我的代码有问题吗?我遇到“从类型 '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")
我知道这个错误很常见,但直到现在我还不太清楚我是如何或为什么会出现这个错误的。
感谢您的启发!
【问题讨论】:
标签: sql vb.net ms-access oledbcommand