gpt4 book ai didi

database - DBnull 到整数转换错误

转载 作者:搜寻专家 更新时间:2023-10-30 22:14:42 25 4
gpt4 key购买 nike

我得到 从类型 'DBNull' 到类型 'Integer' 的转换无效。”错误行“Dim avgObject As string = Cstr(avgCom.ExecuteScalar())

当删除 where module_ID='"& moduleSelect & "' 语句时,该命令有效,我不知道如何解决这个问题,有人可以帮忙吗?

    Dim moduleSelect As String = moduleRadio.SelectedValue
Using connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True")
Using avgCom As New SqlCommand("SELECT AVG(exam) FROM completed_module where module_ID='" & moduleSelect & "' ", _
connection)
connection.Open()
Dim avgObject As Integer = CInt(avgCom.ExecuteScalar())
Dim averageVar As String
averageVar = avgObject.ToString
avgLabel.Text = averageVar
End Using

最佳答案

我相信你正在寻找这样的东西,首先检查它是否为 dbnull:

Dim moduleSelect As String = moduleRadio.SelectedValue

Using connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True")
Using avgCom As New SqlCommand("SELECT AVG(exam) FROM completed_module where module_ID='" & moduleSelect & "' ", _
connection)
connection.Open()
Dim result = avgCom.ExecuteScalar()
If IsDBNull(result) then return
Dim avgObject As Integer = CInt(result)
Dim averageVar As String
averageVar = avgObject.ToString
avgLabel.Text = averageVar
End Using

关于database - DBnull 到整数转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15882133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com