[英]Conversion from type dbnull to type double is not valid
<!-- Function 1-->
Function GetStudentResultTotal(ByVal schid As String, ByVal level As String, ByVal session As String, ByVal term As String, ByVal klass As String, ByVal regno As String) As Double
If SCH_ID <> "" Then
schid = SCH_ID
End If
Dim total As Double = 0
Dim subjectCount As Integer = 0
Dim fields As New ArrayList
fields.Add("SUM(" & StudentData.Total & ") AS GrandTotal")
Dim filterValues As New Hashtable
filterValues.Add(StudentData.SchoolID, schid)
filterValues.Add(StudentData.Level, level)
filterValues.Add(StudentData.Session, session)
filterValues.Add(StudentData.Term, term)
filterValues.Add(StudentData.Klass, klass)
filterValues.Add(StudentData.RegNo, regno)
Dim filterArgs As String = "WHERE " & StudentData.SchoolID & "=@" & StudentData.SchoolID & " AND " & StudentData.Level & "=@" & StudentData.Level & " AND " & StudentData.Session & "=@" & StudentData.Session & " AND " & StudentData.Term & "=@" & StudentData.Term & " AND " & StudentData.Klass & "=@" & StudentData.Klass & " AND " & StudentData.RegNo & "=@" & StudentData.RegNo
Dim data As DataSet = _Data.GetData(StudentData.tblStudentResult, fields, filterValues, filterArgs)
'If data.Tables(0).Rows.Count > 0 Then
' For Each dr As DataRow In data.Tables(0).Rows
' total += CDbl(NormalizeRecord(dr(StudentData.Total)))
' subjectCount += 1
' Next
'End If
Dim dr As DataRow = data.Tables(0).Rows(0)
total = CDbl(dr("GrandTotal"))
Return total
End Function
<!-- Function 2-->
Function GetData(ByVal tbl As String, ByVal values As ArrayList, ByVal filters As Hashtable, ByVal filterArgs As String) As DataSet
Dim _ds As New DataSet
Dim sql As String = "SELECT "
Dim fields As String = ""
Using conn As New MySqlConnection(connString)
conn.Open()
If values IsNot Nothing Then
For i = 0 To values.Count - 1
If fields = "" Then
fields = values.Item(i).ToString
Else
fields &= "," & values.Item(i).ToString
End If
Next
sql &= fields & " "
End If
sql &= "FROM " & tbl
If filterArgs <> "" Then
sql &= " " & filterArgs
End If
Dim cmd As New MySqlCommand(sql, conn)
If filters IsNot Nothing Then
For i = 0 To filters.Count - 1
cmd.Parameters.AddWithValue("@" & filters.Keys(i), filters.Values(i))
Next
End If
Dim da As New MySqlDataAdapter(cmd)
da.Fill(_ds)
conn.Close()
End Using
Return _ds
End Function
<!-- Function 3-->
Function NormalizeRecord(ByVal value As String) As String
If value = "-" Then
value = "0"
End If
Return value
End Function
我的代碼中描述的 Function 1 應該對列總計求和並返回結果,但如果它返回 null 值,它總是會拋出錯誤(從 dbnull 類型到 double 類型的轉換無效),尤其是在第一次插入記錄時。 我如何控制 null 值?
那么,有兩種方法可以解決這個問題。
首先,您可能沒有任何行,或者返回的唯一行具有 null 值。
如果您執行“sum()”,那么如果任何行不是 null,那么您將得到一個值。
但是,沒有行,或者列中的行是 null,那么你會看到/得到/找到一個 null。
因此,一個簡單的解決方法是使用 isnull。
所以,你的代碼可以說使用這個:
.Add("IsNull(SUM(" & StudentData.Total & "),0) AS GrandTotal")
以上可能是您最好的選擇,因為即使查詢由於篩選甚至不返回任何行,您仍然會返回 0。
編輯:我看到你將其標記為 MySQL,而不是 SQL 服務器 - 我的錯誤,所以我建議使用以下解決方案。
但是,實際上,您經常會在 data.tables 中遇到 null 個值(順便說一句,為什么要使用數據集代替 data.table?這里不需要數據集,因為您沒有表的集合)。
那么,接下來,既然你經常這樣做?
將以下內容放入您的“系統范圍”實用程序包中:
Public Function Nz(ByVal Value As Object,
Optional ByVal MyDefault As Object = "") As Object
If Value Is Nothing OrElse IsDBNull(Value) Then
Return MyDefault
Else
Return Value
End If
End Function
所以,現在你可以說這樣做:
total = nz(dr("GrandTotal"),0)
您可以/可以修改 SQL 查詢,並讓它為那些 null 行返回 0 值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.