如何解決「對象不能從DBNull轉換爲其他類型」? 我正在使用組合框來檢索服務的價格和折扣價格,如果我在combobox3中選擇一個項目一次,錯誤不會發生,但是如果我在combobox3中再次選擇一個項目,「對象不能從DBNull轉換爲其他類型「。會發生..如何解決「對象不能從DBNull轉換爲其他類型」。
這裏是我的代碼
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
這是我的查詢
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
這裏是圖像
可能是什麼造成具有這個問題?
什麼迭代行的點,如果你分配給一組單獨的文本框? – StingyJack
這甚至沒有編譯。 – StingyJack