-2

how can I troubleshoot "Object cannot be cast from DBNull to other types."? I am using a combobox, to retrieve the price and discount price of the service, If I select an item in combobox3 once, error will not occur, but if I select a item again in combobox3, "Object cannot be cast from DBNull to other types." will occur..

here is my code

Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged

    Dim ds As New DataSet

    ds = getPrice(arr(ComboBox2.SelectedIndex), ComboBox3.Text)


    If ds.Tables("getPrice").Rows.Count > 0 Then
        For i As Integer = 0 To ds.Tables("getPrice").Rows.Count - 1

            TextBox1.Text = Convert.ToDecimal(ds.Tables("getPrice").Rows(i).Item(0))
            TextBox2.Text = Convert.ToDecimal(ds.Tables("getPrice").Rows(i).Item(1))

        Next
    End If

End Sub

this is my query

 Public Function getPrice(ByVal serviceId As String, ByVal breedSize As String)

    If breedSize = "Small Breed" Then
        sqlStr = "Select price_small_breed, discount_small_breed From tblServicePrice Where service_id = " + serviceId + ""
    ElseIf breedSize = "Medium Breed" Then
        sqlStr = "Select price_medium_breed, discount_medium_breed From tblServicePrice Where service_id = " + serviceId + ""
    ElseIf breedSize = "Big Breed" Then
        sqlStr = "Select price_big_breed, discount_big_breed From tblServicePrice Where service_id = " + serviceId + ""
    End If

    ds.Clear()
    da = New OleDbDataAdapter(sqlStr, con.ConnectionString)
    da.Fill(ds, "getPrice")


    Return ds

End Function

here is the image

enter image description here

what could be the caused of having this problem?

4

4 回答 4

1
TextBox1.Text = If(ds.Tables("getPrice").Rows(i).Item(0) Is DBNull.Value, "", Convert.ToDecimal(ds.Tables("getPrice").Rows(i).Item(0)).ToString()) 
TextBox2.Text = If(ds.Tables("getPrice").Rows(i).Item(0) Is DBNull.Value, "", Convert.ToDecimal(ds.Tables("getPrice").Rows(i).Item(1)).ToString()) 
于 2012-06-20T17:01:51.060 回答
1

如果您针对数据库运行在 GetPrice 中执行的特定查询语句,您将看到至少有一行在您正在读取的序号位置具有空值。

serviceID(注意大写)可能不是您假设的值。

于 2012-06-20T17:32:32.800 回答
0

如果数据库中有 NULL 值,会发生什么?如果可以接受使用某个值,例如零,您可以修改查询,如

sqlStr = "Select COALESCE(price_small_breed, 0), COALESCE(discount_small_breed, 0) From tblServicePrice Where service_id = " & serviceId & ""
于 2012-06-20T17:23:42.707 回答
-4

我的答案是这样的。我声明了新数据集,我没有使用代码顶部的数据集

Public Function getPrice(ByVal serviceId As String, ByVal breedSize As String) As DataSet

        Dim dsa As New DataSet


        If breedSize = "Small Breed" Then
            sqlStr = "Select price_small_breed, discount_small_breed From tblServicePrice Where service_id = " + serviceId + ""
        ElseIf breedSize = "Medium Breed" Then
            sqlStr = "Select price_medium_breed, discount_medium_breed From tblServicePrice Where service_id = " + serviceId + ""
        ElseIf breedSize = "Big Breed" Then
            sqlStr = "Select price_big_breed, discount_big_breed From tblServicePrice Where service_id = " + serviceId + ""
        End If

        dsa.Clear()
        da = New OleDbDataAdapter(sqlStr, con.ConnectionString)
        da.Fill(dsa, "getPrice")

        Return dsa

    End Function

这是我以前的代码..

Public Function getPrice(ByVal serviceId As String, ByVal breedSize As String)

    If breedSize = "Small Breed" Then
        sqlStr = "Select price_small_breed, discount_small_breed From tblServicePrice Where service_id = " + serviceId + ""
    ElseIf breedSize = "Medium Breed" Then
        sqlStr = "Select price_medium_breed, discount_medium_breed From tblServicePrice Where service_id = " + serviceId + ""
    ElseIf breedSize = "Big Breed" Then
        sqlStr = "Select price_big_breed, discount_big_breed From tblServicePrice Where service_id = " + serviceId + ""
    End If

    ds.Clear()
    da = New OleDbDataAdapter(sqlStr, con.ConnectionString)
    da.Fill(ds, "getPrice")


    Return ds

End Function
于 2012-06-20T17:33:12.793 回答