从dbnull类型到double类型的转换无效。

huangapple go评论60阅读模式
英文:

Conversion from type dbnull to type double is not valid

问题

<!-- 函数 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)
    If Not IsDBNull(dr("GrandTotal")) Then
        total = CDbl(dr("GrandTotal"))
    End If
    Return total
End 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

<!-- 函数 3-->

Function NormalizeRecord(ByVal value As String) As String
    If value = "-" Then
        value = "0"
    End If
    Return value
End Function

函数 1 的问题是,如果返回的值是 null,它会引发错误(从类型 dbnull 转换为类型 double 不是有效的)。我在函数 1 中添加了一个检查,以确保值不是 null,如果值是 null,它将总数设置为 0。这应该可以解决 null 值的问题。

英文:
<!-- 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 as described in my code is supposed to sum the column total and return the result but it always throw error (Conversion from type dbnull to type double is not valid) if it returns null value especially when am inserting record for the first time. how can i control null value?

答案1

得分: 1

首先,有两种处理方法。

首先,你可能没有任何行,或者返回的唯一行具有空值。

如果你执行一个 "sum()",那么如果任何行不为空,你就会得到一个值。

然而,如果没有行,或者行中的列为空,你会看到/得到/找到一个空值。

所以,一个简单的解决办法是使用 isnull。

所以,你的代码可以这样写:

.Add("IsNull(SUM(" & StudentData.Total & "),0) AS GrandTotal")

上面的方法可能是你最好的选择,因为即使查询由于筛选而不返回任何行,你仍然会得到一个0。

编辑:我看到你将此标记为 MySQL,而不是 SQL Server - 这是我的错误,所以我建议使用以下解决方案。

然而,经常情况下,实际上相当多,你会在你的数据表中遇到空值
(顺便问一下,你为什么要在数据表的位置使用数据集?在这里你不需要数据集,因为你没有一个表的集合)。

所以,接下来,由于你经常要做这个?

将下面的内容放在你的 "系统范围" 实用程序例程中:

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 查询,并让它返回那些空行的0值。

英文:

Well, there are two ways to deal with this.

First up, you might have no rows, or the ONLY row returned has a null value.

If you doing a "sum()", then if ANY of the rows are not null, then you WILL get a value back.

however, no rows, or rows with the column that are null, then you see/get/find a null.

So, one easy fix would be to use isnull.

So, your code could say use this:

.Add("IsNull(SUM(" & StudentData.Total & "),0) AS GrandTotal")

The above is probably your best bet, since EVEN if the query were to not even return any rows due to filter, you still get a 0 back.

Edit: I see you tagged this as MySQL, and not SQL server - my mistake, so I suggest using below solution.

however, often, in fact quite a bit, you will encounter null values in your data tables
(BTW, why are you using a dataset in place of a data table? You don't need a dataset here, since you don't have a colleciton of tables).

So, next up, since you often have do to this?

Place in your "system wide" bag of utility routines this:

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

So, now you can say do this:

total = nz(dr("GrandTotal"),0)

You could/can modify the SQL query, and have it return a 0 value for those null rows.

huangapple
  • 本文由 发表于 2023年1月9日 04:14:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定